blob: 52a44560005beb352df2c481258084d929d25dde [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 Chiendd361c92012-04-10 23:40:37 +080022#include "dex_verifier.h"
Logan Chiena78e3c82011-12-27 17:59:35 +080023#include "inferred_reg_category_map.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080024#include "ir_builder.h"
25#include "logging.h"
Logan Chien4dd96f52012-02-29 01:26:58 +080026#include "oat_compilation_unit.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080027#include "object.h"
28#include "object_utils.h"
Logan Chien42e0e152012-01-13 15:42:36 +080029#include "runtime_support_func.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"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080034
35#include <iomanip>
36
37#include <llvm/Analysis/Verifier.h>
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()),
63 irb_(*cunit->GetIRBuilder()), func_(NULL), retval_reg_(NULL),
64 basic_block_reg_alloca_(NULL), basic_block_shadow_frame_alloca_(NULL),
65 basic_block_reg_zero_init_(NULL), basic_block_reg_arg_init_(NULL),
66 basic_blocks_(code_item_->insns_size_in_code_units_),
67 basic_block_landing_pads_(code_item_->tries_size_, NULL),
68 basic_block_unwind_(NULL), basic_block_unreachable_(NULL),
Logan Chien937105a2012-04-02 02:37:37 +080069 shadow_frame_(NULL), elf_func_idx_(cunit_->AcquireUniqueElfFuncIndex()) {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080070}
71
72
73MethodCompiler::~MethodCompiler() {
Logan Chienc670a8d2011-12-20 21:25:56 +080074 STLDeleteElements(&regs_);
Shih-wei Liaod1fec812012-02-13 09:51:10 -080075}
76
77
Logan Chien0b827102011-12-20 19:46:14 +080078void MethodCompiler::CreateFunction() {
79 // LLVM function name
Logan Chien937105a2012-04-02 02:37:37 +080080 std::string func_name(ElfFuncName(elf_func_idx_));
Logan Chien0b827102011-12-20 19:46:14 +080081
82 // Get function type
83 llvm::FunctionType* func_type =
Logan Chiendd361c92012-04-10 23:40:37 +080084 GetFunctionType(method_idx_, oat_compilation_unit_->IsStatic());
Logan Chien0b827102011-12-20 19:46:14 +080085
86 // Create function
87 func_ = llvm::Function::Create(func_type, llvm::Function::ExternalLinkage,
88 func_name, module_);
89
90 // Set argument name
91 llvm::Function::arg_iterator arg_iter(func_->arg_begin());
92 llvm::Function::arg_iterator arg_end(func_->arg_end());
93
94 DCHECK_NE(arg_iter, arg_end);
95 arg_iter->setName("method");
96 ++arg_iter;
97
Logan Chiendd361c92012-04-10 23:40:37 +080098 if (!oat_compilation_unit_->IsStatic()) {
Logan Chien0b827102011-12-20 19:46:14 +080099 DCHECK_NE(arg_iter, arg_end);
100 arg_iter->setName("this");
101 ++arg_iter;
102 }
103
104 for (unsigned i = 0; arg_iter != arg_end; ++i, ++arg_iter) {
105 arg_iter->setName(StringPrintf("a%u", i));
106 }
107}
108
109
110llvm::FunctionType* MethodCompiler::GetFunctionType(uint32_t method_idx,
111 bool is_static) {
112 // Get method signature
113 DexFile::MethodId const& method_id = dex_file_->GetMethodId(method_idx);
114
Logan Chien8faf8022012-02-24 12:25:29 +0800115 uint32_t shorty_size;
Logan Chien0b827102011-12-20 19:46:14 +0800116 char const* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size);
Logan Chien8faf8022012-02-24 12:25:29 +0800117 CHECK_GE(shorty_size, 1u);
Logan Chien0b827102011-12-20 19:46:14 +0800118
119 // Get return type
120 llvm::Type* ret_type = irb_.getJType(shorty[0], kAccurate);
121
122 // Get argument type
123 std::vector<llvm::Type*> args_type;
124
125 args_type.push_back(irb_.getJObjectTy()); // method object pointer
126
127 if (!is_static) {
128 args_type.push_back(irb_.getJType('L', kAccurate)); // "this" object pointer
129 }
130
Logan Chien8faf8022012-02-24 12:25:29 +0800131 for (uint32_t i = 1; i < shorty_size; ++i) {
Logan Chien0b827102011-12-20 19:46:14 +0800132 args_type.push_back(irb_.getJType(shorty[i], kAccurate));
133 }
134
135 return llvm::FunctionType::get(ret_type, args_type, false);
136}
137
138
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800139void MethodCompiler::EmitPrologue() {
Logan Chienc670a8d2011-12-20 21:25:56 +0800140 // Create basic blocks for prologue
TDYa1274165a832012-04-03 17:47:16 -0700141 basic_block_stack_overflow_ =
142 llvm::BasicBlock::Create(*context_, "prologue.stack_overflow_check", func_);
143
Logan Chienc670a8d2011-12-20 21:25:56 +0800144 basic_block_reg_alloca_ =
145 llvm::BasicBlock::Create(*context_, "prologue.alloca", func_);
146
Logan Chien8dfcbea2012-02-17 18:50:32 +0800147 basic_block_shadow_frame_alloca_ =
148 llvm::BasicBlock::Create(*context_, "prologue.shadowframe", func_);
149
Logan Chienc670a8d2011-12-20 21:25:56 +0800150 basic_block_reg_zero_init_ =
151 llvm::BasicBlock::Create(*context_, "prologue.zeroinit", func_);
152
Logan Chiend6ececa2011-12-27 16:20:15 +0800153 basic_block_reg_arg_init_ =
154 llvm::BasicBlock::Create(*context_, "prologue.arginit", func_);
155
TDYa1274165a832012-04-03 17:47:16 -0700156 // Before alloca, check stack overflow.
157 EmitStackOverflowCheck();
158
Logan Chienc670a8d2011-12-20 21:25:56 +0800159 // Create register array
160 for (uint16_t r = 0; r < code_item_->registers_size_; ++r) {
161 regs_.push_back(DalvikReg::CreateLocalVarReg(*this, r));
162 }
163
164 retval_reg_.reset(DalvikReg::CreateRetValReg(*this));
Logan Chiend6ececa2011-12-27 16:20:15 +0800165
Logan Chien8dfcbea2012-02-17 18:50:32 +0800166 // Create Shadow Frame
167 EmitPrologueAllocShadowFrame();
168
Logan Chiend6ececa2011-12-27 16:20:15 +0800169 // Store argument to dalvik register
170 irb_.SetInsertPoint(basic_block_reg_arg_init_);
171 EmitPrologueAssignArgRegister();
172
173 // Branch to start address
174 irb_.CreateBr(GetBasicBlock(0));
Logan Chienc670a8d2011-12-20 21:25:56 +0800175}
176
177
TDYa1274165a832012-04-03 17:47:16 -0700178void MethodCompiler::EmitStackOverflowCheck() {
179 irb_.SetInsertPoint(basic_block_stack_overflow_);
180
181 // Call llvm intrinsic function to get frame address.
182 llvm::Function* frameaddress =
183 llvm::Intrinsic::getDeclaration(module_, llvm::Intrinsic::frameaddress);
184
185 // The type of llvm::frameaddress is: i8* @llvm.frameaddress(i32)
186 llvm::Value* frame_address = irb_.CreateCall(frameaddress, irb_.getInt32(0));
187
188 // Cast i8* to int
189 frame_address = irb_.CreatePtrToInt(frame_address, irb_.getPtrEquivIntTy());
190
191 // Get thread.stack_end_
192 llvm::Value* thread_object_addr =
193 irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
194
195 llvm::Value* stack_end_addr =
196 irb_.CreatePtrDisp(thread_object_addr,
197 irb_.getPtrEquivInt(Thread::StackEndOffset().Int32Value()),
198 irb_.getPtrEquivIntTy()->getPointerTo());
199
200 llvm::Value* stack_end = irb_.CreateLoad(stack_end_addr);
201
202 // Check the frame address < thread.stack_end_ ?
203 llvm::Value* is_stack_overflow = irb_.CreateICmpULT(frame_address, stack_end);
204
205 llvm::BasicBlock* block_exception =
206 llvm::BasicBlock::Create(*context_, "stack_overflow", func_);
207
208 llvm::BasicBlock* block_continue =
209 llvm::BasicBlock::Create(*context_, "stack_overflow_cont", func_);
210
211 irb_.CreateCondBr(is_stack_overflow, block_exception, block_continue);
212
213 // If stack overflow, throw exception.
214 irb_.SetInsertPoint(block_exception);
215 irb_.CreateCall(irb_.GetRuntime(ThrowStackOverflowException));
216
217 // Unwind.
Logan Chiendd361c92012-04-10 23:40:37 +0800218 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
TDYa1274165a832012-04-03 17:47:16 -0700219 if (ret_shorty == 'V') {
220 irb_.CreateRetVoid();
221 } else {
222 irb_.CreateRet(irb_.getJZero(ret_shorty));
223 }
224
225 basic_block_stack_overflow_ = block_continue;
226}
227
228
Logan Chienc670a8d2011-12-20 21:25:56 +0800229void MethodCompiler::EmitPrologueLastBranch() {
TDYa1274165a832012-04-03 17:47:16 -0700230 irb_.SetInsertPoint(basic_block_stack_overflow_);
231 irb_.CreateBr(basic_block_reg_alloca_);
232
Logan Chienc670a8d2011-12-20 21:25:56 +0800233 irb_.SetInsertPoint(basic_block_reg_alloca_);
Logan Chien8dfcbea2012-02-17 18:50:32 +0800234 irb_.CreateBr(basic_block_shadow_frame_alloca_);
235
236 irb_.SetInsertPoint(basic_block_shadow_frame_alloca_);
Logan Chienc670a8d2011-12-20 21:25:56 +0800237 irb_.CreateBr(basic_block_reg_zero_init_);
238
239 irb_.SetInsertPoint(basic_block_reg_zero_init_);
Logan Chiend6ececa2011-12-27 16:20:15 +0800240 irb_.CreateBr(basic_block_reg_arg_init_);
241}
242
243
Logan Chien8dfcbea2012-02-17 18:50:32 +0800244void MethodCompiler::EmitPrologueAllocShadowFrame() {
245 irb_.SetInsertPoint(basic_block_shadow_frame_alloca_);
246
247 // Allocate the shadow frame now!
248 uint32_t sirt_size = code_item_->registers_size_;
249 // TODO: registers_size_ is a bad approximation. Compute a
250 // tighter approximation at Dex verifier while performing data-flow
251 // analysis.
252
253 llvm::StructType* shadow_frame_type = irb_.getShadowFrameTy(sirt_size);
254 shadow_frame_ = irb_.CreateAlloca(shadow_frame_type);
255
256 // Zero-initialization of the shadow frame
257 llvm::ConstantAggregateZero* zero_initializer =
258 llvm::ConstantAggregateZero::get(shadow_frame_type);
259
260 irb_.CreateStore(zero_initializer, shadow_frame_);
261
Logan Chien8dfcbea2012-02-17 18:50:32 +0800262 // Store the method pointer
Logan Chien1b0a1b72012-03-15 06:20:17 +0800263 llvm::Value* method_field_addr =
264 irb_.CreatePtrDisp(shadow_frame_,
265 irb_.getPtrEquivInt(ShadowFrame::MethodOffset()),
266 irb_.getJObjectTy()->getPointerTo());
267
Logan Chien8dfcbea2012-02-17 18:50:32 +0800268 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
269 irb_.CreateStore(method_object_addr, method_field_addr);
270
271 // Store the number of the pointer slots
Logan Chien1b0a1b72012-03-15 06:20:17 +0800272 llvm::ConstantInt* num_of_refs_offset =
273 irb_.getPtrEquivInt(ShadowFrame::NumberOfReferencesOffset());
274
275 llvm::Value* num_of_refs_field_addr =
276 irb_.CreatePtrDisp(shadow_frame_, num_of_refs_offset,
277 irb_.getJIntTy()->getPointerTo());
278
279 llvm::ConstantInt* num_of_refs_value = irb_.getJInt(sirt_size);
280 irb_.CreateStore(num_of_refs_value, num_of_refs_field_addr);
Logan Chien8dfcbea2012-02-17 18:50:32 +0800281
282 // Push the shadow frame
283 llvm::Value* shadow_frame_upcast =
284 irb_.CreateConstGEP2_32(shadow_frame_, 0, 0);
285
286 irb_.CreateCall(irb_.GetRuntime(PushShadowFrame), shadow_frame_upcast);
287}
288
289
Logan Chiend6ececa2011-12-27 16:20:15 +0800290void MethodCompiler::EmitPrologueAssignArgRegister() {
291 uint16_t arg_reg = code_item_->registers_size_ - code_item_->ins_size_;
292
293 llvm::Function::arg_iterator arg_iter(func_->arg_begin());
294 llvm::Function::arg_iterator arg_end(func_->arg_end());
295
Logan Chiendd361c92012-04-10 23:40:37 +0800296 uint32_t shorty_size = 0;
297 char const* shorty = oat_compilation_unit_->GetShorty(&shorty_size);
298 CHECK_GE(shorty_size, 1u);
Logan Chiend6ececa2011-12-27 16:20:15 +0800299
300 ++arg_iter; // skip method object
301
Logan Chiendd361c92012-04-10 23:40:37 +0800302 if (!oat_compilation_unit_->IsStatic()) {
Logan Chiend6ececa2011-12-27 16:20:15 +0800303 EmitStoreDalvikReg(arg_reg, kObject, kAccurate, arg_iter);
304 ++arg_iter;
305 ++arg_reg;
306 }
307
Logan Chiendd361c92012-04-10 23:40:37 +0800308 for (uint32_t i = 1; i < shorty_size; ++i, ++arg_iter) {
Logan Chiend6ececa2011-12-27 16:20:15 +0800309 EmitStoreDalvikReg(arg_reg, shorty[i], kAccurate, arg_iter);
310
311 ++arg_reg;
312 if (shorty[i] == 'J' || shorty[i] == 'D') {
313 // Wide types, such as long and double, are using a pair of registers
314 // to store the value, so we have to increase arg_reg again.
315 ++arg_reg;
316 }
317 }
318
319 DCHECK_EQ(arg_end, arg_iter);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800320}
321
322
Logan Chien83426162011-12-09 09:29:50 +0800323void MethodCompiler::EmitInstructions() {
Logan Chiend6c239a2011-12-23 15:11:45 +0800324 uint32_t dex_pc = 0;
325 while (dex_pc < code_item_->insns_size_in_code_units_) {
326 Instruction const* insn = Instruction::At(code_item_->insns_ + dex_pc);
327 EmitInstruction(dex_pc, insn);
328 dex_pc += insn->SizeInCodeUnits();
329 }
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800330}
331
332
Logan Chien83426162011-12-09 09:29:50 +0800333void MethodCompiler::EmitInstruction(uint32_t dex_pc,
334 Instruction const* insn) {
Logan Chiend6c239a2011-12-23 15:11:45 +0800335
336 // Set the IRBuilder insertion point
337 irb_.SetInsertPoint(GetBasicBlock(dex_pc));
338
Logan Chien70f94b42011-12-27 17:49:11 +0800339#define ARGS dex_pc, insn
340
341 // Dispatch the instruction
342 switch (insn->Opcode()) {
343 case Instruction::NOP:
344 EmitInsn_Nop(ARGS);
345 break;
346
347 case Instruction::MOVE:
348 case Instruction::MOVE_FROM16:
349 case Instruction::MOVE_16:
350 EmitInsn_Move(ARGS, kInt);
351 break;
352
353 case Instruction::MOVE_WIDE:
354 case Instruction::MOVE_WIDE_FROM16:
355 case Instruction::MOVE_WIDE_16:
356 EmitInsn_Move(ARGS, kLong);
357 break;
358
359 case Instruction::MOVE_OBJECT:
360 case Instruction::MOVE_OBJECT_FROM16:
361 case Instruction::MOVE_OBJECT_16:
362 EmitInsn_Move(ARGS, kObject);
363 break;
364
365 case Instruction::MOVE_RESULT:
366 EmitInsn_MoveResult(ARGS, kInt);
367 break;
368
369 case Instruction::MOVE_RESULT_WIDE:
370 EmitInsn_MoveResult(ARGS, kLong);
371 break;
372
373 case Instruction::MOVE_RESULT_OBJECT:
374 EmitInsn_MoveResult(ARGS, kObject);
375 break;
376
377 case Instruction::MOVE_EXCEPTION:
378 EmitInsn_MoveException(ARGS);
379 break;
380
381 case Instruction::RETURN_VOID:
382 EmitInsn_ReturnVoid(ARGS);
383 break;
384
385 case Instruction::RETURN:
386 case Instruction::RETURN_WIDE:
387 case Instruction::RETURN_OBJECT:
388 EmitInsn_Return(ARGS);
389 break;
390
391 case Instruction::CONST_4:
392 case Instruction::CONST_16:
393 case Instruction::CONST:
394 case Instruction::CONST_HIGH16:
395 EmitInsn_LoadConstant(ARGS, kInt);
396 break;
397
398 case Instruction::CONST_WIDE_16:
399 case Instruction::CONST_WIDE_32:
400 case Instruction::CONST_WIDE:
401 case Instruction::CONST_WIDE_HIGH16:
402 EmitInsn_LoadConstant(ARGS, kLong);
403 break;
404
405 case Instruction::CONST_STRING:
406 case Instruction::CONST_STRING_JUMBO:
407 EmitInsn_LoadConstantString(ARGS);
408 break;
409
410 case Instruction::CONST_CLASS:
411 EmitInsn_LoadConstantClass(ARGS);
412 break;
413
414 case Instruction::MONITOR_ENTER:
415 EmitInsn_MonitorEnter(ARGS);
416 break;
417
418 case Instruction::MONITOR_EXIT:
419 EmitInsn_MonitorExit(ARGS);
420 break;
421
422 case Instruction::CHECK_CAST:
423 EmitInsn_CheckCast(ARGS);
424 break;
425
426 case Instruction::INSTANCE_OF:
427 EmitInsn_InstanceOf(ARGS);
428 break;
429
430 case Instruction::ARRAY_LENGTH:
431 EmitInsn_ArrayLength(ARGS);
432 break;
433
434 case Instruction::NEW_INSTANCE:
435 EmitInsn_NewInstance(ARGS);
436 break;
437
438 case Instruction::NEW_ARRAY:
439 EmitInsn_NewArray(ARGS);
440 break;
441
442 case Instruction::FILLED_NEW_ARRAY:
443 EmitInsn_FilledNewArray(ARGS, false);
444 break;
445
446 case Instruction::FILLED_NEW_ARRAY_RANGE:
447 EmitInsn_FilledNewArray(ARGS, true);
448 break;
449
450 case Instruction::FILL_ARRAY_DATA:
451 EmitInsn_FillArrayData(ARGS);
452 break;
453
454 case Instruction::THROW:
455 EmitInsn_ThrowException(ARGS);
456 break;
457
458 case Instruction::GOTO:
459 case Instruction::GOTO_16:
460 case Instruction::GOTO_32:
461 EmitInsn_UnconditionalBranch(ARGS);
462 break;
463
464 case Instruction::PACKED_SWITCH:
465 EmitInsn_PackedSwitch(ARGS);
466 break;
467
468 case Instruction::SPARSE_SWITCH:
469 EmitInsn_SparseSwitch(ARGS);
470 break;
471
472 case Instruction::CMPL_FLOAT:
473 EmitInsn_FPCompare(ARGS, kFloat, false);
474 break;
475
476 case Instruction::CMPG_FLOAT:
477 EmitInsn_FPCompare(ARGS, kFloat, true);
478 break;
479
480 case Instruction::CMPL_DOUBLE:
481 EmitInsn_FPCompare(ARGS, kDouble, false);
482 break;
483
484 case Instruction::CMPG_DOUBLE:
485 EmitInsn_FPCompare(ARGS, kDouble, true);
486 break;
487
488 case Instruction::CMP_LONG:
489 EmitInsn_LongCompare(ARGS);
490 break;
491
492 case Instruction::IF_EQ:
493 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_EQ);
494 break;
495
496 case Instruction::IF_NE:
497 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_NE);
498 break;
499
500 case Instruction::IF_LT:
501 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_LT);
502 break;
503
504 case Instruction::IF_GE:
505 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_GE);
506 break;
507
508 case Instruction::IF_GT:
509 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_GT);
510 break;
511
512 case Instruction::IF_LE:
513 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_LE);
514 break;
515
516 case Instruction::IF_EQZ:
517 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_EQ);
518 break;
519
520 case Instruction::IF_NEZ:
521 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_NE);
522 break;
523
524 case Instruction::IF_LTZ:
525 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_LT);
526 break;
527
528 case Instruction::IF_GEZ:
529 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_GE);
530 break;
531
532 case Instruction::IF_GTZ:
533 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_GT);
534 break;
535
536 case Instruction::IF_LEZ:
537 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_LE);
538 break;
539
540 case Instruction::AGET:
541 EmitInsn_AGet(ARGS, kInt);
542 break;
543
544 case Instruction::AGET_WIDE:
545 EmitInsn_AGet(ARGS, kLong);
546 break;
547
548 case Instruction::AGET_OBJECT:
549 EmitInsn_AGet(ARGS, kObject);
550 break;
551
552 case Instruction::AGET_BOOLEAN:
553 EmitInsn_AGet(ARGS, kBoolean);
554 break;
555
556 case Instruction::AGET_BYTE:
557 EmitInsn_AGet(ARGS, kByte);
558 break;
559
560 case Instruction::AGET_CHAR:
561 EmitInsn_AGet(ARGS, kChar);
562 break;
563
564 case Instruction::AGET_SHORT:
565 EmitInsn_AGet(ARGS, kShort);
566 break;
567
568 case Instruction::APUT:
569 EmitInsn_APut(ARGS, kInt);
570 break;
571
572 case Instruction::APUT_WIDE:
573 EmitInsn_APut(ARGS, kLong);
574 break;
575
576 case Instruction::APUT_OBJECT:
577 EmitInsn_APut(ARGS, kObject);
578 break;
579
580 case Instruction::APUT_BOOLEAN:
581 EmitInsn_APut(ARGS, kBoolean);
582 break;
583
584 case Instruction::APUT_BYTE:
585 EmitInsn_APut(ARGS, kByte);
586 break;
587
588 case Instruction::APUT_CHAR:
589 EmitInsn_APut(ARGS, kChar);
590 break;
591
592 case Instruction::APUT_SHORT:
593 EmitInsn_APut(ARGS, kShort);
594 break;
595
596 case Instruction::IGET:
597 EmitInsn_IGet(ARGS, kInt);
598 break;
599
600 case Instruction::IGET_WIDE:
601 EmitInsn_IGet(ARGS, kLong);
602 break;
603
604 case Instruction::IGET_OBJECT:
605 EmitInsn_IGet(ARGS, kObject);
606 break;
607
608 case Instruction::IGET_BOOLEAN:
609 EmitInsn_IGet(ARGS, kBoolean);
610 break;
611
612 case Instruction::IGET_BYTE:
613 EmitInsn_IGet(ARGS, kByte);
614 break;
615
616 case Instruction::IGET_CHAR:
617 EmitInsn_IGet(ARGS, kChar);
618 break;
619
620 case Instruction::IGET_SHORT:
621 EmitInsn_IGet(ARGS, kShort);
622 break;
623
624 case Instruction::IPUT:
625 EmitInsn_IPut(ARGS, kInt);
626 break;
627
628 case Instruction::IPUT_WIDE:
629 EmitInsn_IPut(ARGS, kLong);
630 break;
631
632 case Instruction::IPUT_OBJECT:
633 EmitInsn_IPut(ARGS, kObject);
634 break;
635
636 case Instruction::IPUT_BOOLEAN:
637 EmitInsn_IPut(ARGS, kBoolean);
638 break;
639
640 case Instruction::IPUT_BYTE:
641 EmitInsn_IPut(ARGS, kByte);
642 break;
643
644 case Instruction::IPUT_CHAR:
645 EmitInsn_IPut(ARGS, kChar);
646 break;
647
648 case Instruction::IPUT_SHORT:
649 EmitInsn_IPut(ARGS, kShort);
650 break;
651
652 case Instruction::SGET:
653 EmitInsn_SGet(ARGS, kInt);
654 break;
655
656 case Instruction::SGET_WIDE:
657 EmitInsn_SGet(ARGS, kLong);
658 break;
659
660 case Instruction::SGET_OBJECT:
661 EmitInsn_SGet(ARGS, kObject);
662 break;
663
664 case Instruction::SGET_BOOLEAN:
665 EmitInsn_SGet(ARGS, kBoolean);
666 break;
667
668 case Instruction::SGET_BYTE:
669 EmitInsn_SGet(ARGS, kByte);
670 break;
671
672 case Instruction::SGET_CHAR:
673 EmitInsn_SGet(ARGS, kChar);
674 break;
675
676 case Instruction::SGET_SHORT:
677 EmitInsn_SGet(ARGS, kShort);
678 break;
679
680 case Instruction::SPUT:
681 EmitInsn_SPut(ARGS, kInt);
682 break;
683
684 case Instruction::SPUT_WIDE:
685 EmitInsn_SPut(ARGS, kLong);
686 break;
687
688 case Instruction::SPUT_OBJECT:
689 EmitInsn_SPut(ARGS, kObject);
690 break;
691
692 case Instruction::SPUT_BOOLEAN:
693 EmitInsn_SPut(ARGS, kBoolean);
694 break;
695
696 case Instruction::SPUT_BYTE:
697 EmitInsn_SPut(ARGS, kByte);
698 break;
699
700 case Instruction::SPUT_CHAR:
701 EmitInsn_SPut(ARGS, kChar);
702 break;
703
704 case Instruction::SPUT_SHORT:
705 EmitInsn_SPut(ARGS, kShort);
706 break;
707
708
709 case Instruction::INVOKE_VIRTUAL:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800710 EmitInsn_Invoke(ARGS, kVirtual, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800711 break;
712
713 case Instruction::INVOKE_SUPER:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800714 EmitInsn_Invoke(ARGS, kSuper, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800715 break;
716
717 case Instruction::INVOKE_DIRECT:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800718 EmitInsn_Invoke(ARGS, kDirect, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800719 break;
720
721 case Instruction::INVOKE_STATIC:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800722 EmitInsn_Invoke(ARGS, kStatic, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800723 break;
724
725 case Instruction::INVOKE_INTERFACE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800726 EmitInsn_Invoke(ARGS, kInterface, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800727 break;
728
729 case Instruction::INVOKE_VIRTUAL_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800730 EmitInsn_Invoke(ARGS, kVirtual, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800731 break;
732
733 case Instruction::INVOKE_SUPER_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800734 EmitInsn_Invoke(ARGS, kSuper, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800735 break;
736
737 case Instruction::INVOKE_DIRECT_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800738 EmitInsn_Invoke(ARGS, kDirect, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800739 break;
740
741 case Instruction::INVOKE_STATIC_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800742 EmitInsn_Invoke(ARGS, kStatic, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800743 break;
744
745 case Instruction::INVOKE_INTERFACE_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800746 EmitInsn_Invoke(ARGS, kInterface, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800747 break;
748
749 case Instruction::NEG_INT:
750 EmitInsn_Neg(ARGS, kInt);
751 break;
752
753 case Instruction::NOT_INT:
754 EmitInsn_Not(ARGS, kInt);
755 break;
756
757 case Instruction::NEG_LONG:
758 EmitInsn_Neg(ARGS, kLong);
759 break;
760
761 case Instruction::NOT_LONG:
762 EmitInsn_Not(ARGS, kLong);
763 break;
764
765 case Instruction::NEG_FLOAT:
766 EmitInsn_FNeg(ARGS, kFloat);
767 break;
768
769 case Instruction::NEG_DOUBLE:
770 EmitInsn_FNeg(ARGS, kDouble);
771 break;
772
773 case Instruction::INT_TO_LONG:
774 EmitInsn_SExt(ARGS);
775 break;
776
777 case Instruction::INT_TO_FLOAT:
778 EmitInsn_IntToFP(ARGS, kInt, kFloat);
779 break;
780
781 case Instruction::INT_TO_DOUBLE:
782 EmitInsn_IntToFP(ARGS, kInt, kDouble);
783 break;
784
785 case Instruction::LONG_TO_INT:
786 EmitInsn_Trunc(ARGS);
787 break;
788
789 case Instruction::LONG_TO_FLOAT:
790 EmitInsn_IntToFP(ARGS, kLong, kFloat);
791 break;
792
793 case Instruction::LONG_TO_DOUBLE:
794 EmitInsn_IntToFP(ARGS, kLong, kDouble);
795 break;
796
797 case Instruction::FLOAT_TO_INT:
798 EmitInsn_FPToInt(ARGS, kFloat, kInt);
799 break;
800
801 case Instruction::FLOAT_TO_LONG:
802 EmitInsn_FPToInt(ARGS, kFloat, kLong);
803 break;
804
805 case Instruction::FLOAT_TO_DOUBLE:
806 EmitInsn_FExt(ARGS);
807 break;
808
809 case Instruction::DOUBLE_TO_INT:
810 EmitInsn_FPToInt(ARGS, kDouble, kInt);
811 break;
812
813 case Instruction::DOUBLE_TO_LONG:
814 EmitInsn_FPToInt(ARGS, kDouble, kLong);
815 break;
816
817 case Instruction::DOUBLE_TO_FLOAT:
818 EmitInsn_FTrunc(ARGS);
819 break;
820
821 case Instruction::INT_TO_BYTE:
822 EmitInsn_TruncAndSExt(ARGS, 8);
823 break;
824
825 case Instruction::INT_TO_CHAR:
826 EmitInsn_TruncAndZExt(ARGS, 16);
827 break;
828
829 case Instruction::INT_TO_SHORT:
830 EmitInsn_TruncAndSExt(ARGS, 16);
831 break;
832
833 case Instruction::ADD_INT:
834 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kInt, false);
835 break;
836
837 case Instruction::SUB_INT:
838 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kInt, false);
839 break;
840
841 case Instruction::MUL_INT:
842 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kInt, false);
843 break;
844
845 case Instruction::DIV_INT:
846 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kInt, false);
847 break;
848
849 case Instruction::REM_INT:
850 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kInt, false);
851 break;
852
853 case Instruction::AND_INT:
854 EmitInsn_IntArithm(ARGS, kIntArithm_And, kInt, false);
855 break;
856
857 case Instruction::OR_INT:
858 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kInt, false);
859 break;
860
861 case Instruction::XOR_INT:
862 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kInt, false);
863 break;
864
865 case Instruction::SHL_INT:
Logan Chien5539ad02012-04-02 14:36:55 +0800866 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kInt, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800867 break;
868
869 case Instruction::SHR_INT:
Logan Chien5539ad02012-04-02 14:36:55 +0800870 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kInt, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800871 break;
872
873 case Instruction::USHR_INT:
Logan Chien5539ad02012-04-02 14:36:55 +0800874 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kInt, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800875 break;
876
877 case Instruction::ADD_LONG:
878 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kLong, false);
879 break;
880
881 case Instruction::SUB_LONG:
882 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kLong, false);
883 break;
884
885 case Instruction::MUL_LONG:
886 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kLong, false);
887 break;
888
889 case Instruction::DIV_LONG:
890 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kLong, false);
891 break;
892
893 case Instruction::REM_LONG:
894 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kLong, false);
895 break;
896
897 case Instruction::AND_LONG:
898 EmitInsn_IntArithm(ARGS, kIntArithm_And, kLong, false);
899 break;
900
901 case Instruction::OR_LONG:
902 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kLong, false);
903 break;
904
905 case Instruction::XOR_LONG:
906 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kLong, false);
907 break;
908
909 case Instruction::SHL_LONG:
Logan Chien5539ad02012-04-02 14:36:55 +0800910 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kLong, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800911 break;
912
913 case Instruction::SHR_LONG:
Logan Chien5539ad02012-04-02 14:36:55 +0800914 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kLong, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800915 break;
916
917 case Instruction::USHR_LONG:
Logan Chien5539ad02012-04-02 14:36:55 +0800918 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kLong, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800919 break;
920
921 case Instruction::ADD_FLOAT:
922 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kFloat, false);
923 break;
924
925 case Instruction::SUB_FLOAT:
926 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kFloat, false);
927 break;
928
929 case Instruction::MUL_FLOAT:
930 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kFloat, false);
931 break;
932
933 case Instruction::DIV_FLOAT:
934 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kFloat, false);
935 break;
936
937 case Instruction::REM_FLOAT:
938 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kFloat, false);
939 break;
940
941 case Instruction::ADD_DOUBLE:
942 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kDouble, false);
943 break;
944
945 case Instruction::SUB_DOUBLE:
946 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kDouble, false);
947 break;
948
949 case Instruction::MUL_DOUBLE:
950 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kDouble, false);
951 break;
952
953 case Instruction::DIV_DOUBLE:
954 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kDouble, false);
955 break;
956
957 case Instruction::REM_DOUBLE:
958 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kDouble, false);
959 break;
960
961 case Instruction::ADD_INT_2ADDR:
962 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kInt, true);
963 break;
964
965 case Instruction::SUB_INT_2ADDR:
966 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kInt, true);
967 break;
968
969 case Instruction::MUL_INT_2ADDR:
970 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kInt, true);
971 break;
972
973 case Instruction::DIV_INT_2ADDR:
974 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kInt, true);
975 break;
976
977 case Instruction::REM_INT_2ADDR:
978 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kInt, true);
979 break;
980
981 case Instruction::AND_INT_2ADDR:
982 EmitInsn_IntArithm(ARGS, kIntArithm_And, kInt, true);
983 break;
984
985 case Instruction::OR_INT_2ADDR:
986 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kInt, true);
987 break;
988
989 case Instruction::XOR_INT_2ADDR:
990 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kInt, true);
991 break;
992
993 case Instruction::SHL_INT_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +0800994 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kInt, true);
Logan Chien70f94b42011-12-27 17:49:11 +0800995 break;
996
997 case Instruction::SHR_INT_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +0800998 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kInt, true);
Logan Chien70f94b42011-12-27 17:49:11 +0800999 break;
1000
1001 case Instruction::USHR_INT_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001002 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kInt, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001003 break;
1004
1005 case Instruction::ADD_LONG_2ADDR:
1006 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kLong, true);
1007 break;
1008
1009 case Instruction::SUB_LONG_2ADDR:
1010 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kLong, true);
1011 break;
1012
1013 case Instruction::MUL_LONG_2ADDR:
1014 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kLong, true);
1015 break;
1016
1017 case Instruction::DIV_LONG_2ADDR:
1018 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kLong, true);
1019 break;
1020
1021 case Instruction::REM_LONG_2ADDR:
1022 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kLong, true);
1023 break;
1024
1025 case Instruction::AND_LONG_2ADDR:
1026 EmitInsn_IntArithm(ARGS, kIntArithm_And, kLong, true);
1027 break;
1028
1029 case Instruction::OR_LONG_2ADDR:
1030 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kLong, true);
1031 break;
1032
1033 case Instruction::XOR_LONG_2ADDR:
1034 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kLong, true);
1035 break;
1036
1037 case Instruction::SHL_LONG_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001038 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kLong, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001039 break;
1040
1041 case Instruction::SHR_LONG_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001042 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kLong, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001043 break;
1044
1045 case Instruction::USHR_LONG_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001046 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kLong, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001047 break;
1048
1049 case Instruction::ADD_FLOAT_2ADDR:
1050 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kFloat, true);
1051 break;
1052
1053 case Instruction::SUB_FLOAT_2ADDR:
1054 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kFloat, true);
1055 break;
1056
1057 case Instruction::MUL_FLOAT_2ADDR:
1058 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kFloat, true);
1059 break;
1060
1061 case Instruction::DIV_FLOAT_2ADDR:
1062 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kFloat, true);
1063 break;
1064
1065 case Instruction::REM_FLOAT_2ADDR:
1066 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kFloat, true);
1067 break;
1068
1069 case Instruction::ADD_DOUBLE_2ADDR:
1070 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kDouble, true);
1071 break;
1072
1073 case Instruction::SUB_DOUBLE_2ADDR:
1074 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kDouble, true);
1075 break;
1076
1077 case Instruction::MUL_DOUBLE_2ADDR:
1078 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kDouble, true);
1079 break;
1080
1081 case Instruction::DIV_DOUBLE_2ADDR:
1082 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kDouble, true);
1083 break;
1084
1085 case Instruction::REM_DOUBLE_2ADDR:
1086 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kDouble, true);
1087 break;
1088
1089 case Instruction::ADD_INT_LIT16:
1090 case Instruction::ADD_INT_LIT8:
1091 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Add);
1092 break;
1093
1094 case Instruction::RSUB_INT:
1095 case Instruction::RSUB_INT_LIT8:
1096 EmitInsn_RSubImmediate(ARGS);
1097 break;
1098
1099 case Instruction::MUL_INT_LIT16:
1100 case Instruction::MUL_INT_LIT8:
1101 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Mul);
1102 break;
1103
1104 case Instruction::DIV_INT_LIT16:
1105 case Instruction::DIV_INT_LIT8:
1106 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Div);
1107 break;
1108
1109 case Instruction::REM_INT_LIT16:
1110 case Instruction::REM_INT_LIT8:
1111 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Rem);
1112 break;
1113
1114 case Instruction::AND_INT_LIT16:
1115 case Instruction::AND_INT_LIT8:
1116 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_And);
1117 break;
1118
1119 case Instruction::OR_INT_LIT16:
1120 case Instruction::OR_INT_LIT8:
1121 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Or);
1122 break;
1123
1124 case Instruction::XOR_INT_LIT16:
1125 case Instruction::XOR_INT_LIT8:
1126 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Xor);
1127 break;
1128
1129 case Instruction::SHL_INT_LIT8:
Logan Chien5539ad02012-04-02 14:36:55 +08001130 EmitInsn_IntShiftArithmImmediate(ARGS, kIntArithm_Shl);
Logan Chien70f94b42011-12-27 17:49:11 +08001131 break;
1132
1133 case Instruction::SHR_INT_LIT8:
Logan Chien5539ad02012-04-02 14:36:55 +08001134 EmitInsn_IntShiftArithmImmediate(ARGS, kIntArithm_Shr);
Logan Chien70f94b42011-12-27 17:49:11 +08001135 break;
1136
1137 case Instruction::USHR_INT_LIT8:
Logan Chien5539ad02012-04-02 14:36:55 +08001138 EmitInsn_IntShiftArithmImmediate(ARGS, kIntArithm_UShr);
Logan Chien70f94b42011-12-27 17:49:11 +08001139 break;
1140
Logan Chien9e5f5c12012-04-10 13:51:45 +08001141 case Instruction::THROW_VERIFICATION_ERROR:
1142 EmitInsn_ThrowVerificationError(ARGS);
1143 break;
1144
Logan Chien70f94b42011-12-27 17:49:11 +08001145 case Instruction::UNUSED_3E:
1146 case Instruction::UNUSED_3F:
1147 case Instruction::UNUSED_40:
1148 case Instruction::UNUSED_41:
1149 case Instruction::UNUSED_42:
1150 case Instruction::UNUSED_43:
1151 case Instruction::UNUSED_73:
1152 case Instruction::UNUSED_79:
1153 case Instruction::UNUSED_7A:
1154 case Instruction::UNUSED_E3:
1155 case Instruction::UNUSED_E4:
1156 case Instruction::UNUSED_E5:
1157 case Instruction::UNUSED_E6:
1158 case Instruction::UNUSED_E7:
1159 case Instruction::UNUSED_E8:
1160 case Instruction::UNUSED_E9:
1161 case Instruction::UNUSED_EA:
1162 case Instruction::UNUSED_EB:
1163 case Instruction::UNUSED_EC:
Logan Chien70f94b42011-12-27 17:49:11 +08001164 case Instruction::UNUSED_EE:
1165 case Instruction::UNUSED_EF:
1166 case Instruction::UNUSED_F0:
1167 case Instruction::UNUSED_F1:
1168 case Instruction::UNUSED_F2:
1169 case Instruction::UNUSED_F3:
1170 case Instruction::UNUSED_F4:
1171 case Instruction::UNUSED_F5:
1172 case Instruction::UNUSED_F6:
1173 case Instruction::UNUSED_F7:
1174 case Instruction::UNUSED_F8:
1175 case Instruction::UNUSED_F9:
1176 case Instruction::UNUSED_FA:
1177 case Instruction::UNUSED_FB:
1178 case Instruction::UNUSED_FC:
1179 case Instruction::UNUSED_FD:
1180 case Instruction::UNUSED_FE:
1181 case Instruction::UNUSED_FF:
1182 LOG(FATAL) << "Dex file contains UNUSED bytecode: " << insn->Opcode();
1183 break;
1184 }
1185
1186#undef ARGS
1187}
1188
1189
1190void MethodCompiler::EmitInsn_Nop(uint32_t dex_pc,
1191 Instruction const* insn) {
Logan Chiene09a6b72011-12-27 17:50:21 +08001192
1193 uint16_t insn_signature = code_item_->insns_[dex_pc];
1194
1195 if (insn_signature == Instruction::kPackedSwitchSignature ||
1196 insn_signature == Instruction::kSparseSwitchSignature ||
1197 insn_signature == Instruction::kArrayDataSignature) {
1198 irb_.CreateUnreachable();
Elliott Hughesb25c3f62012-03-26 16:35:06 -07001199 } else {
Logan Chiene09a6b72011-12-27 17:50:21 +08001200 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1201 }
Shih-wei Liaod1fec812012-02-13 09:51:10 -08001202}
1203
1204
Logan Chien70f94b42011-12-27 17:49:11 +08001205void MethodCompiler::EmitInsn_Move(uint32_t dex_pc,
1206 Instruction const* insn,
1207 JType jty) {
Logan Chien48173132011-12-27 17:51:13 +08001208
Elliott Hughesadb8c672012-03-06 16:49:32 -08001209 DecodedInstruction dec_insn(insn);
Logan Chien48173132011-12-27 17:51:13 +08001210
Elliott Hughesadb8c672012-03-06 16:49:32 -08001211 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, jty, kReg);
1212 EmitStoreDalvikReg(dec_insn.vA, jty, kReg, src_value);
Logan Chien48173132011-12-27 17:51:13 +08001213
Logan Chien70f94b42011-12-27 17:49:11 +08001214 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1215}
1216
1217
1218void MethodCompiler::EmitInsn_MoveResult(uint32_t dex_pc,
1219 Instruction const* insn,
1220 JType jty) {
Logan Chien48173132011-12-27 17:51:13 +08001221
Elliott Hughesadb8c672012-03-06 16:49:32 -08001222 DecodedInstruction dec_insn(insn);
Logan Chien48173132011-12-27 17:51:13 +08001223
1224 llvm::Value* src_value = EmitLoadDalvikRetValReg(jty, kReg);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001225 EmitStoreDalvikReg(dec_insn.vA, jty, kReg, src_value);
Logan Chien48173132011-12-27 17:51:13 +08001226
Logan Chien70f94b42011-12-27 17:49:11 +08001227 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1228}
1229
1230
1231void MethodCompiler::EmitInsn_MoveException(uint32_t dex_pc,
1232 Instruction const* insn) {
Logan Chien3354cec2012-01-13 14:29:03 +08001233
Elliott Hughesadb8c672012-03-06 16:49:32 -08001234 DecodedInstruction dec_insn(insn);
Logan Chien3354cec2012-01-13 14:29:03 +08001235
1236 // Get thread-local exception field address
1237 llvm::Constant* exception_field_offset =
1238 irb_.getPtrEquivInt(Thread::ExceptionOffset().Int32Value());
1239
1240 llvm::Value* thread_object_addr =
1241 irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1242
1243 llvm::Value* exception_field_addr =
1244 irb_.CreatePtrDisp(thread_object_addr, exception_field_offset,
1245 irb_.getJObjectTy()->getPointerTo());
1246
1247 // Get exception object address
1248 llvm::Value* exception_object_addr = irb_.CreateLoad(exception_field_addr);
1249
1250 // Set thread-local exception field address to NULL
1251 irb_.CreateStore(irb_.getJNull(), exception_field_addr);
1252
1253 // Keep the exception object in the Dalvik register
Elliott Hughesadb8c672012-03-06 16:49:32 -08001254 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, exception_object_addr);
Logan Chien3354cec2012-01-13 14:29:03 +08001255
Logan Chien70f94b42011-12-27 17:49:11 +08001256 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1257}
1258
1259
1260void MethodCompiler::EmitInsn_ThrowException(uint32_t dex_pc,
1261 Instruction const* insn) {
Logan Chien6c6f12d2012-01-13 19:26:27 +08001262
Elliott Hughesadb8c672012-03-06 16:49:32 -08001263 DecodedInstruction dec_insn(insn);
Logan Chien6c6f12d2012-01-13 19:26:27 +08001264
1265 llvm::Value* exception_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001266 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chien6c6f12d2012-01-13 19:26:27 +08001267
Logan Chien8dfcbea2012-02-17 18:50:32 +08001268 EmitUpdateLineNumFromDexPC(dex_pc);
1269
Logan Chien6c6f12d2012-01-13 19:26:27 +08001270 irb_.CreateCall(irb_.GetRuntime(ThrowException), exception_addr);
1271
1272 EmitBranchExceptionLandingPad(dex_pc);
Logan Chien70f94b42011-12-27 17:49:11 +08001273}
1274
1275
Logan Chien9e5f5c12012-04-10 13:51:45 +08001276void MethodCompiler::EmitInsn_ThrowVerificationError(uint32_t dex_pc,
1277 Instruction const* insn) {
1278
1279 DecodedInstruction dec_insn(insn);
1280
1281 EmitUpdateLineNumFromDexPC(dex_pc);
1282
1283 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1284 llvm::Value* kind_value = irb_.getInt32(dec_insn.vA);
1285 llvm::Value* ref_value = irb_.getInt32(dec_insn.vB);
1286
1287 irb_.CreateCall3(irb_.GetRuntime(ThrowVerificationError),
1288 method_object_addr, kind_value, ref_value);
1289
1290 EmitBranchExceptionLandingPad(dex_pc);
1291}
1292
1293
Logan Chien70f94b42011-12-27 17:49:11 +08001294void MethodCompiler::EmitInsn_ReturnVoid(uint32_t dex_pc,
1295 Instruction const* insn) {
Logan Chien8898a272011-12-27 17:51:56 +08001296 // Garbage collection safe-point
1297 EmitGuard_GarbageCollectionSuspend(dex_pc);
1298
Logan Chien8dfcbea2012-02-17 18:50:32 +08001299 // Pop the shadow frame
1300 EmitPopShadowFrame();
1301
Logan Chien8898a272011-12-27 17:51:56 +08001302 // Return!
1303 irb_.CreateRetVoid();
Logan Chien70f94b42011-12-27 17:49:11 +08001304}
1305
1306
1307void MethodCompiler::EmitInsn_Return(uint32_t dex_pc,
1308 Instruction const* insn) {
Logan Chien8898a272011-12-27 17:51:56 +08001309
Elliott Hughesadb8c672012-03-06 16:49:32 -08001310 DecodedInstruction dec_insn(insn);
Logan Chien8898a272011-12-27 17:51:56 +08001311
1312 // 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 // NOTE: It is important to keep this AFTER the GC safe-point. Otherwise,
1318 // the return value might be collected since the shadow stack is popped.
1319
Logan Chien8898a272011-12-27 17:51:56 +08001320 // Return!
Logan Chiendd361c92012-04-10 23:40:37 +08001321 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
Elliott Hughesadb8c672012-03-06 16:49:32 -08001322 llvm::Value* retval = EmitLoadDalvikReg(dec_insn.vA, ret_shorty, kAccurate);
Logan Chien8898a272011-12-27 17:51:56 +08001323
1324 irb_.CreateRet(retval);
Logan Chien70f94b42011-12-27 17:49:11 +08001325}
1326
1327
1328void MethodCompiler::EmitInsn_LoadConstant(uint32_t dex_pc,
1329 Instruction const* insn,
1330 JType imm_jty) {
Shih-wei Liao798366e2012-02-16 09:25:33 -08001331
Elliott Hughesadb8c672012-03-06 16:49:32 -08001332 DecodedInstruction dec_insn(insn);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001333
1334 DCHECK(imm_jty == kInt || imm_jty == kLong) << imm_jty;
1335
1336 int64_t imm = 0;
1337
1338 switch (insn->Opcode()) {
1339 // 32-bit Immediate
1340 case Instruction::CONST_4:
1341 case Instruction::CONST_16:
1342 case Instruction::CONST:
1343 case Instruction::CONST_WIDE_16:
1344 case Instruction::CONST_WIDE_32:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001345 imm = static_cast<int64_t>(static_cast<int32_t>(dec_insn.vB));
Shih-wei Liao798366e2012-02-16 09:25:33 -08001346 break;
1347
1348 case Instruction::CONST_HIGH16:
1349 imm = static_cast<int64_t>(static_cast<int32_t>(
Elliott Hughesadb8c672012-03-06 16:49:32 -08001350 static_cast<uint32_t>(static_cast<uint16_t>(dec_insn.vB)) << 16));
Shih-wei Liao798366e2012-02-16 09:25:33 -08001351 break;
1352
1353 // 64-bit Immediate
1354 case Instruction::CONST_WIDE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001355 imm = static_cast<int64_t>(dec_insn.vB_wide);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001356 break;
1357
1358 case Instruction::CONST_WIDE_HIGH16:
1359 imm = static_cast<int64_t>(
Elliott Hughesadb8c672012-03-06 16:49:32 -08001360 static_cast<uint64_t>(static_cast<uint16_t>(dec_insn.vB)) << 48);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001361 break;
1362
1363 // Unknown opcode for load constant (unreachable)
1364 default:
1365 LOG(FATAL) << "Unknown opcode for load constant: " << insn->Opcode();
1366 break;
1367 }
1368
1369 // Store the non-object register
1370 llvm::Type* imm_type = irb_.getJType(imm_jty, kAccurate);
1371 llvm::Constant* imm_value = llvm::ConstantInt::getSigned(imm_type, imm);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001372 EmitStoreDalvikReg(dec_insn.vA, imm_jty, kAccurate, imm_value);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001373
1374 // Store the object register if it is possible to be null.
1375 if (imm_jty == kInt && imm == 0) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001376 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, irb_.getJNull());
Shih-wei Liao798366e2012-02-16 09:25:33 -08001377 }
1378
Logan Chien70f94b42011-12-27 17:49:11 +08001379 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1380}
1381
1382
1383void MethodCompiler::EmitInsn_LoadConstantString(uint32_t dex_pc,
1384 Instruction const* insn) {
Logan Chienc3b4ba12012-01-16 19:52:53 +08001385
Elliott Hughesadb8c672012-03-06 16:49:32 -08001386 DecodedInstruction dec_insn(insn);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001387
Elliott Hughesadb8c672012-03-06 16:49:32 -08001388 uint32_t string_idx = dec_insn.vB;
Logan Chienc3b4ba12012-01-16 19:52:53 +08001389
1390 llvm::Value* string_field_addr = EmitLoadDexCacheStringFieldAddr(string_idx);
1391
1392 llvm::Value* string_addr = irb_.CreateLoad(string_field_addr);
1393
1394 if (!compiler_->CanAssumeStringIsPresentInDexCache(dex_cache_, string_idx)) {
1395 llvm::BasicBlock* block_str_exist =
1396 CreateBasicBlockWithDexPC(dex_pc, "str_exist");
1397
1398 llvm::BasicBlock* block_str_resolve =
1399 CreateBasicBlockWithDexPC(dex_pc, "str_resolve");
1400
1401 // Test: Is the string resolved and in the dex cache?
1402 llvm::Value* equal_null = irb_.CreateICmpEQ(string_addr, irb_.getJNull());
1403
TDYa127a849cb62012-04-01 05:59:34 -07001404 irb_.CreateCondBr(equal_null, block_str_resolve, block_str_exist);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001405
1406 // String is resolved, go to next basic block.
1407 irb_.SetInsertPoint(block_str_exist);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001408 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, string_addr);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001409 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1410
1411 // String is not resolved yet, resolve it now.
1412 irb_.SetInsertPoint(block_str_resolve);
1413
1414 llvm::Function* runtime_func = irb_.GetRuntime(ResolveString);
1415
1416 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1417
1418 llvm::Value* string_idx_value = irb_.getInt32(string_idx);
1419
Logan Chien8dfcbea2012-02-17 18:50:32 +08001420 EmitUpdateLineNumFromDexPC(dex_pc);
1421
1422 string_addr = irb_.CreateCall2(runtime_func, method_object_addr,
1423 string_idx_value);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001424
1425 EmitGuard_ExceptionLandingPad(dex_pc);
1426 }
1427
1428 // Store the string object to the Dalvik register
Elliott Hughesadb8c672012-03-06 16:49:32 -08001429 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, string_addr);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001430
Logan Chien70f94b42011-12-27 17:49:11 +08001431 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1432}
1433
1434
Logan Chien27b30252012-01-14 03:43:35 +08001435llvm::Value* MethodCompiler::EmitLoadConstantClass(uint32_t dex_pc,
1436 uint32_t type_idx) {
1437 if (!compiler_->CanAccessTypeWithoutChecks(method_idx_, dex_cache_,
1438 *dex_file_, type_idx)) {
1439 llvm::Value* type_idx_value = irb_.getInt32(type_idx);
1440
1441 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1442
1443 llvm::Function* runtime_func =
1444 irb_.GetRuntime(InitializeTypeAndVerifyAccess);
1445
Logan Chien8dfcbea2012-02-17 18:50:32 +08001446 EmitUpdateLineNumFromDexPC(dex_pc);
1447
Logan Chien27b30252012-01-14 03:43:35 +08001448 llvm::Value* type_object_addr =
1449 irb_.CreateCall2(runtime_func, type_idx_value, method_object_addr);
1450
1451 EmitGuard_ExceptionLandingPad(dex_pc);
1452
1453 return type_object_addr;
1454
1455 } else {
1456 // Try to load the class (type) object from the test cache.
1457 llvm::Value* type_field_addr =
1458 EmitLoadDexCacheResolvedTypeFieldAddr(type_idx);
1459
1460 llvm::Value* type_object_addr = irb_.CreateLoad(type_field_addr);
1461
1462 if (compiler_->CanAssumeTypeIsPresentInDexCache(dex_cache_, type_idx)) {
1463 return type_object_addr;
1464 }
1465
1466 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
1467
1468 // Test whether class (type) object is in the dex cache or not
1469 llvm::Value* equal_null =
1470 irb_.CreateICmpEQ(type_object_addr, irb_.getJNull());
1471
1472 llvm::BasicBlock* block_cont =
1473 CreateBasicBlockWithDexPC(dex_pc, "cont");
1474
1475 llvm::BasicBlock* block_load_class =
1476 CreateBasicBlockWithDexPC(dex_pc, "load_class");
1477
1478 irb_.CreateCondBr(equal_null, block_load_class, block_cont);
1479
1480 // Failback routine to load the class object
1481 irb_.SetInsertPoint(block_load_class);
1482
1483 llvm::Function* runtime_func = irb_.GetRuntime(InitializeType);
1484
1485 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
1486
1487 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1488
Logan Chien8dfcbea2012-02-17 18:50:32 +08001489 EmitUpdateLineNumFromDexPC(dex_pc);
1490
Logan Chien27b30252012-01-14 03:43:35 +08001491 llvm::Value* loaded_type_object_addr =
1492 irb_.CreateCall2(runtime_func, type_idx_value, method_object_addr);
1493
1494 EmitGuard_ExceptionLandingPad(dex_pc);
1495
1496 llvm::BasicBlock* block_after_load_class = irb_.GetInsertBlock();
1497
1498 irb_.CreateBr(block_cont);
1499
1500 // Now the class object must be loaded
1501 irb_.SetInsertPoint(block_cont);
1502
1503 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1504
1505 phi->addIncoming(type_object_addr, block_original);
1506 phi->addIncoming(loaded_type_object_addr, block_after_load_class);
1507
1508 return phi;
1509 }
1510}
1511
1512
Logan Chien70f94b42011-12-27 17:49:11 +08001513void MethodCompiler::EmitInsn_LoadConstantClass(uint32_t dex_pc,
1514 Instruction const* insn) {
Logan Chien27b30252012-01-14 03:43:35 +08001515
Elliott Hughesadb8c672012-03-06 16:49:32 -08001516 DecodedInstruction dec_insn(insn);
Logan Chien27b30252012-01-14 03:43:35 +08001517
Elliott Hughesadb8c672012-03-06 16:49:32 -08001518 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vB);
1519 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, type_object_addr);
Logan Chien27b30252012-01-14 03:43:35 +08001520
Logan Chien70f94b42011-12-27 17:49:11 +08001521 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1522}
1523
1524
1525void MethodCompiler::EmitInsn_MonitorEnter(uint32_t dex_pc,
1526 Instruction const* insn) {
Logan Chien9e0dbe42012-01-13 12:11:37 +08001527
Elliott Hughesadb8c672012-03-06 16:49:32 -08001528 DecodedInstruction dec_insn(insn);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001529
1530 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001531 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001532
1533 // TODO: Slow path always. May not need NullPointerException check.
1534 EmitGuard_NullPointerException(dex_pc, object_addr);
1535
Logan Chien8dfcbea2012-02-17 18:50:32 +08001536 EmitUpdateLineNumFromDexPC(dex_pc);
1537
Logan Chien9e0dbe42012-01-13 12:11:37 +08001538 irb_.CreateCall(irb_.GetRuntime(LockObject), object_addr);
1539 EmitGuard_ExceptionLandingPad(dex_pc);
1540
Logan Chien70f94b42011-12-27 17:49:11 +08001541 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1542}
1543
1544
1545void MethodCompiler::EmitInsn_MonitorExit(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
1553 EmitGuard_NullPointerException(dex_pc, object_addr);
1554
Logan Chien8dfcbea2012-02-17 18:50:32 +08001555 EmitUpdateLineNumFromDexPC(dex_pc);
1556
Logan Chien9e0dbe42012-01-13 12:11:37 +08001557 irb_.CreateCall(irb_.GetRuntime(UnlockObject), object_addr);
1558 EmitGuard_ExceptionLandingPad(dex_pc);
1559
Logan Chien70f94b42011-12-27 17:49:11 +08001560 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1561}
1562
1563
1564void MethodCompiler::EmitInsn_CheckCast(uint32_t dex_pc,
1565 Instruction const* insn) {
Logan Chienfc880952012-01-15 23:53:10 +08001566
Elliott Hughesadb8c672012-03-06 16:49:32 -08001567 DecodedInstruction dec_insn(insn);
Logan Chienfc880952012-01-15 23:53:10 +08001568
1569 llvm::BasicBlock* block_test_class =
1570 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1571
1572 llvm::BasicBlock* block_test_sub_class =
1573 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
1574
1575 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001576 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chienfc880952012-01-15 23:53:10 +08001577
1578 // Test: Is the reference equal to null? Act as no-op when it is null.
1579 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
1580
1581 irb_.CreateCondBr(equal_null,
1582 GetNextBasicBlock(dex_pc),
1583 block_test_class);
1584
1585 // Test: Is the object instantiated from the given class?
1586 irb_.SetInsertPoint(block_test_class);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001587 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vB);
Logan Chienfc880952012-01-15 23:53:10 +08001588 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1589
1590 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
1591
1592 llvm::Value* object_type_field_addr =
1593 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
1594
1595 llvm::Value* object_type_object_addr =
1596 irb_.CreateLoad(object_type_field_addr);
1597
1598 llvm::Value* equal_class =
1599 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
1600
1601 irb_.CreateCondBr(equal_class,
1602 GetNextBasicBlock(dex_pc),
1603 block_test_sub_class);
1604
1605 // Test: Is the object instantiated from the subclass of the given class?
1606 irb_.SetInsertPoint(block_test_sub_class);
1607
Logan Chien8dfcbea2012-02-17 18:50:32 +08001608 EmitUpdateLineNumFromDexPC(dex_pc);
1609
Logan Chienfc880952012-01-15 23:53:10 +08001610 irb_.CreateCall2(irb_.GetRuntime(CheckCast),
1611 type_object_addr, object_type_object_addr);
1612
1613 EmitGuard_ExceptionLandingPad(dex_pc);
1614
Logan Chien70f94b42011-12-27 17:49:11 +08001615 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1616}
1617
1618
1619void MethodCompiler::EmitInsn_InstanceOf(uint32_t dex_pc,
1620 Instruction const* insn) {
Logan Chien68725e22012-01-15 22:25:34 +08001621
Elliott Hughesadb8c672012-03-06 16:49:32 -08001622 DecodedInstruction dec_insn(insn);
Logan Chien68725e22012-01-15 22:25:34 +08001623
1624 llvm::Constant* zero = irb_.getJInt(0);
1625 llvm::Constant* one = irb_.getJInt(1);
1626
1627 llvm::BasicBlock* block_nullp = CreateBasicBlockWithDexPC(dex_pc, "nullp");
1628
1629 llvm::BasicBlock* block_test_class =
1630 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1631
1632 llvm::BasicBlock* block_class_equals =
1633 CreateBasicBlockWithDexPC(dex_pc, "class_eq");
1634
1635 llvm::BasicBlock* block_test_sub_class =
1636 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
1637
1638 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001639 EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
Logan Chien68725e22012-01-15 22:25:34 +08001640
1641 // Overview of the following code :
1642 // We check for null, if so, then false, otherwise check for class == . If so
1643 // then true, otherwise do callout slowpath.
1644 //
1645 // Test: Is the reference equal to null? Set 0 when it is null.
1646 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
1647
1648 irb_.CreateCondBr(equal_null, block_nullp, block_test_class);
1649
1650 irb_.SetInsertPoint(block_nullp);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001651 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, zero);
Logan Chien68725e22012-01-15 22:25:34 +08001652 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1653
1654 // Test: Is the object instantiated from the given class?
1655 irb_.SetInsertPoint(block_test_class);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001656 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vC);
Logan Chien68725e22012-01-15 22:25:34 +08001657 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1658
1659 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
1660
1661 llvm::Value* object_type_field_addr =
1662 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
1663
1664 llvm::Value* object_type_object_addr =
1665 irb_.CreateLoad(object_type_field_addr);
1666
1667 llvm::Value* equal_class =
1668 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
1669
1670 irb_.CreateCondBr(equal_class, block_class_equals, block_test_sub_class);
1671
1672 irb_.SetInsertPoint(block_class_equals);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001673 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, one);
Logan Chien68725e22012-01-15 22:25:34 +08001674 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1675
1676 // Test: Is the object instantiated from the subclass of the given class?
1677 irb_.SetInsertPoint(block_test_sub_class);
1678
1679 llvm::Value* result =
1680 irb_.CreateCall2(irb_.GetRuntime(IsAssignable),
1681 type_object_addr, object_type_object_addr);
1682
Elliott Hughesadb8c672012-03-06 16:49:32 -08001683 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result);
Logan Chien68725e22012-01-15 22:25:34 +08001684
Logan Chien70f94b42011-12-27 17:49:11 +08001685 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1686}
1687
1688
Logan Chien61bb6142012-02-03 15:34:53 +08001689llvm::Value* MethodCompiler::EmitLoadArrayLength(llvm::Value* array) {
1690 // Load array length field address
1691 llvm::Constant* array_len_field_offset =
1692 irb_.getPtrEquivInt(Array::LengthOffset().Int32Value());
1693
1694 llvm::Value* array_len_field_addr =
1695 irb_.CreatePtrDisp(array, array_len_field_offset,
1696 irb_.getJIntTy()->getPointerTo());
1697
1698 // Load array length
1699 return irb_.CreateLoad(array_len_field_addr);
1700}
1701
1702
Logan Chien70f94b42011-12-27 17:49:11 +08001703void MethodCompiler::EmitInsn_ArrayLength(uint32_t dex_pc,
1704 Instruction const* insn) {
Logan Chien61bb6142012-02-03 15:34:53 +08001705
Elliott Hughesadb8c672012-03-06 16:49:32 -08001706 DecodedInstruction dec_insn(insn);
Logan Chien61bb6142012-02-03 15:34:53 +08001707
1708 // Get the array object address
Elliott Hughesadb8c672012-03-06 16:49:32 -08001709 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
Logan Chien61bb6142012-02-03 15:34:53 +08001710 EmitGuard_NullPointerException(dex_pc, array_addr);
1711
1712 // Get the array length and store it to the register
1713 llvm::Value* array_len = EmitLoadArrayLength(array_addr);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001714 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, array_len);
Logan Chien61bb6142012-02-03 15:34:53 +08001715
Logan Chien70f94b42011-12-27 17:49:11 +08001716 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1717}
1718
1719
1720void MethodCompiler::EmitInsn_NewInstance(uint32_t dex_pc,
1721 Instruction const* insn) {
Logan Chien032bdad2012-01-16 09:59:23 +08001722
Elliott Hughesadb8c672012-03-06 16:49:32 -08001723 DecodedInstruction dec_insn(insn);
Logan Chien032bdad2012-01-16 09:59:23 +08001724
1725 llvm::Function* runtime_func;
1726 if (compiler_->CanAccessTypeWithoutChecks(method_idx_, dex_cache_,
Elliott Hughesadb8c672012-03-06 16:49:32 -08001727 *dex_file_, dec_insn.vB)) {
Logan Chien032bdad2012-01-16 09:59:23 +08001728 runtime_func = irb_.GetRuntime(AllocObject);
1729 } else {
1730 runtime_func = irb_.GetRuntime(AllocObjectWithAccessCheck);
1731 }
1732
Elliott Hughesadb8c672012-03-06 16:49:32 -08001733 llvm::Constant* type_index_value = irb_.getInt32(dec_insn.vB);
Logan Chien032bdad2012-01-16 09:59:23 +08001734
1735 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1736
Logan Chien8dfcbea2012-02-17 18:50:32 +08001737 EmitUpdateLineNumFromDexPC(dex_pc);
1738
Logan Chien032bdad2012-01-16 09:59:23 +08001739 llvm::Value* object_addr =
1740 irb_.CreateCall2(runtime_func, type_index_value, method_object_addr);
1741
1742 EmitGuard_ExceptionLandingPad(dex_pc);
1743
Elliott Hughesadb8c672012-03-06 16:49:32 -08001744 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, object_addr);
Logan Chien032bdad2012-01-16 09:59:23 +08001745
Logan Chien70f94b42011-12-27 17:49:11 +08001746 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1747}
1748
1749
Logan Chiena2cc6a32012-01-16 10:38:41 +08001750llvm::Value* MethodCompiler::EmitAllocNewArray(uint32_t dex_pc,
1751 int32_t length,
1752 uint32_t type_idx,
1753 bool is_filled_new_array) {
1754 llvm::Function* runtime_func;
1755
1756 bool skip_access_check =
1757 compiler_->CanAccessTypeWithoutChecks(method_idx_, dex_cache_,
1758 *dex_file_, type_idx);
1759
TDYa127a849cb62012-04-01 05:59:34 -07001760 llvm::Value* array_length_value;
1761
Logan Chiena2cc6a32012-01-16 10:38:41 +08001762 if (is_filled_new_array) {
1763 runtime_func = skip_access_check ?
1764 irb_.GetRuntime(CheckAndAllocArray) :
1765 irb_.GetRuntime(CheckAndAllocArrayWithAccessCheck);
TDYa127a849cb62012-04-01 05:59:34 -07001766 array_length_value = irb_.getInt32(length);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001767 } else {
1768 runtime_func = skip_access_check ?
1769 irb_.GetRuntime(AllocArray) :
1770 irb_.GetRuntime(AllocArrayWithAccessCheck);
TDYa127a849cb62012-04-01 05:59:34 -07001771 array_length_value = EmitLoadDalvikReg(length, kInt, kAccurate);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001772 }
1773
1774 llvm::Constant* type_index_value = irb_.getInt32(type_idx);
1775
1776 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1777
Logan Chien8dfcbea2012-02-17 18:50:32 +08001778 EmitUpdateLineNumFromDexPC(dex_pc);
1779
Logan Chiena2cc6a32012-01-16 10:38:41 +08001780 llvm::Value* object_addr =
1781 irb_.CreateCall3(runtime_func, type_index_value, method_object_addr,
1782 array_length_value);
1783
1784 EmitGuard_ExceptionLandingPad(dex_pc);
1785
1786 return object_addr;
1787}
1788
1789
Logan Chien70f94b42011-12-27 17:49:11 +08001790void MethodCompiler::EmitInsn_NewArray(uint32_t dex_pc,
1791 Instruction const* insn) {
Logan Chiena2cc6a32012-01-16 10:38:41 +08001792
Elliott Hughesadb8c672012-03-06 16:49:32 -08001793 DecodedInstruction dec_insn(insn);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001794
1795 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001796 EmitAllocNewArray(dex_pc, dec_insn.vB, dec_insn.vC, false);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001797
Elliott Hughesadb8c672012-03-06 16:49:32 -08001798 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, object_addr);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001799
Logan Chien70f94b42011-12-27 17:49:11 +08001800 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1801}
1802
1803
1804void MethodCompiler::EmitInsn_FilledNewArray(uint32_t dex_pc,
1805 Instruction const* insn,
1806 bool is_range) {
Logan Chiena85fb2f2012-01-16 12:52:56 +08001807
Elliott Hughesadb8c672012-03-06 16:49:32 -08001808 DecodedInstruction dec_insn(insn);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001809
1810 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001811 EmitAllocNewArray(dex_pc, dec_insn.vA, dec_insn.vB, true);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001812
Elliott Hughesadb8c672012-03-06 16:49:32 -08001813 if (dec_insn.vA > 0) {
Logan Chiendd361c92012-04-10 23:40:37 +08001814 // Resolve the element type
1815 Class* klass = dex_cache_->GetResolvedType(dec_insn.vB);
1816 // TODO: Avoid the usage of the dex_cache_. Try to figure out a better
1817 // way to distinguish [I and [L.
Logan Chiena85fb2f2012-01-16 12:52:56 +08001818 CHECK_NE(klass, static_cast<Class*>(NULL));
Logan Chiena85fb2f2012-01-16 12:52:56 +08001819
Logan Chiendd361c92012-04-10 23:40:37 +08001820 uint32_t alignment;
1821 llvm::Constant* elem_size;
1822 llvm::PointerType* field_type;
Logan Chiena85fb2f2012-01-16 12:52:56 +08001823
Logan Chiendd361c92012-04-10 23:40:37 +08001824 // NOTE: Currently filled-new-array only supports 'L', '[', and 'I'
1825 // as the element, thus we are only checking 2 cases: primitive int and
1826 // non-primitive type.
Logan Chiena85fb2f2012-01-16 12:52:56 +08001827 if (klass->IsPrimitiveInt()) {
Logan Chiendd361c92012-04-10 23:40:37 +08001828 alignment = sizeof(int32_t);
1829 elem_size = irb_.getPtrEquivInt(sizeof(int32_t));
Logan Chiena85fb2f2012-01-16 12:52:56 +08001830 field_type = irb_.getJIntTy()->getPointerTo();
1831 } else {
1832 CHECK(!klass->IsPrimitive());
Logan Chiendd361c92012-04-10 23:40:37 +08001833 alignment = irb_.getSizeOfPtrEquivInt();
1834 elem_size = irb_.getSizeOfPtrEquivIntValue();
Logan Chiena85fb2f2012-01-16 12:52:56 +08001835 field_type = irb_.getJObjectTy()->getPointerTo();
1836 }
1837
Logan Chiendd361c92012-04-10 23:40:37 +08001838 llvm::Value* data_field_offset =
1839 irb_.getPtrEquivInt(Array::DataOffset(alignment).Int32Value());
1840
1841 llvm::Value* data_field_addr =
1842 irb_.CreatePtrDisp(object_addr, data_field_offset, field_type);
1843
Logan Chiena85fb2f2012-01-16 12:52:56 +08001844 // TODO: Tune this code. Currently we are generating one instruction for
1845 // one element which may be very space consuming. Maybe changing to use
1846 // memcpy may help; however, since we can't guarantee that the alloca of
1847 // dalvik register are continuous, we can't perform such optimization yet.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001848 for (uint32_t i = 0; i < dec_insn.vA; ++i) {
Logan Chiena85fb2f2012-01-16 12:52:56 +08001849 int reg_index;
1850 if (is_range) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001851 reg_index = dec_insn.vC + i;
Logan Chiena85fb2f2012-01-16 12:52:56 +08001852 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001853 reg_index = dec_insn.arg[i];
Logan Chiena85fb2f2012-01-16 12:52:56 +08001854 }
1855
1856 llvm::Value* reg_value;
1857 if (klass->IsPrimitiveInt()) {
1858 reg_value = EmitLoadDalvikReg(reg_index, kInt, kAccurate);
1859 } else {
1860 reg_value = EmitLoadDalvikReg(reg_index, kObject, kAccurate);
1861 }
1862
1863 irb_.CreateStore(reg_value, data_field_addr);
1864
Logan Chiendd361c92012-04-10 23:40:37 +08001865 data_field_addr =
1866 irb_.CreatePtrDisp(data_field_addr, elem_size, field_type);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001867 }
1868 }
1869
1870 EmitStoreDalvikRetValReg(kObject, kAccurate, object_addr);
1871
Logan Chien70f94b42011-12-27 17:49:11 +08001872 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1873}
1874
1875
1876void MethodCompiler::EmitInsn_FillArrayData(uint32_t dex_pc,
1877 Instruction const* insn) {
Logan Chiene58b6582012-01-16 17:13:13 +08001878
Elliott Hughesadb8c672012-03-06 16:49:32 -08001879 DecodedInstruction dec_insn(insn);
Logan Chiene58b6582012-01-16 17:13:13 +08001880
1881 // Read the payload
1882 struct PACKED Payload {
1883 uint16_t ident_;
1884 uint16_t elem_width_;
1885 uint32_t num_elems_;
1886 uint8_t data_[];
1887 };
1888
1889 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
Elliott Hughesadb8c672012-03-06 16:49:32 -08001890 static_cast<int32_t>(dec_insn.vB);
Logan Chiene58b6582012-01-16 17:13:13 +08001891
1892 Payload const* payload =
1893 reinterpret_cast<Payload const*>(code_item_->insns_ + payload_offset);
1894
1895 uint32_t size_in_bytes = payload->elem_width_ * payload->num_elems_;
1896
1897 // Load and check the array
Elliott Hughesadb8c672012-03-06 16:49:32 -08001898 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiene58b6582012-01-16 17:13:13 +08001899
1900 EmitGuard_NullPointerException(dex_pc, array_addr);
1901
1902 if (payload->num_elems_ > 0) {
1903 // Test: Is array length big enough?
1904 llvm::Constant* last_index = irb_.getJInt(payload->num_elems_ - 1);
1905
1906 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array_addr, last_index);
1907
1908 // Get array data field
1909 llvm::Value* data_field_offset_value =
TDYa127b77799d2012-04-06 22:16:31 -07001910 irb_.getPtrEquivInt(Array::DataOffset(payload->elem_width_).Int32Value());
Logan Chiene58b6582012-01-16 17:13:13 +08001911
1912 llvm::Value* data_field_addr =
1913 irb_.CreatePtrDisp(array_addr, data_field_offset_value,
1914 irb_.getInt8Ty()->getPointerTo());
1915
1916 // Emit payload to bitcode constant pool
1917 std::vector<llvm::Constant*> const_pool_data;
1918 for (uint32_t i = 0; i < size_in_bytes; ++i) {
1919 const_pool_data.push_back(irb_.getInt8(payload->data_[i]));
1920 }
1921
1922 llvm::Constant* const_pool_data_array_value = llvm::ConstantArray::get(
1923 llvm::ArrayType::get(irb_.getInt8Ty(), size_in_bytes), const_pool_data);
1924
1925 llvm::Value* const_pool_data_array_addr =
1926 new llvm::GlobalVariable(*module_,
1927 const_pool_data_array_value->getType(),
1928 false, llvm::GlobalVariable::InternalLinkage,
1929 const_pool_data_array_value,
1930 "array_data_payload");
1931
1932 // Find the memcpy intrinsic
1933 llvm::Type* memcpy_arg_types[] = {
1934 llvm::Type::getInt8Ty(*context_)->getPointerTo(),
1935 llvm::Type::getInt8Ty(*context_)->getPointerTo(),
1936 llvm::Type::getInt32Ty(*context_)
1937 };
1938
1939 llvm::Function* memcpy_intrinsic =
1940 llvm::Intrinsic::getDeclaration(module_,
1941 llvm::Intrinsic::memcpy,
1942 memcpy_arg_types);
1943
1944 // Copy now!
1945 llvm::Value *args[] = {
1946 data_field_addr,
1947 irb_.CreateConstGEP2_32(const_pool_data_array_addr, 0, 0),
1948 irb_.getInt32(size_in_bytes),
1949 irb_.getInt32(0), // alignment: no guarantee
1950 irb_.getFalse() // is_volatile: false
1951 };
1952
1953 irb_.CreateCall(memcpy_intrinsic, args);
1954 }
1955
Logan Chien70f94b42011-12-27 17:49:11 +08001956 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1957}
1958
1959
1960void MethodCompiler::EmitInsn_UnconditionalBranch(uint32_t dex_pc,
1961 Instruction const* insn) {
Logan Chiena466c162011-12-27 17:55:46 +08001962
Elliott Hughesadb8c672012-03-06 16:49:32 -08001963 DecodedInstruction dec_insn(insn);
Logan Chiena466c162011-12-27 17:55:46 +08001964
Elliott Hughesadb8c672012-03-06 16:49:32 -08001965 int32_t branch_offset = dec_insn.vA;
Logan Chiena466c162011-12-27 17:55:46 +08001966
1967 if (branch_offset <= 0) {
1968 // Garbage collection safe-point on backward branch
1969 EmitGuard_GarbageCollectionSuspend(dex_pc);
1970 }
1971
1972 irb_.CreateBr(GetBasicBlock(dex_pc + branch_offset));
Logan Chien70f94b42011-12-27 17:49:11 +08001973}
1974
1975
1976void MethodCompiler::EmitInsn_PackedSwitch(uint32_t dex_pc,
1977 Instruction const* insn) {
Logan Chien7a89b6d2011-12-27 17:56:56 +08001978
Elliott Hughesadb8c672012-03-06 16:49:32 -08001979 DecodedInstruction dec_insn(insn);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001980
1981 struct PACKED Payload {
1982 uint16_t ident_;
1983 uint16_t num_cases_;
1984 int32_t first_key_;
1985 int32_t targets_[];
1986 };
1987
1988 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
Elliott Hughesadb8c672012-03-06 16:49:32 -08001989 static_cast<int32_t>(dec_insn.vB);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001990
1991 Payload const* payload =
1992 reinterpret_cast<Payload const*>(code_item_->insns_ + payload_offset);
1993
Elliott Hughesadb8c672012-03-06 16:49:32 -08001994 llvm::Value* value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001995
1996 llvm::SwitchInst* sw =
1997 irb_.CreateSwitch(value, GetNextBasicBlock(dex_pc), payload->num_cases_);
1998
1999 for (uint16_t i = 0; i < payload->num_cases_; ++i) {
2000 sw->addCase(irb_.getInt32(payload->first_key_ + i),
2001 GetBasicBlock(dex_pc + payload->targets_[i]));
2002 }
Logan Chien70f94b42011-12-27 17:49:11 +08002003}
2004
2005
2006void MethodCompiler::EmitInsn_SparseSwitch(uint32_t dex_pc,
2007 Instruction const* insn) {
Logan Chien7a89b6d2011-12-27 17:56:56 +08002008
Elliott Hughesadb8c672012-03-06 16:49:32 -08002009 DecodedInstruction dec_insn(insn);
Logan Chien7a89b6d2011-12-27 17:56:56 +08002010
2011 struct PACKED Payload {
2012 uint16_t ident_;
2013 uint16_t num_cases_;
2014 int32_t keys_and_targets_[];
2015 };
2016
2017 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
Elliott Hughesadb8c672012-03-06 16:49:32 -08002018 static_cast<int32_t>(dec_insn.vB);
Logan Chien7a89b6d2011-12-27 17:56:56 +08002019
2020 Payload const* payload =
2021 reinterpret_cast<Payload const*>(code_item_->insns_ + payload_offset);
2022
2023 int32_t const* keys = payload->keys_and_targets_;
2024 int32_t const* targets = payload->keys_and_targets_ + payload->num_cases_;
2025
Elliott Hughesadb8c672012-03-06 16:49:32 -08002026 llvm::Value* value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chien7a89b6d2011-12-27 17:56:56 +08002027
2028 llvm::SwitchInst* sw =
2029 irb_.CreateSwitch(value, GetNextBasicBlock(dex_pc), payload->num_cases_);
2030
2031 for (size_t i = 0; i < payload->num_cases_; ++i) {
2032 sw->addCase(irb_.getInt32(keys[i]), GetBasicBlock(dex_pc + targets[i]));
2033 }
Logan Chien70f94b42011-12-27 17:49:11 +08002034}
2035
2036
2037void MethodCompiler::EmitInsn_FPCompare(uint32_t dex_pc,
2038 Instruction const* insn,
2039 JType fp_jty,
2040 bool gt_bias) {
Logan Chien2c37e8e2011-12-27 17:58:46 +08002041
Elliott Hughesadb8c672012-03-06 16:49:32 -08002042 DecodedInstruction dec_insn(insn);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002043
2044 DCHECK(fp_jty == kFloat || fp_jty == kDouble) << "JType: " << fp_jty;
2045
Elliott Hughesadb8c672012-03-06 16:49:32 -08002046 llvm::Value* src1_value = EmitLoadDalvikReg(dec_insn.vB, fp_jty, kAccurate);
2047 llvm::Value* src2_value = EmitLoadDalvikReg(dec_insn.vC, fp_jty, kAccurate);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002048
2049 llvm::Value* cmp_eq = irb_.CreateFCmpOEQ(src1_value, src2_value);
2050 llvm::Value* cmp_lt;
2051
2052 if (gt_bias) {
2053 cmp_lt = irb_.CreateFCmpOLT(src1_value, src2_value);
2054 } else {
2055 cmp_lt = irb_.CreateFCmpULT(src1_value, src2_value);
2056 }
2057
2058 llvm::Value* result = EmitCompareResultSelection(cmp_eq, cmp_lt);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002059 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002060
Logan Chien70f94b42011-12-27 17:49:11 +08002061 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2062}
2063
2064
2065void MethodCompiler::EmitInsn_LongCompare(uint32_t dex_pc,
2066 Instruction const* insn) {
Logan Chien2c37e8e2011-12-27 17:58:46 +08002067
Elliott Hughesadb8c672012-03-06 16:49:32 -08002068 DecodedInstruction dec_insn(insn);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002069
Elliott Hughesadb8c672012-03-06 16:49:32 -08002070 llvm::Value* src1_value = EmitLoadDalvikReg(dec_insn.vB, kLong, kAccurate);
2071 llvm::Value* src2_value = EmitLoadDalvikReg(dec_insn.vC, kLong, kAccurate);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002072
2073 llvm::Value* cmp_eq = irb_.CreateICmpEQ(src1_value, src2_value);
2074 llvm::Value* cmp_lt = irb_.CreateICmpSLT(src1_value, src2_value);
2075
2076 llvm::Value* result = EmitCompareResultSelection(cmp_eq, cmp_lt);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002077 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002078
Logan Chien70f94b42011-12-27 17:49:11 +08002079 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2080}
2081
2082
Logan Chien2c37e8e2011-12-27 17:58:46 +08002083llvm::Value* MethodCompiler::EmitCompareResultSelection(llvm::Value* cmp_eq,
2084 llvm::Value* cmp_lt) {
2085
2086 llvm::Constant* zero = irb_.getJInt(0);
2087 llvm::Constant* pos1 = irb_.getJInt(1);
2088 llvm::Constant* neg1 = irb_.getJInt(-1);
2089
2090 llvm::Value* result_lt = irb_.CreateSelect(cmp_lt, neg1, pos1);
2091 llvm::Value* result_eq = irb_.CreateSelect(cmp_eq, zero, result_lt);
2092
2093 return result_eq;
2094}
2095
2096
Logan Chien70f94b42011-12-27 17:49:11 +08002097void MethodCompiler::EmitInsn_BinaryConditionalBranch(uint32_t dex_pc,
2098 Instruction const* insn,
2099 CondBranchKind cond) {
Logan Chiena78e3c82011-12-27 17:59:35 +08002100
Elliott Hughesadb8c672012-03-06 16:49:32 -08002101 DecodedInstruction dec_insn(insn);
Logan Chiena78e3c82011-12-27 17:59:35 +08002102
Elliott Hughesadb8c672012-03-06 16:49:32 -08002103 int8_t src1_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vA);
2104 int8_t src2_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vB);
Logan Chiena78e3c82011-12-27 17:59:35 +08002105
2106 DCHECK_NE(kRegUnknown, src1_reg_cat);
2107 DCHECK_NE(kRegUnknown, src2_reg_cat);
2108 DCHECK_NE(kRegCat2, src1_reg_cat);
2109 DCHECK_NE(kRegCat2, src2_reg_cat);
2110
Elliott Hughesadb8c672012-03-06 16:49:32 -08002111 int32_t branch_offset = dec_insn.vC;
Logan Chiena78e3c82011-12-27 17:59:35 +08002112
2113 if (branch_offset <= 0) {
2114 // Garbage collection safe-point on backward branch
2115 EmitGuard_GarbageCollectionSuspend(dex_pc);
2116 }
2117
2118 if (src1_reg_cat == kRegZero && src2_reg_cat == kRegZero) {
2119 irb_.CreateBr(GetBasicBlock(dex_pc + branch_offset));
2120 return;
2121 }
2122
2123 llvm::Value* src1_value;
2124 llvm::Value* src2_value;
2125
2126 if (src1_reg_cat != kRegZero && src2_reg_cat != kRegZero) {
2127 CHECK_EQ(src1_reg_cat, src2_reg_cat);
2128
2129 if (src1_reg_cat == kRegCat1nr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002130 src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
2131 src2_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002132 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002133 src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
2134 src2_value = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002135 }
2136 } else {
2137 DCHECK(src1_reg_cat == kRegZero ||
2138 src2_reg_cat == kRegZero);
2139
2140 if (src1_reg_cat == kRegZero) {
2141 if (src2_reg_cat == kRegCat1nr) {
2142 src1_value = irb_.getJInt(0);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002143 src2_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002144 } else {
2145 src1_value = irb_.getJNull();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002146 src2_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002147 }
2148 } else { // src2_reg_cat == kRegZero
2149 if (src2_reg_cat == kRegCat1nr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002150 src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002151 src2_value = irb_.getJInt(0);
2152 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002153 src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002154 src2_value = irb_.getJNull();
2155 }
2156 }
2157 }
2158
2159 llvm::Value* cond_value =
2160 EmitConditionResult(src1_value, src2_value, cond);
2161
2162 irb_.CreateCondBr(cond_value,
2163 GetBasicBlock(dex_pc + branch_offset),
2164 GetNextBasicBlock(dex_pc));
Logan Chien70f94b42011-12-27 17:49:11 +08002165}
2166
2167
2168void MethodCompiler::EmitInsn_UnaryConditionalBranch(uint32_t dex_pc,
2169 Instruction const* insn,
2170 CondBranchKind cond) {
Logan Chiena78e3c82011-12-27 17:59:35 +08002171
Elliott Hughesadb8c672012-03-06 16:49:32 -08002172 DecodedInstruction dec_insn(insn);
Logan Chiena78e3c82011-12-27 17:59:35 +08002173
Elliott Hughesadb8c672012-03-06 16:49:32 -08002174 int8_t src_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vA);
Logan Chiena78e3c82011-12-27 17:59:35 +08002175
2176 DCHECK_NE(kRegUnknown, src_reg_cat);
2177 DCHECK_NE(kRegCat2, src_reg_cat);
2178
Elliott Hughesadb8c672012-03-06 16:49:32 -08002179 int32_t branch_offset = dec_insn.vB;
Logan Chiena78e3c82011-12-27 17:59:35 +08002180
2181 if (branch_offset <= 0) {
2182 // Garbage collection safe-point on backward branch
2183 EmitGuard_GarbageCollectionSuspend(dex_pc);
2184 }
2185
2186 if (src_reg_cat == kRegZero) {
2187 irb_.CreateBr(GetBasicBlock(dex_pc + branch_offset));
2188 return;
2189 }
2190
2191 llvm::Value* src1_value;
2192 llvm::Value* src2_value;
2193
2194 if (src_reg_cat == kRegCat1nr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002195 src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002196 src2_value = irb_.getInt32(0);
2197 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002198 src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002199 src2_value = irb_.getJNull();
2200 }
2201
2202 llvm::Value* cond_value =
2203 EmitConditionResult(src1_value, src2_value, cond);
2204
2205 irb_.CreateCondBr(cond_value,
2206 GetBasicBlock(dex_pc + branch_offset),
2207 GetNextBasicBlock(dex_pc));
2208}
2209
2210
2211RegCategory MethodCompiler::GetInferredRegCategory(uint32_t dex_pc,
2212 uint16_t reg_idx) {
Logan Chiendd361c92012-04-10 23:40:37 +08002213
2214 Compiler::MethodReference mref(dex_file_, method_idx_);
2215
2216 InferredRegCategoryMap const* map =
2217 verifier::DexVerifier::GetInferredRegCategoryMap(mref);
2218
Logan Chiena78e3c82011-12-27 17:59:35 +08002219 CHECK_NE(map, static_cast<InferredRegCategoryMap*>(NULL));
2220
2221 return map->GetRegCategory(dex_pc, reg_idx);
2222}
2223
2224
2225llvm::Value* MethodCompiler::EmitConditionResult(llvm::Value* lhs,
2226 llvm::Value* rhs,
2227 CondBranchKind cond) {
2228 switch (cond) {
2229 case kCondBranch_EQ:
2230 return irb_.CreateICmpEQ(lhs, rhs);
2231
2232 case kCondBranch_NE:
2233 return irb_.CreateICmpNE(lhs, rhs);
2234
2235 case kCondBranch_LT:
2236 return irb_.CreateICmpSLT(lhs, rhs);
2237
2238 case kCondBranch_GE:
2239 return irb_.CreateICmpSGE(lhs, rhs);
2240
2241 case kCondBranch_GT:
2242 return irb_.CreateICmpSGT(lhs, rhs);
2243
2244 case kCondBranch_LE:
2245 return irb_.CreateICmpSLE(lhs, rhs);
2246
2247 default: // Unreachable
2248 LOG(FATAL) << "Unknown conditional branch kind: " << cond;
2249 return NULL;
2250 }
Logan Chien70f94b42011-12-27 17:49:11 +08002251}
2252
2253
Logan Chiene27fdbb2012-01-02 23:27:26 +08002254void
2255MethodCompiler::EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc,
2256 llvm::Value* array,
2257 llvm::Value* index) {
2258 llvm::Value* array_len = EmitLoadArrayLength(array);
2259
2260 llvm::Value* cmp = irb_.CreateICmpUGE(index, array_len);
2261
2262 llvm::BasicBlock* block_exception =
2263 CreateBasicBlockWithDexPC(dex_pc, "overflow");
2264
2265 llvm::BasicBlock* block_continue =
2266 CreateBasicBlockWithDexPC(dex_pc, "cont");
2267
2268 irb_.CreateCondBr(cmp, block_exception, block_continue);
2269
2270 irb_.SetInsertPoint(block_exception);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002271
2272 EmitUpdateLineNumFromDexPC(dex_pc);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002273 irb_.CreateCall2(irb_.GetRuntime(ThrowIndexOutOfBounds), index, array_len);
2274 EmitBranchExceptionLandingPad(dex_pc);
2275
2276 irb_.SetInsertPoint(block_continue);
2277}
2278
2279
2280void MethodCompiler::EmitGuard_ArrayException(uint32_t dex_pc,
2281 llvm::Value* array,
2282 llvm::Value* index) {
2283 EmitGuard_NullPointerException(dex_pc, array);
2284 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array, index);
2285}
2286
2287
2288// Emit Array GetElementPtr
2289llvm::Value* MethodCompiler::EmitArrayGEP(llvm::Value* array_addr,
2290 llvm::Value* index_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08002291 llvm::Type* elem_type,
2292 JType elem_jty) {
2293
2294 int data_offset;
2295 if (elem_jty == kLong || elem_jty == kDouble ||
2296 (elem_jty == kObject && sizeof(uint64_t) == sizeof(Object*))) {
2297 data_offset = Array::DataOffset(sizeof(int64_t)).Int32Value();
2298 } else {
2299 data_offset = Array::DataOffset(sizeof(int32_t)).Int32Value();
2300 }
Logan Chiene27fdbb2012-01-02 23:27:26 +08002301
2302 llvm::Constant* data_offset_value =
Ian Rogers04ec04e2012-02-28 16:15:33 -08002303 irb_.getPtrEquivInt(data_offset);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002304
2305 llvm::Value* array_data_addr =
2306 irb_.CreatePtrDisp(array_addr, data_offset_value,
2307 elem_type->getPointerTo());
2308
2309 return irb_.CreateGEP(array_data_addr, index_value);
2310}
2311
2312
Logan Chien70f94b42011-12-27 17:49:11 +08002313void MethodCompiler::EmitInsn_AGet(uint32_t dex_pc,
2314 Instruction const* insn,
2315 JType elem_jty) {
Logan Chiene27fdbb2012-01-02 23:27:26 +08002316
Elliott Hughesadb8c672012-03-06 16:49:32 -08002317 DecodedInstruction dec_insn(insn);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002318
Elliott Hughesadb8c672012-03-06 16:49:32 -08002319 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
2320 llvm::Value* index_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002321
2322 EmitGuard_ArrayException(dex_pc, array_addr, index_value);
2323
2324 llvm::Type* elem_type = irb_.getJType(elem_jty, kArray);
2325
2326 llvm::Value* array_elem_addr =
Ian Rogers04ec04e2012-02-28 16:15:33 -08002327 EmitArrayGEP(array_addr, index_value, elem_type, elem_jty);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002328
2329 llvm::Value* array_elem_value = irb_.CreateLoad(array_elem_addr);
2330
Elliott Hughesadb8c672012-03-06 16:49:32 -08002331 EmitStoreDalvikReg(dec_insn.vA, elem_jty, kArray, array_elem_value);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002332
Logan Chien70f94b42011-12-27 17:49:11 +08002333 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2334}
2335
2336
2337void MethodCompiler::EmitInsn_APut(uint32_t dex_pc,
2338 Instruction const* insn,
2339 JType elem_jty) {
Logan Chien8dabb432012-01-02 23:29:32 +08002340
Elliott Hughesadb8c672012-03-06 16:49:32 -08002341 DecodedInstruction dec_insn(insn);
Logan Chien8dabb432012-01-02 23:29:32 +08002342
Elliott Hughesadb8c672012-03-06 16:49:32 -08002343 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
2344 llvm::Value* index_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate);
Logan Chien8dabb432012-01-02 23:29:32 +08002345
2346 EmitGuard_ArrayException(dex_pc, array_addr, index_value);
2347
2348 llvm::Type* elem_type = irb_.getJType(elem_jty, kArray);
2349
2350 llvm::Value* array_elem_addr =
Ian Rogers04ec04e2012-02-28 16:15:33 -08002351 EmitArrayGEP(array_addr, index_value, elem_type, elem_jty);
Logan Chien8dabb432012-01-02 23:29:32 +08002352
Elliott Hughesadb8c672012-03-06 16:49:32 -08002353 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, elem_jty, kArray);
Logan Chien8dabb432012-01-02 23:29:32 +08002354
TDYa1271b86d072012-04-05 17:38:56 -07002355 if (elem_jty == kObject) { // If put an object, check the type.
2356 llvm::Function* runtime_func = irb_.GetRuntime(CheckPutArrayElement);
2357
2358 irb_.CreateCall2(runtime_func, new_value, array_addr);
2359
2360 EmitGuard_ExceptionLandingPad(dex_pc);
2361 }
2362
Logan Chien8dabb432012-01-02 23:29:32 +08002363 irb_.CreateStore(new_value, array_elem_addr);
2364
Logan Chien70f94b42011-12-27 17:49:11 +08002365 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2366}
2367
2368
Logan Chien48f1d2a2012-01-02 22:49:53 +08002369void MethodCompiler::PrintUnresolvedFieldWarning(int32_t field_idx) {
Logan Chiendd361c92012-04-10 23:40:37 +08002370 DexFile::FieldId const& field_id = dex_file_->GetFieldId(field_idx);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002371
2372 LOG(WARNING) << "unable to resolve static field " << field_idx << " ("
Logan Chiendd361c92012-04-10 23:40:37 +08002373 << dex_file_->GetFieldName(field_id) << ") in "
2374 << dex_file_->GetFieldDeclaringClassDescriptor(field_id);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002375}
2376
2377
Logan Chien70f94b42011-12-27 17:49:11 +08002378void MethodCompiler::EmitInsn_IGet(uint32_t dex_pc,
2379 Instruction const* insn,
2380 JType field_jty) {
Logan Chien48f1d2a2012-01-02 22:49:53 +08002381
Elliott Hughesadb8c672012-03-06 16:49:32 -08002382 DecodedInstruction dec_insn(insn);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002383
Elliott Hughesadb8c672012-03-06 16:49:32 -08002384 uint32_t reg_idx = dec_insn.vB;
2385 uint32_t field_idx = dec_insn.vC;
Logan Chien48f1d2a2012-01-02 22:49:53 +08002386
2387 Field* field = dex_cache_->GetResolvedField(field_idx);
2388
2389 llvm::Value* object_addr = EmitLoadDalvikReg(reg_idx, kObject, kAccurate);
2390
2391 EmitGuard_NullPointerException(dex_pc, object_addr);
2392
2393 llvm::Value* field_value;
2394
2395 if (field == NULL) {
2396 PrintUnresolvedFieldWarning(field_idx);
2397
2398 llvm::Function* runtime_func;
2399
2400 if (field_jty == kObject) {
Logan Chien3b2b2e72012-03-06 16:11:45 +08002401 runtime_func = irb_.GetRuntime(GetObjectInstance);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002402 } else if (field_jty == kLong || field_jty == kDouble) {
Logan Chien3b2b2e72012-03-06 16:11:45 +08002403 runtime_func = irb_.GetRuntime(Get64Instance);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002404 } else {
Logan Chien3b2b2e72012-03-06 16:11:45 +08002405 runtime_func = irb_.GetRuntime(Get32Instance);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002406 }
2407
2408 llvm::ConstantInt* field_idx_value = irb_.getInt32(field_idx);
2409
2410 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2411
Logan Chien8dfcbea2012-02-17 18:50:32 +08002412 EmitUpdateLineNumFromDexPC(dex_pc);
2413
Logan Chien3b2b2e72012-03-06 16:11:45 +08002414 field_value = irb_.CreateCall3(runtime_func, field_idx_value,
2415 method_object_addr, object_addr);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002416
2417 EmitGuard_ExceptionLandingPad(dex_pc);
2418
2419 } else {
2420 llvm::PointerType* field_type =
2421 irb_.getJType(field_jty, kField)->getPointerTo();
2422
2423 llvm::ConstantInt* field_offset =
2424 irb_.getPtrEquivInt(field->GetOffset().Int32Value());
2425
2426 llvm::Value* field_addr =
2427 irb_.CreatePtrDisp(object_addr, field_offset, field_type);
2428
2429 field_value = irb_.CreateLoad(field_addr);
2430 }
2431
Elliott Hughesadb8c672012-03-06 16:49:32 -08002432 EmitStoreDalvikReg(dec_insn.vA, field_jty, kField, field_value);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002433
Logan Chien70f94b42011-12-27 17:49:11 +08002434 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2435}
2436
2437
2438void MethodCompiler::EmitInsn_IPut(uint32_t dex_pc,
2439 Instruction const* insn,
2440 JType field_jty) {
Logan Chiendd6aa872012-01-03 16:06:32 +08002441
Elliott Hughesadb8c672012-03-06 16:49:32 -08002442 DecodedInstruction dec_insn(insn);
Logan Chiendd6aa872012-01-03 16:06:32 +08002443
Elliott Hughesadb8c672012-03-06 16:49:32 -08002444 uint32_t reg_idx = dec_insn.vB;
2445 uint32_t field_idx = dec_insn.vC;
Logan Chiendd6aa872012-01-03 16:06:32 +08002446
2447 Field* field = dex_cache_->GetResolvedField(field_idx);
2448
2449 llvm::Value* object_addr = EmitLoadDalvikReg(reg_idx, kObject, kAccurate);
2450
2451 EmitGuard_NullPointerException(dex_pc, object_addr);
2452
Elliott Hughesadb8c672012-03-06 16:49:32 -08002453 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, field_jty, kField);
Logan Chiendd6aa872012-01-03 16:06:32 +08002454
2455 if (field == NULL) {
2456 PrintUnresolvedFieldWarning(field_idx);
2457
2458 llvm::Function* runtime_func;
2459
2460 if (field_jty == kObject) {
2461 runtime_func = irb_.GetRuntime(SetObjectInstance);
2462 } else if (field_jty == kLong || field_jty == kDouble) {
2463 runtime_func = irb_.GetRuntime(Set64Instance);
2464 } else {
2465 runtime_func = irb_.GetRuntime(Set32Instance);
2466 }
2467
2468 llvm::Value* field_idx_value = irb_.getInt32(field_idx);
2469
2470 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2471
Logan Chien8dfcbea2012-02-17 18:50:32 +08002472 EmitUpdateLineNumFromDexPC(dex_pc);
2473
Logan Chien3b2b2e72012-03-06 16:11:45 +08002474 irb_.CreateCall4(runtime_func, field_idx_value,
2475 method_object_addr, object_addr, new_value);
Logan Chiendd6aa872012-01-03 16:06:32 +08002476
2477 EmitGuard_ExceptionLandingPad(dex_pc);
2478
2479 } else {
2480 llvm::PointerType* field_type =
2481 irb_.getJType(field_jty, kField)->getPointerTo();
2482
2483 llvm::Value* field_offset =
2484 irb_.getPtrEquivInt(field->GetOffset().Int32Value());
2485
2486 llvm::Value* field_addr =
2487 irb_.CreatePtrDisp(object_addr, field_offset, field_type);
2488
2489 irb_.CreateStore(new_value, field_addr);
2490 }
2491
Logan Chien70f94b42011-12-27 17:49:11 +08002492 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2493}
2494
2495
Logan Chien438c4b62012-01-17 16:06:00 +08002496Field* MethodCompiler::ResolveField(uint32_t field_idx) {
2497 Thread* thread = Thread::Current();
2498
2499 // Save the exception state
2500 Throwable* old_exception = NULL;
2501 if (thread->IsExceptionPending()) {
2502 old_exception = thread->GetException();
2503 thread->ClearException();
2504 }
2505
2506 // Resolve the fields through the class linker
2507 Field* field = class_linker_->ResolveField(*dex_file_, field_idx,
2508 dex_cache_, class_loader_,
2509 /* is_static= */ true);
2510
2511 if (field == NULL) {
2512 // Ignore the exception raised during the field resolution
2513 thread->ClearException();
2514 }
2515
2516 // Restore the exception state
2517 if (old_exception != NULL) {
2518 thread->SetException(old_exception);
2519 }
2520
2521 return field;
2522}
2523
2524
2525Field* MethodCompiler::
2526FindFieldAndDeclaringTypeIdx(uint32_t field_idx,
2527 uint32_t &resolved_type_idx) {
2528
2529 // Resolve the field through the class linker
2530 Field* field = ResolveField(field_idx);
2531 if (field == NULL) {
2532 return NULL;
2533 }
2534
2535 DexFile::FieldId const& field_id = dex_file_->GetFieldId(field_idx);
2536
2537 // Search for the type_idx of the declaring class
2538 uint16_t type_idx = field_id.class_idx_;
2539 Class* klass = dex_cache_->GetResolvedTypes()->Get(type_idx);
2540
2541 // Test: Is referring_class equal to declaring_class?
2542
2543 // If true, then return the type_idx; otherwise, this field must be declared
2544 // in super class or interfaces, we have to search for declaring class.
2545
2546 if (field->GetDeclaringClass() == klass) {
2547 resolved_type_idx = type_idx;
2548 return field;
2549 }
2550
2551 // Search for the type_idx of the super class or interfaces
2552 std::string desc(FieldHelper(field).GetDeclaringClassDescriptor());
2553
2554 DexFile::StringId const* string_id = dex_file_->FindStringId(desc);
2555
2556 if (string_id == NULL) {
2557 return NULL;
2558 }
2559
2560 DexFile::TypeId const* type_id =
2561 dex_file_->FindTypeId(dex_file_->GetIndexForStringId(*string_id));
2562
2563 if (type_id == NULL) {
2564 return NULL;
2565 }
2566
2567 resolved_type_idx = dex_file_->GetIndexForTypeId(*type_id);
2568 return field;
2569}
2570
2571
2572llvm::Value* MethodCompiler::EmitLoadStaticStorage(uint32_t dex_pc,
2573 uint32_t type_idx) {
2574 llvm::BasicBlock* block_load_static =
2575 CreateBasicBlockWithDexPC(dex_pc, "load_static");
2576
2577 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
2578
2579 // Load static storage from dex cache
2580 llvm::Value* storage_field_addr =
2581 EmitLoadDexCacheStaticStorageFieldAddr(type_idx);
2582
2583 llvm::Value* storage_object_addr = irb_.CreateLoad(storage_field_addr);
2584
2585 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
2586
2587 // Test: Is the static storage of this class initialized?
2588 llvm::Value* equal_null =
2589 irb_.CreateICmpEQ(storage_object_addr, irb_.getJNull());
2590
2591 irb_.CreateCondBr(equal_null, block_load_static, block_cont);
2592
2593 // Failback routine to load the class object
2594 irb_.SetInsertPoint(block_load_static);
2595
2596 llvm::Function* runtime_func =
2597 irb_.GetRuntime(InitializeStaticStorage);
2598
2599 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
2600
2601 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2602
Logan Chien8dfcbea2012-02-17 18:50:32 +08002603 EmitUpdateLineNumFromDexPC(dex_pc);
2604
Logan Chien438c4b62012-01-17 16:06:00 +08002605 llvm::Value* loaded_storage_object_addr =
2606 irb_.CreateCall2(runtime_func, type_idx_value, method_object_addr);
2607
2608 EmitGuard_ExceptionLandingPad(dex_pc);
2609
2610 llvm::BasicBlock* block_after_load_static = irb_.GetInsertBlock();
2611
2612 irb_.CreateBr(block_cont);
2613
2614 // Now the class object must be loaded
2615 irb_.SetInsertPoint(block_cont);
2616
2617 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
2618
2619 phi->addIncoming(storage_object_addr, block_original);
2620 phi->addIncoming(loaded_storage_object_addr, block_after_load_static);
2621
2622 return phi;
2623}
2624
2625
Logan Chien70f94b42011-12-27 17:49:11 +08002626void MethodCompiler::EmitInsn_SGet(uint32_t dex_pc,
2627 Instruction const* insn,
2628 JType field_jty) {
Logan Chien438c4b62012-01-17 16:06:00 +08002629
Elliott Hughesadb8c672012-03-06 16:49:32 -08002630 DecodedInstruction dec_insn(insn);
Logan Chien438c4b62012-01-17 16:06:00 +08002631
2632 uint32_t declaring_type_idx = DexFile::kDexNoIndex;
2633
Elliott Hughesadb8c672012-03-06 16:49:32 -08002634 Field* field = FindFieldAndDeclaringTypeIdx(dec_insn.vB, declaring_type_idx);
Logan Chien438c4b62012-01-17 16:06:00 +08002635
2636 llvm::Value* static_field_value;
2637
2638 if (field == NULL) {
2639 llvm::Function* runtime_func;
2640
2641 if (field_jty == kObject) {
2642 runtime_func = irb_.GetRuntime(GetObjectStatic);
2643 } else if (field_jty == kLong || field_jty == kDouble) {
2644 runtime_func = irb_.GetRuntime(Get64Static);
2645 } else {
2646 runtime_func = irb_.GetRuntime(Get32Static);
2647 }
2648
Elliott Hughesadb8c672012-03-06 16:49:32 -08002649 llvm::Constant* field_idx_value = irb_.getInt32(dec_insn.vB);
Logan Chien438c4b62012-01-17 16:06:00 +08002650
2651 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2652
Logan Chien8dfcbea2012-02-17 18:50:32 +08002653 EmitUpdateLineNumFromDexPC(dex_pc);
2654
Logan Chien438c4b62012-01-17 16:06:00 +08002655 static_field_value =
2656 irb_.CreateCall2(runtime_func, field_idx_value, method_object_addr);
2657
2658 EmitGuard_ExceptionLandingPad(dex_pc);
2659
2660 } else {
2661 llvm::Value* static_storage_addr =
2662 EmitLoadStaticStorage(dex_pc, declaring_type_idx);
2663
2664 llvm::Value* static_field_offset_value =
2665 irb_.getPtrEquivInt(field->GetOffset().Int32Value());
2666
2667 llvm::Value* static_field_addr =
2668 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
2669 irb_.getJType(field_jty, kField)->getPointerTo());
2670
2671 static_field_value = irb_.CreateLoad(static_field_addr);
2672 }
2673
Elliott Hughesadb8c672012-03-06 16:49:32 -08002674 EmitStoreDalvikReg(dec_insn.vA, field_jty, kField, static_field_value);
Logan Chien438c4b62012-01-17 16:06:00 +08002675
Logan Chien70f94b42011-12-27 17:49:11 +08002676 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2677}
2678
2679
2680void MethodCompiler::EmitInsn_SPut(uint32_t dex_pc,
2681 Instruction const* insn,
2682 JType field_jty) {
Logan Chien14179c82012-01-17 17:06:34 +08002683
Elliott Hughesadb8c672012-03-06 16:49:32 -08002684 DecodedInstruction dec_insn(insn);
Logan Chien14179c82012-01-17 17:06:34 +08002685
2686 uint32_t declaring_type_idx = DexFile::kDexNoIndex;
2687
Elliott Hughesadb8c672012-03-06 16:49:32 -08002688 Field* field = FindFieldAndDeclaringTypeIdx(dec_insn.vB, declaring_type_idx);
Logan Chien14179c82012-01-17 17:06:34 +08002689
Elliott Hughesadb8c672012-03-06 16:49:32 -08002690 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, field_jty, kField);
Logan Chien14179c82012-01-17 17:06:34 +08002691
2692 if (field == NULL) {
2693 llvm::Function* runtime_func;
2694
2695 if (field_jty == kObject) {
2696 runtime_func = irb_.GetRuntime(SetObjectStatic);
2697 } else if (field_jty == kLong || field_jty == kDouble) {
2698 runtime_func = irb_.GetRuntime(Set64Static);
2699 } else {
2700 runtime_func = irb_.GetRuntime(Set32Static);
2701 }
2702
Elliott Hughesadb8c672012-03-06 16:49:32 -08002703 llvm::Constant* field_idx_value = irb_.getInt32(dec_insn.vB);
Logan Chien14179c82012-01-17 17:06:34 +08002704
2705 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2706
Logan Chien8dfcbea2012-02-17 18:50:32 +08002707 EmitUpdateLineNumFromDexPC(dex_pc);
2708
Logan Chien14179c82012-01-17 17:06:34 +08002709 irb_.CreateCall3(runtime_func, field_idx_value,
2710 method_object_addr, new_value);
2711
2712 EmitGuard_ExceptionLandingPad(dex_pc);
2713
2714 } else {
2715 llvm::Value* static_storage_addr =
2716 EmitLoadStaticStorage(dex_pc, declaring_type_idx);
2717
2718 llvm::Value* static_field_offset_value =
2719 irb_.getPtrEquivInt(field->GetOffset().Int32Value());
2720
2721 llvm::Value* static_field_addr =
2722 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
2723 irb_.getJType(field_jty, kField)->getPointerTo());
2724
2725 irb_.CreateStore(new_value, static_field_addr);
2726 }
2727
Logan Chien70f94b42011-12-27 17:49:11 +08002728 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2729}
2730
2731
Logan Chien1a121b92012-02-15 22:23:42 +08002732void MethodCompiler::
2733EmitLoadActualParameters(std::vector<llvm::Value*>& args,
2734 uint32_t callee_method_idx,
Elliott Hughesadb8c672012-03-06 16:49:32 -08002735 DecodedInstruction const& dec_insn,
Logan Chien7e7fabc2012-04-10 18:59:11 +08002736 InvokeArgFmt arg_fmt,
Logan Chien1a121b92012-02-15 22:23:42 +08002737 bool is_static) {
2738
2739 // Get method signature
2740 DexFile::MethodId const& method_id =
2741 dex_file_->GetMethodId(callee_method_idx);
2742
Logan Chien8faf8022012-02-24 12:25:29 +08002743 uint32_t shorty_size;
Logan Chien1a121b92012-02-15 22:23:42 +08002744 char const* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size);
Logan Chien8faf8022012-02-24 12:25:29 +08002745 CHECK_GE(shorty_size, 1u);
Logan Chien1a121b92012-02-15 22:23:42 +08002746
2747 // Load argument values according to the shorty (without "this")
2748 uint16_t reg_count = 0;
2749
2750 if (!is_static) {
2751 ++reg_count; // skip the "this" pointer
2752 }
2753
Logan Chien7e7fabc2012-04-10 18:59:11 +08002754 bool is_range = (arg_fmt == kArgRange);
2755
Logan Chien8faf8022012-02-24 12:25:29 +08002756 for (uint32_t i = 1; i < shorty_size; ++i) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002757 uint32_t reg_idx = (is_range) ? (dec_insn.vC + reg_count)
2758 : (dec_insn.arg[reg_count]);
Logan Chien1a121b92012-02-15 22:23:42 +08002759
2760 args.push_back(EmitLoadDalvikReg(reg_idx, shorty[i], kAccurate));
2761
2762 ++reg_count;
2763 if (shorty[i] == 'J' || shorty[i] == 'D') {
2764 // Wide types, such as long and double, are using a pair of registers
2765 // to store the value, so we have to increase arg_reg again.
2766 ++reg_count;
2767 }
2768 }
2769
Elliott Hughesadb8c672012-03-06 16:49:32 -08002770 DCHECK_EQ(reg_count, dec_insn.vA)
Logan Chien1a121b92012-02-15 22:23:42 +08002771 << "Actual argument mismatch for callee: "
2772 << PrettyMethod(callee_method_idx, *dex_file_);
2773}
2774
TDYa12785321912012-04-01 15:24:56 -07002775llvm::Value* MethodCompiler::EmitEnsureResolved(llvm::Value* callee,
2776 llvm::Value* caller,
2777 uint32_t dex_method_idx,
TDYa1270b686e52012-04-09 22:43:35 -07002778 bool is_virtual) {
2779 // TODO: Remove this after we solve the trampoline related problems.
2780 return irb_.CreateCall4(irb_.GetRuntime(EnsureResolved),
2781 callee,
2782 caller,
2783 irb_.getInt32(dex_method_idx),
2784 irb_.getInt1(is_virtual));
TDYa12785321912012-04-01 15:24:56 -07002785}
2786
TDYa1270b686e52012-04-09 22:43:35 -07002787llvm::Value* MethodCompiler::EmitFixStub(llvm::Value* callee_method_object_addr,
2788 uint32_t method_idx,
2789 bool is_static) {
2790 // TODO: Remove this after we solve the link and trampoline related problems.
2791 llvm::Value* code_addr = irb_.CreateCall(irb_.GetRuntime(FixStub), callee_method_object_addr);
2792
2793 llvm::FunctionType* method_type = GetFunctionType(method_idx, is_static);
2794
2795 return irb_.CreatePointerCast(code_addr, method_type->getPointerTo());
TDYa12785321912012-04-01 15:24:56 -07002796}
Logan Chien1a121b92012-02-15 22:23:42 +08002797
Shih-wei Liao399ed3f2012-03-08 01:27:04 -08002798
Logan Chien7e7fabc2012-04-10 18:59:11 +08002799void MethodCompiler::EmitInsn_Invoke(uint32_t dex_pc,
2800 Instruction const* insn,
2801 InvokeType invoke_type,
2802 InvokeArgFmt arg_fmt) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002803 DecodedInstruction dec_insn(insn);
Logan Chien46fbb412012-02-15 22:29:08 +08002804
Logan Chien61c65dc2012-02-29 03:22:30 +08002805 bool is_static = (invoke_type == kStatic);
Logan Chien7e7fabc2012-04-10 18:59:11 +08002806 uint32_t callee_method_idx = dec_insn.vB;
Logan Chien61c65dc2012-02-29 03:22:30 +08002807
Logan Chien7e7fabc2012-04-10 18:59:11 +08002808 // Compute invoke related information for compiler decision
2809 int vtable_idx = -1;
Logan Chien92ad16d2012-03-18 05:48:55 +08002810 uintptr_t direct_code = 0; // Currently unused
2811 uintptr_t direct_method = 0; // Currently unused
Logan Chien61c65dc2012-02-29 03:22:30 +08002812 bool is_fast_path = compiler_->
Logan Chien7e7fabc2012-04-10 18:59:11 +08002813 ComputeInvokeInfo(callee_method_idx, oat_compilation_unit_,
Logan Chien92ad16d2012-03-18 05:48:55 +08002814 invoke_type, vtable_idx, direct_code, direct_method);
Logan Chien61c65dc2012-02-29 03:22:30 +08002815
Logan Chien7e7fabc2012-04-10 18:59:11 +08002816 // Load *this* actual parameter
Logan Chien1a121b92012-02-15 22:23:42 +08002817 llvm::Value* this_addr = NULL;
2818
2819 if (!is_static) {
2820 // Test: Is *this* parameter equal to null?
Logan Chien7e7fabc2012-04-10 18:59:11 +08002821 this_addr = (arg_fmt == kArgReg) ?
2822 EmitLoadDalvikReg(dec_insn.arg[0], kObject, kAccurate):
2823 EmitLoadDalvikReg(dec_insn.vC + 0, kObject, kAccurate);
2824
Logan Chien1a121b92012-02-15 22:23:42 +08002825 EmitGuard_NullPointerException(dex_pc, this_addr);
2826 }
2827
Logan Chien7e7fabc2012-04-10 18:59:11 +08002828 // Load the method object
TDYa1274e42a592012-04-10 20:13:54 -07002829 llvm::Value* callee_method_object_addr = NULL;
Logan Chien7e7fabc2012-04-10 18:59:11 +08002830
2831 if (!is_fast_path) {
TDYa1274e42a592012-04-10 20:13:54 -07002832 callee_method_object_addr =
Logan Chien7e7fabc2012-04-10 18:59:11 +08002833 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx, invoke_type,
2834 this_addr, dex_pc, is_fast_path);
2835 } else {
2836 switch (invoke_type) {
2837 case kStatic:
2838 case kDirect:
TDYa1274e42a592012-04-10 20:13:54 -07002839 callee_method_object_addr =
Logan Chien7e7fabc2012-04-10 18:59:11 +08002840 EmitLoadSDCalleeMethodObjectAddr(callee_method_idx);
2841 break;
2842
2843 case kVirtual:
2844 DCHECK(vtable_idx != -1);
TDYa1274e42a592012-04-10 20:13:54 -07002845 callee_method_object_addr =
Logan Chien7e7fabc2012-04-10 18:59:11 +08002846 EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr);
2847 break;
2848
2849 case kSuper:
2850 LOG(FATAL) << "invoke-super should be promoted to invoke-direct in "
2851 "the fast path.";
2852 break;
2853
2854 case kInterface:
TDYa1274e42a592012-04-10 20:13:54 -07002855 callee_method_object_addr =
Logan Chien7e7fabc2012-04-10 18:59:11 +08002856 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx,
2857 invoke_type, this_addr,
2858 dex_pc, is_fast_path);
2859 break;
2860 }
TDYa1274e42a592012-04-10 20:13:54 -07002861
2862 // Ensure the callee method object is resolved.
2863 bool is_virtual = (dec_insn.opcode == Instruction::INVOKE_VIRTUAL) ||
2864 (dec_insn.opcode == Instruction::INVOKE_VIRTUAL_RANGE) ||
2865 (dec_insn.opcode == Instruction::INVOKE_SUPER) ||
2866 (dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
2867
2868 llvm::Value* caller_method_object_addr = EmitLoadMethodObjectAddr();
2869
2870 callee_method_object_addr = EmitEnsureResolved(callee_method_object_addr,
2871 caller_method_object_addr,
2872 callee_method_idx,
2873 is_virtual);
2874
2875 EmitGuard_ExceptionLandingPad(dex_pc);
Logan Chien7e7fabc2012-04-10 18:59:11 +08002876 }
2877
TDYa1270b686e52012-04-09 22:43:35 -07002878#if 0
Logan Chien7e7fabc2012-04-10 18:59:11 +08002879 llvm::Value* code_field_offset_value =
2880 irb_.getPtrEquivInt(Method::GetCodeOffset().Int32Value());
TDYa12785321912012-04-01 15:24:56 -07002881
Logan Chien7e7fabc2012-04-10 18:59:11 +08002882 llvm::Value* code_field_addr =
2883 irb_.CreatePtrDisp(callee_method_object_addr, code_field_offset_value,
2884 irb_.getInt8Ty()->getPointerTo()->getPointerTo());
2885
2886 llvm::Value* code_addr = irb_.CreateLoad(code_field_addr);
2887#else
2888 // TODO: Remove this after we solve the link and trampoline related problems.
2889 llvm::Value* code_addr =
2890 EmitFixStub(callee_method_object_addr, callee_method_idx, is_static);
2891
2892 EmitGuard_ExceptionLandingPad(dex_pc);
TDYa1270b686e52012-04-09 22:43:35 -07002893#endif
TDYa12785321912012-04-01 15:24:56 -07002894
Logan Chien1a121b92012-02-15 22:23:42 +08002895 // Load the actual parameter
2896 std::vector<llvm::Value*> args;
2897
2898 args.push_back(callee_method_object_addr); // method object for callee
2899
2900 if (!is_static) {
Logan Chien7e7fabc2012-04-10 18:59:11 +08002901 DCHECK(this_addr != NULL);
Logan Chien1a121b92012-02-15 22:23:42 +08002902 args.push_back(this_addr); // "this" object for callee
2903 }
2904
2905 EmitLoadActualParameters(args, callee_method_idx, dec_insn,
Logan Chien7e7fabc2012-04-10 18:59:11 +08002906 arg_fmt, is_static);
Logan Chien1a121b92012-02-15 22:23:42 +08002907
Logan Chien8dfcbea2012-02-17 18:50:32 +08002908 // Invoke callee
2909 EmitUpdateLineNumFromDexPC(dex_pc);
Logan Chien1a121b92012-02-15 22:23:42 +08002910 llvm::Value* retval = irb_.CreateCall(code_addr, args);
2911 EmitGuard_ExceptionLandingPad(dex_pc);
2912
Logan Chien7e7fabc2012-04-10 18:59:11 +08002913 uint32_t callee_access_flags = is_static ? kAccStatic : 0;
2914 UniquePtr<OatCompilationUnit> callee_oat_compilation_unit(
2915 oat_compilation_unit_->GetCallee(callee_method_idx, callee_access_flags));
2916
2917 char ret_shorty = callee_oat_compilation_unit->GetShorty()[0];
Shih-wei Liao90d50992012-02-19 03:32:05 -08002918 if (ret_shorty != 'V') {
Logan Chien1a121b92012-02-15 22:23:42 +08002919 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
2920 }
2921
Logan Chien70f94b42011-12-27 17:49:11 +08002922 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2923}
2924
2925
Logan Chien7e7fabc2012-04-10 18:59:11 +08002926llvm::Value* MethodCompiler::
2927EmitLoadSDCalleeMethodObjectAddr(uint32_t callee_method_idx) {
2928 llvm::Value* callee_method_object_field_addr =
2929 EmitLoadDexCacheResolvedMethodFieldAddr(callee_method_idx);
Logan Chien7caf37e2012-02-03 22:56:04 +08002930
Logan Chien7e7fabc2012-04-10 18:59:11 +08002931 return irb_.CreateLoad(callee_method_object_field_addr);
2932}
Logan Chien7caf37e2012-02-03 22:56:04 +08002933
Logan Chien7caf37e2012-02-03 22:56:04 +08002934
Logan Chien7e7fabc2012-04-10 18:59:11 +08002935llvm::Value* MethodCompiler::
2936EmitLoadVirtualCalleeMethodObjectAddr(int vtable_idx,
2937 llvm::Value* this_addr) {
2938 // Load class object of *this* pointer
2939 llvm::Constant* class_field_offset_value =
2940 irb_.getPtrEquivInt(Object::ClassOffset().Int32Value());
Logan Chien7caf37e2012-02-03 22:56:04 +08002941
Logan Chien7e7fabc2012-04-10 18:59:11 +08002942 llvm::Value* class_field_addr =
2943 irb_.CreatePtrDisp(this_addr, class_field_offset_value,
2944 irb_.getJObjectTy()->getPointerTo());
Logan Chien7caf37e2012-02-03 22:56:04 +08002945
Logan Chien7e7fabc2012-04-10 18:59:11 +08002946 llvm::Value* class_object_addr = irb_.CreateLoad(class_field_addr);
Logan Chien7caf37e2012-02-03 22:56:04 +08002947
Logan Chien7e7fabc2012-04-10 18:59:11 +08002948 // Load vtable address
2949 llvm::Constant* vtable_offset_value =
2950 irb_.getPtrEquivInt(Class::VTableOffset().Int32Value());
2951
2952 llvm::Value* vtable_field_addr =
2953 irb_.CreatePtrDisp(class_object_addr, vtable_offset_value,
2954 irb_.getJObjectTy()->getPointerTo());
2955
2956 llvm::Value* vtable_addr = irb_.CreateLoad(vtable_field_addr);
2957
2958 // Load callee method object
2959 llvm::Value* vtable_idx_value =
2960 irb_.getPtrEquivInt(static_cast<uint64_t>(vtable_idx));
2961
2962 llvm::Value* method_field_addr =
2963 EmitArrayGEP(vtable_addr, vtable_idx_value, irb_.getJObjectTy(), kObject);
2964
2965 return irb_.CreateLoad(method_field_addr);
2966}
2967
2968
2969llvm::Value* MethodCompiler::
2970EmitCallRuntimeForCalleeMethodObjectAddr(uint32_t callee_method_idx,
2971 InvokeType invoke_type,
2972 llvm::Value* this_addr,
2973 uint32_t dex_pc,
2974 bool is_fast_path) {
2975
2976 llvm::Function* runtime_func = NULL;
2977
2978 switch (invoke_type) {
2979 case kStatic:
2980 runtime_func = irb_.GetRuntime(FindStaticMethodWithAccessCheck);
2981 break;
2982
2983 case kDirect:
2984 runtime_func = irb_.GetRuntime(FindDirectMethodWithAccessCheck);
2985 break;
2986
2987 case kVirtual:
2988 runtime_func = irb_.GetRuntime(FindVirtualMethodWithAccessCheck);
2989 break;
2990
2991 case kSuper:
2992 runtime_func = irb_.GetRuntime(FindSuperMethodWithAccessCheck);
2993 break;
2994
2995 case kInterface:
2996 if (is_fast_path) {
2997 runtime_func = irb_.GetRuntime(FindInterfaceMethod);
2998 } else {
2999 runtime_func = irb_.GetRuntime(FindInterfaceMethodWithAccessCheck);
3000 }
3001 break;
3002 }
Logan Chien7caf37e2012-02-03 22:56:04 +08003003
3004 llvm::Value* callee_method_idx_value = irb_.getInt32(callee_method_idx);
3005
Logan Chien7e7fabc2012-04-10 18:59:11 +08003006 if (this_addr == NULL) {
3007 DCHECK_EQ(invoke_type, kStatic);
3008 this_addr = irb_.getJNull();
3009 }
3010
3011 llvm::Value* caller_method_object_addr = EmitLoadMethodObjectAddr();
3012
Logan Chien8dfcbea2012-02-17 18:50:32 +08003013 EmitUpdateLineNumFromDexPC(dex_pc);
3014
TDYa1270b686e52012-04-09 22:43:35 -07003015 llvm::Value* callee_method_object_addr =
Shih-wei Liao399ed3f2012-03-08 01:27:04 -08003016 irb_.CreateCall3(runtime_func,
Logan Chien7e7fabc2012-04-10 18:59:11 +08003017 callee_method_idx_value,
3018 this_addr,
3019 caller_method_object_addr);
Logan Chien7caf37e2012-02-03 22:56:04 +08003020
3021 EmitGuard_ExceptionLandingPad(dex_pc);
3022
Logan Chien7e7fabc2012-04-10 18:59:11 +08003023 return callee_method_object_addr;
Logan Chien70f94b42011-12-27 17:49:11 +08003024}
3025
3026
3027void MethodCompiler::EmitInsn_Neg(uint32_t dex_pc,
3028 Instruction const* insn,
3029 JType op_jty) {
Logan Chien1b5685f2011-12-27 18:01:14 +08003030
Elliott Hughesadb8c672012-03-06 16:49:32 -08003031 DecodedInstruction dec_insn(insn);
Logan Chien1b5685f2011-12-27 18:01:14 +08003032
3033 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3034
Elliott Hughesadb8c672012-03-06 16:49:32 -08003035 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chien1b5685f2011-12-27 18:01:14 +08003036 llvm::Value* result_value = irb_.CreateNeg(src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003037 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chien1b5685f2011-12-27 18:01:14 +08003038
Logan Chien70f94b42011-12-27 17:49:11 +08003039 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3040}
3041
3042
3043void MethodCompiler::EmitInsn_Not(uint32_t dex_pc,
3044 Instruction const* insn,
3045 JType op_jty) {
Logan Chiene53750d2011-12-27 18:02:27 +08003046
Elliott Hughesadb8c672012-03-06 16:49:32 -08003047 DecodedInstruction dec_insn(insn);
Logan Chiene53750d2011-12-27 18:02:27 +08003048
3049 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3050
Elliott Hughesadb8c672012-03-06 16:49:32 -08003051 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chiene53750d2011-12-27 18:02:27 +08003052 llvm::Value* result_value =
3053 irb_.CreateXor(src_value, static_cast<uint64_t>(-1));
3054
Elliott Hughesadb8c672012-03-06 16:49:32 -08003055 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chiene53750d2011-12-27 18:02:27 +08003056
Logan Chien70f94b42011-12-27 17:49:11 +08003057 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3058}
3059
3060
3061void MethodCompiler::EmitInsn_SExt(uint32_t dex_pc,
3062 Instruction const* insn) {
Logan Chien61752ad2011-12-27 18:03:51 +08003063
Elliott Hughesadb8c672012-03-06 16:49:32 -08003064 DecodedInstruction dec_insn(insn);
Logan Chien61752ad2011-12-27 18:03:51 +08003065
Elliott Hughesadb8c672012-03-06 16:49:32 -08003066 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chien61752ad2011-12-27 18:03:51 +08003067 llvm::Value* result_value = irb_.CreateSExt(src_value, irb_.getJLongTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003068 EmitStoreDalvikReg(dec_insn.vA, kLong, kAccurate, result_value);
Logan Chien61752ad2011-12-27 18:03:51 +08003069
Logan Chien70f94b42011-12-27 17:49:11 +08003070 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3071}
3072
3073
3074void MethodCompiler::EmitInsn_Trunc(uint32_t dex_pc,
3075 Instruction const* insn) {
Logan Chien17a57662011-12-27 18:05:14 +08003076
Elliott Hughesadb8c672012-03-06 16:49:32 -08003077 DecodedInstruction dec_insn(insn);
Logan Chien17a57662011-12-27 18:05:14 +08003078
Elliott Hughesadb8c672012-03-06 16:49:32 -08003079 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kLong, kAccurate);
Logan Chien17a57662011-12-27 18:05:14 +08003080 llvm::Value* result_value = irb_.CreateTrunc(src_value, irb_.getJIntTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003081 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chien17a57662011-12-27 18:05:14 +08003082
Logan Chien70f94b42011-12-27 17:49:11 +08003083 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3084}
3085
3086
3087void MethodCompiler::EmitInsn_TruncAndSExt(uint32_t dex_pc,
3088 Instruction const* insn,
3089 unsigned N) {
Logan Chienb6744c52011-12-27 18:06:26 +08003090
Elliott Hughesadb8c672012-03-06 16:49:32 -08003091 DecodedInstruction dec_insn(insn);
Logan Chienb6744c52011-12-27 18:06:26 +08003092
Elliott Hughesadb8c672012-03-06 16:49:32 -08003093 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chienb6744c52011-12-27 18:06:26 +08003094
3095 llvm::Value* trunc_value =
3096 irb_.CreateTrunc(src_value, llvm::Type::getIntNTy(*context_, N));
3097
3098 llvm::Value* result_value = irb_.CreateSExt(trunc_value, irb_.getJIntTy());
3099
Elliott Hughesadb8c672012-03-06 16:49:32 -08003100 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chienb6744c52011-12-27 18:06:26 +08003101
Logan Chien70f94b42011-12-27 17:49:11 +08003102 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3103}
3104
3105
3106void MethodCompiler::EmitInsn_TruncAndZExt(uint32_t dex_pc,
3107 Instruction const* insn,
3108 unsigned N) {
Logan Chienb6744c52011-12-27 18:06:26 +08003109
Elliott Hughesadb8c672012-03-06 16:49:32 -08003110 DecodedInstruction dec_insn(insn);
Logan Chienb6744c52011-12-27 18:06:26 +08003111
Elliott Hughesadb8c672012-03-06 16:49:32 -08003112 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chienb6744c52011-12-27 18:06:26 +08003113
3114 llvm::Value* trunc_value =
3115 irb_.CreateTrunc(src_value, llvm::Type::getIntNTy(*context_, N));
3116
3117 llvm::Value* result_value = irb_.CreateZExt(trunc_value, irb_.getJIntTy());
3118
Elliott Hughesadb8c672012-03-06 16:49:32 -08003119 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chienb6744c52011-12-27 18:06:26 +08003120
Logan Chien70f94b42011-12-27 17:49:11 +08003121 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3122}
3123
3124
3125void MethodCompiler::EmitInsn_FNeg(uint32_t dex_pc,
3126 Instruction const* insn,
3127 JType op_jty) {
Logan Chien7a48b092011-12-27 18:07:45 +08003128
Elliott Hughesadb8c672012-03-06 16:49:32 -08003129 DecodedInstruction dec_insn(insn);
Logan Chien7a48b092011-12-27 18:07:45 +08003130
3131 DCHECK(op_jty == kFloat || op_jty == kDouble) << op_jty;
3132
Elliott Hughesadb8c672012-03-06 16:49:32 -08003133 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chien7a48b092011-12-27 18:07:45 +08003134 llvm::Value* result_value = irb_.CreateFNeg(src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003135 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chien7a48b092011-12-27 18:07:45 +08003136
Logan Chien70f94b42011-12-27 17:49:11 +08003137 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3138}
3139
3140
3141void MethodCompiler::EmitInsn_IntToFP(uint32_t dex_pc,
3142 Instruction const* insn,
3143 JType src_jty,
3144 JType dest_jty) {
Logan Chien62dd4532011-12-27 18:09:00 +08003145
Elliott Hughesadb8c672012-03-06 16:49:32 -08003146 DecodedInstruction dec_insn(insn);
Logan Chien62dd4532011-12-27 18:09:00 +08003147
3148 DCHECK(src_jty == kInt || src_jty == kLong) << src_jty;
3149 DCHECK(dest_jty == kFloat || dest_jty == kDouble) << dest_jty;
3150
Elliott Hughesadb8c672012-03-06 16:49:32 -08003151 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, src_jty, kAccurate);
Logan Chien62dd4532011-12-27 18:09:00 +08003152 llvm::Type* dest_type = irb_.getJType(dest_jty, kAccurate);
3153 llvm::Value* dest_value = irb_.CreateSIToFP(src_value, dest_type);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003154 EmitStoreDalvikReg(dec_insn.vA, dest_jty, kAccurate, dest_value);
Logan Chien62dd4532011-12-27 18:09:00 +08003155
Logan Chien70f94b42011-12-27 17:49:11 +08003156 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3157}
3158
3159
3160void MethodCompiler::EmitInsn_FPToInt(uint32_t dex_pc,
3161 Instruction const* insn,
3162 JType src_jty,
3163 JType dest_jty) {
Logan Chien12dc1752011-12-27 18:10:15 +08003164
Elliott Hughesadb8c672012-03-06 16:49:32 -08003165 DecodedInstruction dec_insn(insn);
Logan Chien12dc1752011-12-27 18:10:15 +08003166
3167 DCHECK(src_jty == kFloat || src_jty == kDouble) << src_jty;
3168 DCHECK(dest_jty == kInt || dest_jty == kLong) << dest_jty;
3169
Elliott Hughesadb8c672012-03-06 16:49:32 -08003170 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, src_jty, kAccurate);
Logan Chien12dc1752011-12-27 18:10:15 +08003171 llvm::Type* dest_type = irb_.getJType(dest_jty, kAccurate);
3172 llvm::Value* dest_value = irb_.CreateFPToSI(src_value, dest_type);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003173 EmitStoreDalvikReg(dec_insn.vA, dest_jty, kAccurate, dest_value);
Logan Chien12dc1752011-12-27 18:10:15 +08003174
Logan Chien70f94b42011-12-27 17:49:11 +08003175 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3176}
3177
3178
3179void MethodCompiler::EmitInsn_FExt(uint32_t dex_pc,
3180 Instruction const* insn) {
Logan Chienc56ded92011-12-27 18:10:57 +08003181
Elliott Hughesadb8c672012-03-06 16:49:32 -08003182 DecodedInstruction dec_insn(insn);
Logan Chienc56ded92011-12-27 18:10:57 +08003183
Elliott Hughesadb8c672012-03-06 16:49:32 -08003184 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kFloat, kAccurate);
Logan Chienc56ded92011-12-27 18:10:57 +08003185 llvm::Value* result_value = irb_.CreateFPExt(src_value, irb_.getJDoubleTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003186 EmitStoreDalvikReg(dec_insn.vA, kDouble, kAccurate, result_value);
Logan Chienc56ded92011-12-27 18:10:57 +08003187
Logan Chien70f94b42011-12-27 17:49:11 +08003188 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3189}
3190
3191
3192void MethodCompiler::EmitInsn_FTrunc(uint32_t dex_pc,
3193 Instruction const* insn) {
Logan Chien927744f2011-12-27 18:11:52 +08003194
Elliott Hughesadb8c672012-03-06 16:49:32 -08003195 DecodedInstruction dec_insn(insn);
Logan Chien927744f2011-12-27 18:11:52 +08003196
Elliott Hughesadb8c672012-03-06 16:49:32 -08003197 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kDouble, kAccurate);
Logan Chien927744f2011-12-27 18:11:52 +08003198 llvm::Value* result_value = irb_.CreateFPTrunc(src_value, irb_.getJFloatTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003199 EmitStoreDalvikReg(dec_insn.vA, kFloat, kAccurate, result_value);
Logan Chien927744f2011-12-27 18:11:52 +08003200
Logan Chien70f94b42011-12-27 17:49:11 +08003201 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3202}
3203
3204
3205void MethodCompiler::EmitInsn_IntArithm(uint32_t dex_pc,
3206 Instruction const* insn,
3207 IntArithmKind arithm,
3208 JType op_jty,
3209 bool is_2addr) {
Logan Chienc3f7d962011-12-27 18:13:18 +08003210
Elliott Hughesadb8c672012-03-06 16:49:32 -08003211 DecodedInstruction dec_insn(insn);
Logan Chienc3f7d962011-12-27 18:13:18 +08003212
3213 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3214
3215 llvm::Value* src1_value;
3216 llvm::Value* src2_value;
3217
3218 if (is_2addr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003219 src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate);
3220 src2_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chienc3f7d962011-12-27 18:13:18 +08003221 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003222 src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
3223 src2_value = EmitLoadDalvikReg(dec_insn.vC, op_jty, kAccurate);
Logan Chienc3f7d962011-12-27 18:13:18 +08003224 }
3225
3226 llvm::Value* result_value =
3227 EmitIntArithmResultComputation(dex_pc, src1_value, src2_value,
3228 arithm, op_jty);
3229
Elliott Hughesadb8c672012-03-06 16:49:32 -08003230 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chienc3f7d962011-12-27 18:13:18 +08003231
Logan Chien70f94b42011-12-27 17:49:11 +08003232 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3233}
3234
3235
3236void MethodCompiler::EmitInsn_IntArithmImmediate(uint32_t dex_pc,
3237 Instruction const* insn,
3238 IntArithmKind arithm) {
Logan Chienc3f7d962011-12-27 18:13:18 +08003239
Elliott Hughesadb8c672012-03-06 16:49:32 -08003240 DecodedInstruction dec_insn(insn);
Logan Chienc3f7d962011-12-27 18:13:18 +08003241
Elliott Hughesadb8c672012-03-06 16:49:32 -08003242 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chienc3f7d962011-12-27 18:13:18 +08003243
Elliott Hughesadb8c672012-03-06 16:49:32 -08003244 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC);
Logan Chienc3f7d962011-12-27 18:13:18 +08003245
3246 llvm::Value* result_value =
3247 EmitIntArithmResultComputation(dex_pc, src_value, imm_value, arithm, kInt);
3248
Elliott Hughesadb8c672012-03-06 16:49:32 -08003249 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chienc3f7d962011-12-27 18:13:18 +08003250
Logan Chien70f94b42011-12-27 17:49:11 +08003251 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3252}
3253
3254
Logan Chienc3f7d962011-12-27 18:13:18 +08003255llvm::Value*
3256MethodCompiler::EmitIntArithmResultComputation(uint32_t dex_pc,
3257 llvm::Value* lhs,
3258 llvm::Value* rhs,
3259 IntArithmKind arithm,
3260 JType op_jty) {
3261 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3262
3263 switch (arithm) {
3264 case kIntArithm_Add:
3265 return irb_.CreateAdd(lhs, rhs);
3266
3267 case kIntArithm_Sub:
3268 return irb_.CreateSub(lhs, rhs);
3269
3270 case kIntArithm_Mul:
3271 return irb_.CreateMul(lhs, rhs);
3272
3273 case kIntArithm_Div:
Logan Chienc3f7d962011-12-27 18:13:18 +08003274 case kIntArithm_Rem:
TDYa127f8641ce2012-04-02 06:40:40 -07003275 return EmitIntDivRemResultComputation(dex_pc, lhs, rhs, arithm, op_jty);
Logan Chienc3f7d962011-12-27 18:13:18 +08003276
3277 case kIntArithm_And:
3278 return irb_.CreateAnd(lhs, rhs);
3279
3280 case kIntArithm_Or:
3281 return irb_.CreateOr(lhs, rhs);
3282
3283 case kIntArithm_Xor:
3284 return irb_.CreateXor(lhs, rhs);
3285
Logan Chienc3f7d962011-12-27 18:13:18 +08003286 default:
3287 LOG(FATAL) << "Unknown integer arithmetic kind: " << arithm;
3288 return NULL;
3289 }
3290}
3291
3292
TDYa127f8641ce2012-04-02 06:40:40 -07003293llvm::Value*
3294MethodCompiler::EmitIntDivRemResultComputation(uint32_t dex_pc,
3295 llvm::Value* dividend,
3296 llvm::Value* divisor,
3297 IntArithmKind arithm,
3298 JType op_jty) {
3299 // Throw exception if the divisor is 0.
3300 EmitGuard_DivZeroException(dex_pc, divisor, op_jty);
3301
3302 // Check the special case: MININT / -1 = MININT
3303 // That case will cause overflow, which is undefined behavior in llvm.
3304 // So we check the divisor is -1 or not, if the divisor is -1, we do
3305 // the special path to avoid undefined behavior.
3306 llvm::Type* op_type = irb_.getJType(op_jty, kAccurate);
3307 llvm::Value* zero = irb_.getJZero(op_jty);
3308 llvm::Value* neg_one = llvm::ConstantInt::getSigned(op_type, -1);
3309 llvm::Value* result = irb_.CreateAlloca(op_type);
3310
3311 llvm::BasicBlock* eq_neg_one = CreateBasicBlockWithDexPC(dex_pc, "eq_neg_one");
3312 llvm::BasicBlock* ne_neg_one = CreateBasicBlockWithDexPC(dex_pc, "ne_neg_one");
3313 llvm::BasicBlock* neg_one_cont = CreateBasicBlockWithDexPC(dex_pc, "neg_one_cont");
3314
3315 llvm::Value* is_equal_neg_one = EmitConditionResult(divisor, neg_one, kCondBranch_EQ);
3316 irb_.CreateCondBr(is_equal_neg_one, eq_neg_one, ne_neg_one);
3317
3318 // If divisor == -1
3319 irb_.SetInsertPoint(eq_neg_one);
3320 llvm::Value* eq_result;
3321 if (arithm == kIntArithm_Div) {
3322 // We can just change from "dividend div -1" to "neg dividend".
3323 // The sub don't care the sign/unsigned because of two's complement representation.
3324 // And the behavior is what we want:
3325 // -(2^n) (2^n)-1
3326 // MININT < k <= MAXINT -> mul k -1 = -k
3327 // MININT == k -> mul k -1 = k
3328 //
3329 // LLVM use sub to represent 'neg'
3330 eq_result = irb_.CreateSub(zero, dividend);
3331 } else {
3332 // Everything modulo -1 will be 0.
3333 eq_result = zero;
3334 }
3335 irb_.CreateStore(eq_result, result);
3336 irb_.CreateBr(neg_one_cont);
3337
3338 // If divisor != -1, just do the division.
3339 irb_.SetInsertPoint(ne_neg_one);
3340 llvm::Value* ne_result;
3341 if (arithm == kIntArithm_Div) {
3342 ne_result = irb_.CreateSDiv(dividend, divisor);
3343 } else {
3344 ne_result = irb_.CreateSRem(dividend, divisor);
3345 }
3346 irb_.CreateStore(ne_result, result);
3347 irb_.CreateBr(neg_one_cont);
3348
3349 irb_.SetInsertPoint(neg_one_cont);
3350 return irb_.CreateLoad(result);
3351}
3352
3353
Logan Chien5539ad02012-04-02 14:36:55 +08003354void MethodCompiler::EmitInsn_IntShiftArithm(uint32_t dex_pc,
3355 Instruction const* insn,
3356 IntShiftArithmKind arithm,
3357 JType op_jty,
3358 bool is_2addr) {
3359
3360 DecodedInstruction dec_insn(insn);
3361
3362 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3363
3364 llvm::Value* src1_value;
3365 llvm::Value* src2_value;
3366
3367 // NOTE: The 2nd operand of the shift arithmetic instruction is
3368 // 32-bit integer regardless of the 1st operand.
3369 if (is_2addr) {
3370 src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate);
3371 src2_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
3372 } else {
3373 src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
3374 src2_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate);
3375 }
3376
3377 llvm::Value* result_value =
3378 EmitIntShiftArithmResultComputation(dex_pc, src1_value, src2_value,
3379 arithm, op_jty);
3380
3381 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
3382
3383 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3384}
3385
3386
3387void MethodCompiler::
3388EmitInsn_IntShiftArithmImmediate(uint32_t dex_pc,
3389 Instruction const* insn,
3390 IntShiftArithmKind arithm) {
3391
3392 DecodedInstruction dec_insn(insn);
3393
3394 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
3395
3396 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC);
3397
3398 llvm::Value* result_value =
3399 EmitIntShiftArithmResultComputation(dex_pc, src_value, imm_value,
3400 arithm, kInt);
3401
3402 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
3403
3404 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3405}
3406
3407
3408llvm::Value*
3409MethodCompiler::EmitIntShiftArithmResultComputation(uint32_t dex_pc,
3410 llvm::Value* lhs,
3411 llvm::Value* rhs,
3412 IntShiftArithmKind arithm,
3413 JType op_jty) {
3414 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3415
3416 if (op_jty == kInt) {
3417 rhs = irb_.CreateAnd(rhs, 0x1f);
3418 } else {
3419 llvm::Value* masked_rhs = irb_.CreateAnd(rhs, 0x3f);
3420 rhs = irb_.CreateZExt(masked_rhs, irb_.getJLongTy());
3421 }
3422
3423 switch (arithm) {
3424 case kIntArithm_Shl:
3425 return irb_.CreateShl(lhs, rhs);
3426
3427 case kIntArithm_Shr:
3428 return irb_.CreateAShr(lhs, rhs);
3429
3430 case kIntArithm_UShr:
3431 return irb_.CreateLShr(lhs, rhs);
3432
3433 default:
3434 LOG(FATAL) << "Unknown integer shift arithmetic kind: " << arithm;
3435 return NULL;
3436 }
3437}
3438
3439
Logan Chien70f94b42011-12-27 17:49:11 +08003440void MethodCompiler::EmitInsn_RSubImmediate(uint32_t dex_pc,
3441 Instruction const* insn) {
Logan Chien65c62d42011-12-27 18:14:18 +08003442
Elliott Hughesadb8c672012-03-06 16:49:32 -08003443 DecodedInstruction dec_insn(insn);
Logan Chien65c62d42011-12-27 18:14:18 +08003444
Elliott Hughesadb8c672012-03-06 16:49:32 -08003445 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
3446 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC);
Logan Chien65c62d42011-12-27 18:14:18 +08003447 llvm::Value* result_value = irb_.CreateSub(imm_value, src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003448 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chien65c62d42011-12-27 18:14:18 +08003449
Logan Chien70f94b42011-12-27 17:49:11 +08003450 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3451}
3452
3453
3454void MethodCompiler::EmitInsn_FPArithm(uint32_t dex_pc,
3455 Instruction const* insn,
3456 FPArithmKind arithm,
3457 JType op_jty,
3458 bool is_2addr) {
Logan Chien76e1c792011-12-27 18:15:01 +08003459
Elliott Hughesadb8c672012-03-06 16:49:32 -08003460 DecodedInstruction dec_insn(insn);
Logan Chien76e1c792011-12-27 18:15:01 +08003461
3462 DCHECK(op_jty == kFloat || op_jty == kDouble) << op_jty;
3463
3464 llvm::Value* src1_value;
3465 llvm::Value* src2_value;
3466
3467 if (is_2addr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003468 src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate);
3469 src2_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chien76e1c792011-12-27 18:15:01 +08003470 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003471 src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
3472 src2_value = EmitLoadDalvikReg(dec_insn.vC, op_jty, kAccurate);
Logan Chien76e1c792011-12-27 18:15:01 +08003473 }
3474
3475 llvm::Value* result_value =
3476 EmitFPArithmResultComputation(dex_pc, src1_value, src2_value, arithm);
3477
Elliott Hughesadb8c672012-03-06 16:49:32 -08003478 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chien76e1c792011-12-27 18:15:01 +08003479
Logan Chien70f94b42011-12-27 17:49:11 +08003480 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3481}
3482
3483
Logan Chien76e1c792011-12-27 18:15:01 +08003484llvm::Value*
3485MethodCompiler::EmitFPArithmResultComputation(uint32_t dex_pc,
3486 llvm::Value *lhs,
3487 llvm::Value *rhs,
3488 FPArithmKind arithm) {
3489 switch (arithm) {
3490 case kFPArithm_Add:
3491 return irb_.CreateFAdd(lhs, rhs);
3492
3493 case kFPArithm_Sub:
3494 return irb_.CreateFSub(lhs, rhs);
3495
3496 case kFPArithm_Mul:
3497 return irb_.CreateFMul(lhs, rhs);
3498
3499 case kFPArithm_Div:
3500 return irb_.CreateFDiv(lhs, rhs);
3501
3502 case kFPArithm_Rem:
3503 return irb_.CreateFRem(lhs, rhs);
3504
3505 default:
3506 LOG(FATAL) << "Unknown floating-point arithmetic kind: " << arithm;
3507 return NULL;
3508 }
3509}
3510
3511
Logan Chienc3f7d962011-12-27 18:13:18 +08003512void MethodCompiler::EmitGuard_DivZeroException(uint32_t dex_pc,
3513 llvm::Value* denominator,
3514 JType op_jty) {
3515 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3516
3517 llvm::Constant* zero = irb_.getJZero(op_jty);
3518
3519 llvm::Value* equal_zero = irb_.CreateICmpEQ(denominator, zero);
3520
3521 llvm::BasicBlock* block_exception = CreateBasicBlockWithDexPC(dex_pc, "div0");
3522
3523 llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont");
3524
3525 irb_.CreateCondBr(equal_zero, block_exception, block_continue);
3526
3527 irb_.SetInsertPoint(block_exception);
Logan Chien8dfcbea2012-02-17 18:50:32 +08003528 EmitUpdateLineNumFromDexPC(dex_pc);
Logan Chienc3f7d962011-12-27 18:13:18 +08003529 irb_.CreateCall(irb_.GetRuntime(ThrowDivZeroException));
3530 EmitBranchExceptionLandingPad(dex_pc);
3531
3532 irb_.SetInsertPoint(block_continue);
3533}
3534
3535
Logan Chien61bb6142012-02-03 15:34:53 +08003536void MethodCompiler::EmitGuard_NullPointerException(uint32_t dex_pc,
3537 llvm::Value* object) {
3538 llvm::Value* equal_null = irb_.CreateICmpEQ(object, irb_.getJNull());
3539
3540 llvm::BasicBlock* block_exception =
3541 CreateBasicBlockWithDexPC(dex_pc, "nullp");
3542
3543 llvm::BasicBlock* block_continue =
3544 CreateBasicBlockWithDexPC(dex_pc, "cont");
3545
3546 irb_.CreateCondBr(equal_null, block_exception, block_continue);
3547
3548 irb_.SetInsertPoint(block_exception);
Logan Chien8dfcbea2012-02-17 18:50:32 +08003549 EmitUpdateLineNumFromDexPC(dex_pc);
TDYa1273f9137d2012-04-08 15:59:19 -07003550 irb_.CreateCall(irb_.GetRuntime(ThrowNullPointerException), irb_.getInt32(dex_pc));
Logan Chien61bb6142012-02-03 15:34:53 +08003551 EmitBranchExceptionLandingPad(dex_pc);
3552
3553 irb_.SetInsertPoint(block_continue);
3554}
3555
3556
Logan Chienbb4d12a2012-02-17 14:10:01 +08003557llvm::Value* MethodCompiler::EmitLoadDexCacheAddr(MemberOffset offset) {
3558 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
3559
3560 llvm::Value* dex_cache_offset_value =
3561 irb_.getPtrEquivInt(offset.Int32Value());
3562
3563 llvm::Value* dex_cache_field_addr =
3564 irb_.CreatePtrDisp(method_object_addr, dex_cache_offset_value,
3565 irb_.getJObjectTy()->getPointerTo());
3566
3567 return irb_.CreateLoad(dex_cache_field_addr);
3568}
3569
3570
Logan Chienbb4d12a2012-02-17 14:10:01 +08003571llvm::Value* MethodCompiler::
3572EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx) {
3573 llvm::Value* static_storage_dex_cache_addr =
3574 EmitLoadDexCacheAddr(Method::DexCacheInitializedStaticStorageOffset());
3575
3576 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
3577
3578 return EmitArrayGEP(static_storage_dex_cache_addr, type_idx_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08003579 irb_.getJObjectTy(), kObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003580}
3581
3582
3583llvm::Value* MethodCompiler::
3584EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx) {
3585 llvm::Value* resolved_type_dex_cache_addr =
3586 EmitLoadDexCacheAddr(Method::DexCacheResolvedTypesOffset());
3587
3588 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
3589
3590 return EmitArrayGEP(resolved_type_dex_cache_addr, type_idx_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08003591 irb_.getJObjectTy(), kObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003592}
3593
3594
3595llvm::Value* MethodCompiler::
Logan Chien61c65dc2012-02-29 03:22:30 +08003596EmitLoadDexCacheResolvedMethodFieldAddr(uint32_t method_idx) {
3597 llvm::Value* resolved_method_dex_cache_addr =
3598 EmitLoadDexCacheAddr(Method::DexCacheResolvedMethodsOffset());
3599
3600 llvm::Value* method_idx_value = irb_.getPtrEquivInt(method_idx);
3601
3602 return EmitArrayGEP(resolved_method_dex_cache_addr, method_idx_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08003603 irb_.getJObjectTy(), kObject);
Logan Chien61c65dc2012-02-29 03:22:30 +08003604}
3605
3606
3607llvm::Value* MethodCompiler::
Logan Chienbb4d12a2012-02-17 14:10:01 +08003608EmitLoadDexCacheStringFieldAddr(uint32_t string_idx) {
3609 llvm::Value* string_dex_cache_addr =
3610 EmitLoadDexCacheAddr(Method::DexCacheStringsOffset());
3611
3612 llvm::Value* string_idx_value = irb_.getPtrEquivInt(string_idx);
3613
3614 return EmitArrayGEP(string_dex_cache_addr, string_idx_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08003615 irb_.getJObjectTy(), kObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003616}
3617
3618
Logan Chien83426162011-12-09 09:29:50 +08003619CompiledMethod *MethodCompiler::Compile() {
Logan Chien0b827102011-12-20 19:46:14 +08003620 // Code generation
3621 CreateFunction();
3622
3623 EmitPrologue();
3624 EmitInstructions();
Logan Chienc670a8d2011-12-20 21:25:56 +08003625 EmitPrologueLastBranch();
Logan Chien0b827102011-12-20 19:46:14 +08003626
Logan Chiend6c239a2011-12-23 15:11:45 +08003627 // Verify the generated bitcode
3628 llvm::verifyFunction(*func_, llvm::PrintMessageAction);
3629
Logan Chien8b977d32012-02-21 19:14:55 +08003630 // Add the memory usage approximation of the compilation unit
3631 cunit_->AddMemUsageApproximation(code_item_->insns_size_in_code_units_ * 900);
3632 // NOTE: From statistic, the bitcode size is 4.5 times bigger than the
3633 // Dex file. Besides, we have to convert the code unit into bytes.
3634 // Thus, we got our magic number 9.
3635
Logan Chien6920bce2012-03-17 21:44:01 +08003636 return new CompiledMethod(cunit_->GetInstructionSet(),
Logan Chien937105a2012-04-02 02:37:37 +08003637 cunit_->GetElfIndex(),
3638 elf_func_idx_);
Logan Chien0b827102011-12-20 19:46:14 +08003639}
3640
3641
3642llvm::Value* MethodCompiler::EmitLoadMethodObjectAddr() {
3643 return func_->arg_begin();
Shih-wei Liaod1fec812012-02-13 09:51:10 -08003644}
Logan Chien83426162011-12-09 09:29:50 +08003645
3646
Logan Chien5bcc04e2012-01-30 14:15:12 +08003647void MethodCompiler::EmitBranchExceptionLandingPad(uint32_t dex_pc) {
3648 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
3649 irb_.CreateBr(lpad);
3650 } else {
3651 irb_.CreateBr(GetUnwindBasicBlock());
3652 }
3653}
3654
3655
3656void MethodCompiler::EmitGuard_ExceptionLandingPad(uint32_t dex_pc) {
3657 llvm::Value* exception_pending =
3658 irb_.CreateCall(irb_.GetRuntime(IsExceptionPending));
3659
3660 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
3661
3662 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
3663 irb_.CreateCondBr(exception_pending, lpad, block_cont);
3664 } else {
3665 irb_.CreateCondBr(exception_pending, GetUnwindBasicBlock(), block_cont);
3666 }
3667
3668 irb_.SetInsertPoint(block_cont);
3669}
3670
3671
Logan Chien924072f2012-01-30 15:07:24 +08003672void MethodCompiler::EmitGuard_GarbageCollectionSuspend(uint32_t dex_pc) {
3673 llvm::Value* runtime_func = irb_.GetRuntime(TestSuspend);
Logan Chien8dfcbea2012-02-17 18:50:32 +08003674 EmitUpdateLineNumFromDexPC(dex_pc);
Logan Chien924072f2012-01-30 15:07:24 +08003675 irb_.CreateCall(runtime_func);
3676
3677 EmitGuard_ExceptionLandingPad(dex_pc);
3678}
3679
3680
Logan Chiend6c239a2011-12-23 15:11:45 +08003681llvm::BasicBlock* MethodCompiler::
3682CreateBasicBlockWithDexPC(uint32_t dex_pc, char const* postfix) {
3683 std::string name;
3684
3685 if (postfix) {
3686 StringAppendF(&name, "B%u.%s", dex_pc, postfix);
3687 } else {
3688 StringAppendF(&name, "B%u", dex_pc);
3689 }
3690
3691 return llvm::BasicBlock::Create(*context_, name, func_);
3692}
3693
3694
3695llvm::BasicBlock* MethodCompiler::GetBasicBlock(uint32_t dex_pc) {
3696 DCHECK(dex_pc < code_item_->insns_size_in_code_units_);
3697
3698 llvm::BasicBlock* basic_block = basic_blocks_[dex_pc];
3699
3700 if (!basic_block) {
3701 basic_block = CreateBasicBlockWithDexPC(dex_pc);
3702 basic_blocks_[dex_pc] = basic_block;
3703 }
3704
3705 return basic_block;
3706}
3707
3708
3709llvm::BasicBlock*
3710MethodCompiler::GetNextBasicBlock(uint32_t dex_pc) {
3711 Instruction const* insn = Instruction::At(code_item_->insns_ + dex_pc);
3712 return GetBasicBlock(dex_pc + insn->SizeInCodeUnits());
3713}
3714
3715
Logan Chien5bcc04e2012-01-30 14:15:12 +08003716int32_t MethodCompiler::GetTryItemOffset(uint32_t dex_pc) {
3717 // TODO: Since we are emitting the dex instructions in ascending order
3718 // w.r.t. address, we can cache the lastest try item offset so that we
3719 // don't have to do binary search for every query.
3720
3721 int32_t min = 0;
3722 int32_t max = code_item_->tries_size_ - 1;
3723
3724 while (min <= max) {
3725 int32_t mid = min + (max - min) / 2;
3726
3727 DexFile::TryItem const* ti = DexFile::GetTryItems(*code_item_, mid);
3728 uint32_t start = ti->start_addr_;
3729 uint32_t end = start + ti->insn_count_;
3730
3731 if (dex_pc < start) {
3732 max = mid - 1;
3733 } else if (dex_pc >= end) {
3734 min = mid + 1;
3735 } else {
3736 return mid; // found
3737 }
3738 }
3739
3740 return -1; // not found
3741}
3742
3743
3744llvm::BasicBlock* MethodCompiler::GetLandingPadBasicBlock(uint32_t dex_pc) {
3745 // Find the try item for this address in this method
3746 int32_t ti_offset = GetTryItemOffset(dex_pc);
3747
3748 if (ti_offset == -1) {
3749 return NULL; // No landing pad is available for this address.
3750 }
3751
3752 // Check for the existing landing pad basic block
3753 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
3754 llvm::BasicBlock* block_lpad = basic_block_landing_pads_[ti_offset];
3755
3756 if (block_lpad) {
3757 // We have generated landing pad for this try item already. Return the
3758 // same basic block.
3759 return block_lpad;
3760 }
3761
3762 // Get try item from code item
3763 DexFile::TryItem const* ti = DexFile::GetTryItems(*code_item_, ti_offset);
3764
3765 // Create landing pad basic block
3766 block_lpad = llvm::BasicBlock::Create(*context_,
3767 StringPrintf("lpad%d", ti_offset),
3768 func_);
3769
3770 // Change IRBuilder insert point
3771 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3772 irb_.SetInsertPoint(block_lpad);
3773
3774 // Find catch block with matching type
3775 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
3776
3777 // TODO: Maybe passing try item offset will be a better idea? For now,
3778 // we are passing dex_pc, so that we can use existing runtime support
3779 // function directly. However, in the runtime supporting function we
3780 // have to search for try item with binary search which can be
3781 // eliminated.
3782 llvm::Value* dex_pc_value = irb_.getInt32(ti->start_addr_);
3783
3784 llvm::Value* catch_handler_index_value =
3785 irb_.CreateCall2(irb_.GetRuntime(FindCatchBlock),
3786 method_object_addr, dex_pc_value);
3787
3788 // Switch instruction (Go to unwind basic block by default)
3789 llvm::SwitchInst* sw =
3790 irb_.CreateSwitch(catch_handler_index_value, GetUnwindBasicBlock());
3791
3792 // Cases with matched catch block
3793 CatchHandlerIterator iter(*code_item_, ti->start_addr_);
3794
3795 for (uint32_t c = 0; iter.HasNext(); iter.Next(), ++c) {
3796 sw->addCase(irb_.getInt32(c), GetBasicBlock(iter.GetHandlerAddress()));
3797 }
3798
3799 // Restore the orignal insert point for IRBuilder
3800 irb_.restoreIP(irb_ip_original);
3801
3802 // Cache this landing pad
3803 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
3804 basic_block_landing_pads_[ti_offset] = block_lpad;
3805
3806 return block_lpad;
3807}
3808
3809
3810llvm::BasicBlock* MethodCompiler::GetUnwindBasicBlock() {
3811 // Check the existing unwinding baisc block block
3812 if (basic_block_unwind_ != NULL) {
3813 return basic_block_unwind_;
3814 }
3815
3816 // Create new basic block for unwinding
3817 basic_block_unwind_ =
3818 llvm::BasicBlock::Create(*context_, "exception_unwind", func_);
3819
3820 // Change IRBuilder insert point
3821 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3822 irb_.SetInsertPoint(basic_block_unwind_);
3823
Logan Chien8dfcbea2012-02-17 18:50:32 +08003824 // Pop the shadow frame
3825 EmitPopShadowFrame();
3826
Logan Chien5bcc04e2012-01-30 14:15:12 +08003827 // Emit the code to return default value (zero) for the given return type.
Logan Chiendd361c92012-04-10 23:40:37 +08003828 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
Logan Chien5bcc04e2012-01-30 14:15:12 +08003829 if (ret_shorty == 'V') {
3830 irb_.CreateRetVoid();
3831 } else {
3832 irb_.CreateRet(irb_.getJZero(ret_shorty));
3833 }
3834
3835 // Restore the orignal insert point for IRBuilder
3836 irb_.restoreIP(irb_ip_original);
3837
3838 return basic_block_unwind_;
3839}
3840
3841
Logan Chienc670a8d2011-12-20 21:25:56 +08003842llvm::Value* MethodCompiler::AllocDalvikLocalVarReg(RegCategory cat,
3843 uint32_t reg_idx) {
3844
3845 // Save current IR builder insert point
3846 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3847
3848 // Alloca
3849 llvm::Value* reg_addr = NULL;
3850
3851 switch (cat) {
3852 case kRegCat1nr:
3853 irb_.SetInsertPoint(basic_block_reg_alloca_);
3854 reg_addr = irb_.CreateAlloca(irb_.getJIntTy(), 0,
3855 StringPrintf("r%u", reg_idx));
3856
3857 irb_.SetInsertPoint(basic_block_reg_zero_init_);
3858 irb_.CreateStore(irb_.getJInt(0), reg_addr);
3859 break;
3860
3861 case kRegCat2:
3862 irb_.SetInsertPoint(basic_block_reg_alloca_);
3863 reg_addr = irb_.CreateAlloca(irb_.getJLongTy(), 0,
3864 StringPrintf("w%u", reg_idx));
3865
3866 irb_.SetInsertPoint(basic_block_reg_zero_init_);
3867 irb_.CreateStore(irb_.getJLong(0), reg_addr);
3868 break;
3869
3870 case kRegObject:
Logan Chien8dfcbea2012-02-17 18:50:32 +08003871 {
3872 irb_.SetInsertPoint(basic_block_shadow_frame_alloca_);
Logan Chienc670a8d2011-12-20 21:25:56 +08003873
Logan Chien8dfcbea2012-02-17 18:50:32 +08003874 llvm::Value* gep_index[] = {
3875 irb_.getInt32(0), // No pointer displacement
3876 irb_.getInt32(1), // SIRT
3877 irb_.getInt32(reg_idx) // Pointer field
3878 };
3879
3880 reg_addr = irb_.CreateGEP(shadow_frame_, gep_index,
3881 StringPrintf("p%u", reg_idx));
3882
3883 irb_.SetInsertPoint(basic_block_reg_zero_init_);
3884 irb_.CreateStore(irb_.getJNull(), reg_addr);
3885 }
Logan Chienc670a8d2011-12-20 21:25:56 +08003886 break;
3887
3888 default:
3889 LOG(FATAL) << "Unknown register category for allocation: " << cat;
3890 }
3891
3892 // Restore IRBuilder insert point
3893 irb_.restoreIP(irb_ip_original);
3894
3895 DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL));
3896 return reg_addr;
3897}
3898
3899
3900llvm::Value* MethodCompiler::AllocDalvikRetValReg(RegCategory cat) {
3901 // Save current IR builder insert point
3902 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3903
3904 // Alloca
3905 llvm::Value* reg_addr = NULL;
3906
3907 switch (cat) {
3908 case kRegCat1nr:
3909 irb_.SetInsertPoint(basic_block_reg_alloca_);
3910 reg_addr = irb_.CreateAlloca(irb_.getJIntTy(), 0, "r_res");
3911 break;
3912
3913 case kRegCat2:
3914 irb_.SetInsertPoint(basic_block_reg_alloca_);
3915 reg_addr = irb_.CreateAlloca(irb_.getJLongTy(), 0, "w_res");
3916 break;
3917
3918 case kRegObject:
3919 irb_.SetInsertPoint(basic_block_reg_alloca_);
3920 reg_addr = irb_.CreateAlloca(irb_.getJObjectTy(), 0, "p_res");
3921 break;
3922
3923 default:
3924 LOG(FATAL) << "Unknown register category for allocation: " << cat;
3925 }
3926
3927 // Restore IRBuilder insert point
3928 irb_.restoreIP(irb_ip_original);
3929
3930 DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL));
3931 return reg_addr;
3932}
3933
3934
Logan Chien8dfcbea2012-02-17 18:50:32 +08003935void MethodCompiler::EmitPopShadowFrame() {
3936 irb_.CreateCall(irb_.GetRuntime(PopShadowFrame));
3937}
3938
3939
3940void MethodCompiler::EmitUpdateLineNum(int32_t line_num) {
Logan Chien1b0a1b72012-03-15 06:20:17 +08003941 llvm::Value* line_num_field_addr =
3942 irb_.CreatePtrDisp(shadow_frame_,
3943 irb_.getPtrEquivInt(ShadowFrame::LineNumOffset()),
3944 irb_.getJIntTy()->getPointerTo());
Logan Chien8dfcbea2012-02-17 18:50:32 +08003945
Logan Chien8dfcbea2012-02-17 18:50:32 +08003946 llvm::ConstantInt* line_num_value = irb_.getInt32(line_num);
3947 irb_.CreateStore(line_num_value, line_num_field_addr);
3948}
3949
3950
3951void MethodCompiler::EmitUpdateLineNumFromDexPC(uint32_t dex_pc) {
Logan Chiendd361c92012-04-10 23:40:37 +08003952 EmitUpdateLineNum(
3953 dex_file_->GetLineNumFromPC(oat_compilation_unit_->IsStatic(),
3954 method_idx_, code_item_, dex_pc));
Logan Chien8dfcbea2012-02-17 18:50:32 +08003955}
3956
3957
Logan Chien83426162011-12-09 09:29:50 +08003958} // namespace compiler_llvm
3959} // namespace art