blob: 556dd510a754ed7e1950614741457c9e1869dabd [file] [log] [blame]
Shih-wei Liaod1fec812012-02-13 09:51:10 -08001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "method_compiler.h"
18
Logan Chienfca7e872011-12-20 20:08:22 +080019#include "backend_types.h"
Logan Chien8b977d32012-02-21 19:14:55 +080020#include "compilation_unit.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080021#include "compiler.h"
Logan Chiena78e3c82011-12-27 17:59:35 +080022#include "inferred_reg_category_map.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080023#include "ir_builder.h"
24#include "logging.h"
Logan Chien4dd96f52012-02-29 01:26:58 +080025#include "oat_compilation_unit.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080026#include "object.h"
27#include "object_utils.h"
Logan Chien42e0e152012-01-13 15:42:36 +080028#include "runtime_support_func.h"
TDYa1275bb86012012-04-11 05:57:28 -070029#include "runtime_support_llvm.h"
Logan Chien1b0a1b72012-03-15 06:20:17 +080030#include "shadow_frame.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080031#include "stl_util.h"
Logan Chien0b827102011-12-20 19:46:14 +080032#include "stringprintf.h"
33#include "utils_llvm.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070034#include "verifier/method_verifier.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080035
36#include <iomanip>
37
38#include <llvm/Analysis/Verifier.h>
Logan Chienc670a8d2011-12-20 21:25:56 +080039#include <llvm/BasicBlock.h>
Shih-wei Liaod1fec812012-02-13 09:51:10 -080040#include <llvm/Function.h>
Logan Chiena85fb2f2012-01-16 12:52:56 +080041#include <llvm/GlobalVariable.h>
42#include <llvm/Intrinsics.h>
Shih-wei Liaod1fec812012-02-13 09:51:10 -080043
Logan Chien83426162011-12-09 09:29:50 +080044namespace art {
45namespace compiler_llvm {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080046
Logan Chien42e0e152012-01-13 15:42:36 +080047using namespace runtime_support;
48
Shih-wei Liaod1fec812012-02-13 09:51:10 -080049
Logan Chien8b977d32012-02-21 19:14:55 +080050MethodCompiler::MethodCompiler(CompilationUnit* cunit,
Logan Chien83426162011-12-09 09:29:50 +080051 Compiler* compiler,
Logan Chien4dd96f52012-02-29 01:26:58 +080052 OatCompilationUnit* oat_compilation_unit)
Logan Chien8b977d32012-02-21 19:14:55 +080053 : cunit_(cunit), compiler_(compiler),
54 class_linker_(oat_compilation_unit->class_linker_),
55 class_loader_(oat_compilation_unit->class_loader_),
56 dex_file_(oat_compilation_unit->dex_file_),
57 dex_cache_(oat_compilation_unit->dex_cache_),
58 code_item_(oat_compilation_unit->code_item_),
59 oat_compilation_unit_(oat_compilation_unit),
Logan Chien8b977d32012-02-21 19:14:55 +080060 method_idx_(oat_compilation_unit->method_idx_),
61 access_flags_(oat_compilation_unit->access_flags_),
62 module_(cunit->GetModule()),
63 context_(cunit->GetLLVMContext()),
64 irb_(*cunit->GetIRBuilder()), func_(NULL), retval_reg_(NULL),
Ian Rogers776ac1f2012-04-13 23:36:36 -070065 basic_block_stack_overflow_(NULL),
Logan Chien8b977d32012-02-21 19:14:55 +080066 basic_block_reg_alloca_(NULL), basic_block_shadow_frame_alloca_(NULL),
67 basic_block_reg_zero_init_(NULL), basic_block_reg_arg_init_(NULL),
68 basic_blocks_(code_item_->insns_size_in_code_units_),
69 basic_block_landing_pads_(code_item_->tries_size_, NULL),
70 basic_block_unwind_(NULL), basic_block_unreachable_(NULL),
Logan Chien937105a2012-04-02 02:37:37 +080071 shadow_frame_(NULL), elf_func_idx_(cunit_->AcquireUniqueElfFuncIndex()) {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080072}
73
74
75MethodCompiler::~MethodCompiler() {
Logan Chienc670a8d2011-12-20 21:25:56 +080076 STLDeleteElements(&regs_);
Shih-wei Liaod1fec812012-02-13 09:51:10 -080077}
78
79
Logan Chien0b827102011-12-20 19:46:14 +080080void MethodCompiler::CreateFunction() {
81 // LLVM function name
Logan Chien937105a2012-04-02 02:37:37 +080082 std::string func_name(ElfFuncName(elf_func_idx_));
Logan Chien0b827102011-12-20 19:46:14 +080083
84 // Get function type
85 llvm::FunctionType* func_type =
Logan Chiendd361c92012-04-10 23:40:37 +080086 GetFunctionType(method_idx_, oat_compilation_unit_->IsStatic());
Logan Chien0b827102011-12-20 19:46:14 +080087
88 // Create function
89 func_ = llvm::Function::Create(func_type, llvm::Function::ExternalLinkage,
90 func_name, module_);
91
92 // Set argument name
93 llvm::Function::arg_iterator arg_iter(func_->arg_begin());
94 llvm::Function::arg_iterator arg_end(func_->arg_end());
95
96 DCHECK_NE(arg_iter, arg_end);
97 arg_iter->setName("method");
98 ++arg_iter;
99
Logan Chiendd361c92012-04-10 23:40:37 +0800100 if (!oat_compilation_unit_->IsStatic()) {
Logan Chien0b827102011-12-20 19:46:14 +0800101 DCHECK_NE(arg_iter, arg_end);
102 arg_iter->setName("this");
103 ++arg_iter;
104 }
105
106 for (unsigned i = 0; arg_iter != arg_end; ++i, ++arg_iter) {
107 arg_iter->setName(StringPrintf("a%u", i));
108 }
109}
110
111
112llvm::FunctionType* MethodCompiler::GetFunctionType(uint32_t method_idx,
113 bool is_static) {
114 // Get method signature
115 DexFile::MethodId const& method_id = dex_file_->GetMethodId(method_idx);
116
Logan Chien8faf8022012-02-24 12:25:29 +0800117 uint32_t shorty_size;
Logan Chien0b827102011-12-20 19:46:14 +0800118 char const* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size);
Logan Chien8faf8022012-02-24 12:25:29 +0800119 CHECK_GE(shorty_size, 1u);
Logan Chien0b827102011-12-20 19:46:14 +0800120
121 // Get return type
122 llvm::Type* ret_type = irb_.getJType(shorty[0], kAccurate);
123
124 // Get argument type
125 std::vector<llvm::Type*> args_type;
126
127 args_type.push_back(irb_.getJObjectTy()); // method object pointer
128
129 if (!is_static) {
130 args_type.push_back(irb_.getJType('L', kAccurate)); // "this" object pointer
131 }
132
Logan Chien8faf8022012-02-24 12:25:29 +0800133 for (uint32_t i = 1; i < shorty_size; ++i) {
Logan Chien0b827102011-12-20 19:46:14 +0800134 args_type.push_back(irb_.getJType(shorty[i], kAccurate));
135 }
136
137 return llvm::FunctionType::get(ret_type, args_type, false);
138}
139
140
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800141void MethodCompiler::EmitPrologue() {
Logan Chienc670a8d2011-12-20 21:25:56 +0800142 // Create basic blocks for prologue
TDYa1274165a832012-04-03 17:47:16 -0700143 basic_block_stack_overflow_ =
144 llvm::BasicBlock::Create(*context_, "prologue.stack_overflow_check", func_);
145
Logan Chienc670a8d2011-12-20 21:25:56 +0800146 basic_block_reg_alloca_ =
147 llvm::BasicBlock::Create(*context_, "prologue.alloca", func_);
148
Logan Chien8dfcbea2012-02-17 18:50:32 +0800149 basic_block_shadow_frame_alloca_ =
150 llvm::BasicBlock::Create(*context_, "prologue.shadowframe", func_);
151
Logan Chienc670a8d2011-12-20 21:25:56 +0800152 basic_block_reg_zero_init_ =
153 llvm::BasicBlock::Create(*context_, "prologue.zeroinit", func_);
154
Logan Chiend6ececa2011-12-27 16:20:15 +0800155 basic_block_reg_arg_init_ =
156 llvm::BasicBlock::Create(*context_, "prologue.arginit", func_);
157
TDYa1274165a832012-04-03 17:47:16 -0700158 // Before alloca, check stack overflow.
159 EmitStackOverflowCheck();
160
Logan Chienc670a8d2011-12-20 21:25:56 +0800161 // Create register array
162 for (uint16_t r = 0; r < code_item_->registers_size_; ++r) {
163 regs_.push_back(DalvikReg::CreateLocalVarReg(*this, r));
164 }
165
166 retval_reg_.reset(DalvikReg::CreateRetValReg(*this));
Logan Chiend6ececa2011-12-27 16:20:15 +0800167
Logan Chien8dfcbea2012-02-17 18:50:32 +0800168 // Create Shadow Frame
169 EmitPrologueAllocShadowFrame();
170
Logan Chiend6ececa2011-12-27 16:20:15 +0800171 // Store argument to dalvik register
172 irb_.SetInsertPoint(basic_block_reg_arg_init_);
173 EmitPrologueAssignArgRegister();
174
175 // Branch to start address
176 irb_.CreateBr(GetBasicBlock(0));
Logan Chienc670a8d2011-12-20 21:25:56 +0800177}
178
179
TDYa1274165a832012-04-03 17:47:16 -0700180void MethodCompiler::EmitStackOverflowCheck() {
181 irb_.SetInsertPoint(basic_block_stack_overflow_);
182
183 // Call llvm intrinsic function to get frame address.
184 llvm::Function* frameaddress =
185 llvm::Intrinsic::getDeclaration(module_, llvm::Intrinsic::frameaddress);
186
187 // The type of llvm::frameaddress is: i8* @llvm.frameaddress(i32)
188 llvm::Value* frame_address = irb_.CreateCall(frameaddress, irb_.getInt32(0));
189
190 // Cast i8* to int
191 frame_address = irb_.CreatePtrToInt(frame_address, irb_.getPtrEquivIntTy());
192
193 // Get thread.stack_end_
194 llvm::Value* thread_object_addr =
195 irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
196
197 llvm::Value* stack_end_addr =
198 irb_.CreatePtrDisp(thread_object_addr,
199 irb_.getPtrEquivInt(Thread::StackEndOffset().Int32Value()),
200 irb_.getPtrEquivIntTy()->getPointerTo());
201
202 llvm::Value* stack_end = irb_.CreateLoad(stack_end_addr);
203
204 // Check the frame address < thread.stack_end_ ?
205 llvm::Value* is_stack_overflow = irb_.CreateICmpULT(frame_address, stack_end);
206
207 llvm::BasicBlock* block_exception =
208 llvm::BasicBlock::Create(*context_, "stack_overflow", func_);
209
210 llvm::BasicBlock* block_continue =
211 llvm::BasicBlock::Create(*context_, "stack_overflow_cont", func_);
212
213 irb_.CreateCondBr(is_stack_overflow, block_exception, block_continue);
214
215 // If stack overflow, throw exception.
216 irb_.SetInsertPoint(block_exception);
217 irb_.CreateCall(irb_.GetRuntime(ThrowStackOverflowException));
218
219 // Unwind.
Logan Chiendd361c92012-04-10 23:40:37 +0800220 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
TDYa1274165a832012-04-03 17:47:16 -0700221 if (ret_shorty == 'V') {
222 irb_.CreateRetVoid();
223 } else {
224 irb_.CreateRet(irb_.getJZero(ret_shorty));
225 }
226
227 basic_block_stack_overflow_ = block_continue;
228}
229
230
Logan Chienc670a8d2011-12-20 21:25:56 +0800231void MethodCompiler::EmitPrologueLastBranch() {
TDYa1274165a832012-04-03 17:47:16 -0700232 irb_.SetInsertPoint(basic_block_stack_overflow_);
233 irb_.CreateBr(basic_block_reg_alloca_);
234
Logan Chienc670a8d2011-12-20 21:25:56 +0800235 irb_.SetInsertPoint(basic_block_reg_alloca_);
Logan Chien8dfcbea2012-02-17 18:50:32 +0800236 irb_.CreateBr(basic_block_shadow_frame_alloca_);
237
238 irb_.SetInsertPoint(basic_block_shadow_frame_alloca_);
Logan Chienc670a8d2011-12-20 21:25:56 +0800239 irb_.CreateBr(basic_block_reg_zero_init_);
240
241 irb_.SetInsertPoint(basic_block_reg_zero_init_);
Logan Chiend6ececa2011-12-27 16:20:15 +0800242 irb_.CreateBr(basic_block_reg_arg_init_);
243}
244
245
Logan Chien8dfcbea2012-02-17 18:50:32 +0800246void MethodCompiler::EmitPrologueAllocShadowFrame() {
247 irb_.SetInsertPoint(basic_block_shadow_frame_alloca_);
248
249 // Allocate the shadow frame now!
250 uint32_t sirt_size = code_item_->registers_size_;
251 // TODO: registers_size_ is a bad approximation. Compute a
252 // tighter approximation at Dex verifier while performing data-flow
253 // analysis.
254
255 llvm::StructType* shadow_frame_type = irb_.getShadowFrameTy(sirt_size);
256 shadow_frame_ = irb_.CreateAlloca(shadow_frame_type);
257
258 // Zero-initialization of the shadow frame
259 llvm::ConstantAggregateZero* zero_initializer =
260 llvm::ConstantAggregateZero::get(shadow_frame_type);
261
262 irb_.CreateStore(zero_initializer, shadow_frame_);
263
Logan Chien8dfcbea2012-02-17 18:50:32 +0800264 // Store the method pointer
Logan Chien1b0a1b72012-03-15 06:20:17 +0800265 llvm::Value* method_field_addr =
266 irb_.CreatePtrDisp(shadow_frame_,
267 irb_.getPtrEquivInt(ShadowFrame::MethodOffset()),
268 irb_.getJObjectTy()->getPointerTo());
269
Logan Chien8dfcbea2012-02-17 18:50:32 +0800270 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
271 irb_.CreateStore(method_object_addr, method_field_addr);
272
273 // Store the number of the pointer slots
Logan Chien1b0a1b72012-03-15 06:20:17 +0800274 llvm::ConstantInt* num_of_refs_offset =
275 irb_.getPtrEquivInt(ShadowFrame::NumberOfReferencesOffset());
276
277 llvm::Value* num_of_refs_field_addr =
278 irb_.CreatePtrDisp(shadow_frame_, num_of_refs_offset,
279 irb_.getJIntTy()->getPointerTo());
280
281 llvm::ConstantInt* num_of_refs_value = irb_.getJInt(sirt_size);
282 irb_.CreateStore(num_of_refs_value, num_of_refs_field_addr);
Logan Chien8dfcbea2012-02-17 18:50:32 +0800283
284 // Push the shadow frame
285 llvm::Value* shadow_frame_upcast =
286 irb_.CreateConstGEP2_32(shadow_frame_, 0, 0);
287
288 irb_.CreateCall(irb_.GetRuntime(PushShadowFrame), shadow_frame_upcast);
289}
290
291
Logan Chiend6ececa2011-12-27 16:20:15 +0800292void MethodCompiler::EmitPrologueAssignArgRegister() {
293 uint16_t arg_reg = code_item_->registers_size_ - code_item_->ins_size_;
294
295 llvm::Function::arg_iterator arg_iter(func_->arg_begin());
296 llvm::Function::arg_iterator arg_end(func_->arg_end());
297
Logan Chiendd361c92012-04-10 23:40:37 +0800298 uint32_t shorty_size = 0;
299 char const* shorty = oat_compilation_unit_->GetShorty(&shorty_size);
300 CHECK_GE(shorty_size, 1u);
Logan Chiend6ececa2011-12-27 16:20:15 +0800301
302 ++arg_iter; // skip method object
303
Logan Chiendd361c92012-04-10 23:40:37 +0800304 if (!oat_compilation_unit_->IsStatic()) {
Logan Chiend6ececa2011-12-27 16:20:15 +0800305 EmitStoreDalvikReg(arg_reg, kObject, kAccurate, arg_iter);
306 ++arg_iter;
307 ++arg_reg;
308 }
309
Logan Chiendd361c92012-04-10 23:40:37 +0800310 for (uint32_t i = 1; i < shorty_size; ++i, ++arg_iter) {
Logan Chiend6ececa2011-12-27 16:20:15 +0800311 EmitStoreDalvikReg(arg_reg, shorty[i], kAccurate, arg_iter);
312
313 ++arg_reg;
314 if (shorty[i] == 'J' || shorty[i] == 'D') {
315 // Wide types, such as long and double, are using a pair of registers
316 // to store the value, so we have to increase arg_reg again.
317 ++arg_reg;
318 }
319 }
320
321 DCHECK_EQ(arg_end, arg_iter);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800322}
323
324
Logan Chien83426162011-12-09 09:29:50 +0800325void MethodCompiler::EmitInstructions() {
Logan Chiend6c239a2011-12-23 15:11:45 +0800326 uint32_t dex_pc = 0;
327 while (dex_pc < code_item_->insns_size_in_code_units_) {
328 Instruction const* insn = Instruction::At(code_item_->insns_ + dex_pc);
329 EmitInstruction(dex_pc, insn);
330 dex_pc += insn->SizeInCodeUnits();
331 }
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800332}
333
334
Logan Chien83426162011-12-09 09:29:50 +0800335void MethodCompiler::EmitInstruction(uint32_t dex_pc,
336 Instruction const* insn) {
Logan Chiend6c239a2011-12-23 15:11:45 +0800337
338 // Set the IRBuilder insertion point
339 irb_.SetInsertPoint(GetBasicBlock(dex_pc));
340
Logan Chien70f94b42011-12-27 17:49:11 +0800341#define ARGS dex_pc, insn
342
343 // Dispatch the instruction
344 switch (insn->Opcode()) {
345 case Instruction::NOP:
346 EmitInsn_Nop(ARGS);
347 break;
348
349 case Instruction::MOVE:
350 case Instruction::MOVE_FROM16:
351 case Instruction::MOVE_16:
352 EmitInsn_Move(ARGS, kInt);
353 break;
354
355 case Instruction::MOVE_WIDE:
356 case Instruction::MOVE_WIDE_FROM16:
357 case Instruction::MOVE_WIDE_16:
358 EmitInsn_Move(ARGS, kLong);
359 break;
360
361 case Instruction::MOVE_OBJECT:
362 case Instruction::MOVE_OBJECT_FROM16:
363 case Instruction::MOVE_OBJECT_16:
364 EmitInsn_Move(ARGS, kObject);
365 break;
366
367 case Instruction::MOVE_RESULT:
368 EmitInsn_MoveResult(ARGS, kInt);
369 break;
370
371 case Instruction::MOVE_RESULT_WIDE:
372 EmitInsn_MoveResult(ARGS, kLong);
373 break;
374
375 case Instruction::MOVE_RESULT_OBJECT:
376 EmitInsn_MoveResult(ARGS, kObject);
377 break;
378
379 case Instruction::MOVE_EXCEPTION:
380 EmitInsn_MoveException(ARGS);
381 break;
382
383 case Instruction::RETURN_VOID:
384 EmitInsn_ReturnVoid(ARGS);
385 break;
386
387 case Instruction::RETURN:
388 case Instruction::RETURN_WIDE:
389 case Instruction::RETURN_OBJECT:
390 EmitInsn_Return(ARGS);
391 break;
392
393 case Instruction::CONST_4:
394 case Instruction::CONST_16:
395 case Instruction::CONST:
396 case Instruction::CONST_HIGH16:
397 EmitInsn_LoadConstant(ARGS, kInt);
398 break;
399
400 case Instruction::CONST_WIDE_16:
401 case Instruction::CONST_WIDE_32:
402 case Instruction::CONST_WIDE:
403 case Instruction::CONST_WIDE_HIGH16:
404 EmitInsn_LoadConstant(ARGS, kLong);
405 break;
406
407 case Instruction::CONST_STRING:
408 case Instruction::CONST_STRING_JUMBO:
409 EmitInsn_LoadConstantString(ARGS);
410 break;
411
412 case Instruction::CONST_CLASS:
413 EmitInsn_LoadConstantClass(ARGS);
414 break;
415
416 case Instruction::MONITOR_ENTER:
417 EmitInsn_MonitorEnter(ARGS);
418 break;
419
420 case Instruction::MONITOR_EXIT:
421 EmitInsn_MonitorExit(ARGS);
422 break;
423
424 case Instruction::CHECK_CAST:
425 EmitInsn_CheckCast(ARGS);
426 break;
427
428 case Instruction::INSTANCE_OF:
429 EmitInsn_InstanceOf(ARGS);
430 break;
431
432 case Instruction::ARRAY_LENGTH:
433 EmitInsn_ArrayLength(ARGS);
434 break;
435
436 case Instruction::NEW_INSTANCE:
437 EmitInsn_NewInstance(ARGS);
438 break;
439
440 case Instruction::NEW_ARRAY:
441 EmitInsn_NewArray(ARGS);
442 break;
443
444 case Instruction::FILLED_NEW_ARRAY:
445 EmitInsn_FilledNewArray(ARGS, false);
446 break;
447
448 case Instruction::FILLED_NEW_ARRAY_RANGE:
449 EmitInsn_FilledNewArray(ARGS, true);
450 break;
451
452 case Instruction::FILL_ARRAY_DATA:
453 EmitInsn_FillArrayData(ARGS);
454 break;
455
456 case Instruction::THROW:
457 EmitInsn_ThrowException(ARGS);
458 break;
459
460 case Instruction::GOTO:
461 case Instruction::GOTO_16:
462 case Instruction::GOTO_32:
463 EmitInsn_UnconditionalBranch(ARGS);
464 break;
465
466 case Instruction::PACKED_SWITCH:
467 EmitInsn_PackedSwitch(ARGS);
468 break;
469
470 case Instruction::SPARSE_SWITCH:
471 EmitInsn_SparseSwitch(ARGS);
472 break;
473
474 case Instruction::CMPL_FLOAT:
475 EmitInsn_FPCompare(ARGS, kFloat, false);
476 break;
477
478 case Instruction::CMPG_FLOAT:
479 EmitInsn_FPCompare(ARGS, kFloat, true);
480 break;
481
482 case Instruction::CMPL_DOUBLE:
483 EmitInsn_FPCompare(ARGS, kDouble, false);
484 break;
485
486 case Instruction::CMPG_DOUBLE:
487 EmitInsn_FPCompare(ARGS, kDouble, true);
488 break;
489
490 case Instruction::CMP_LONG:
491 EmitInsn_LongCompare(ARGS);
492 break;
493
494 case Instruction::IF_EQ:
495 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_EQ);
496 break;
497
498 case Instruction::IF_NE:
499 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_NE);
500 break;
501
502 case Instruction::IF_LT:
503 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_LT);
504 break;
505
506 case Instruction::IF_GE:
507 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_GE);
508 break;
509
510 case Instruction::IF_GT:
511 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_GT);
512 break;
513
514 case Instruction::IF_LE:
515 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_LE);
516 break;
517
518 case Instruction::IF_EQZ:
519 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_EQ);
520 break;
521
522 case Instruction::IF_NEZ:
523 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_NE);
524 break;
525
526 case Instruction::IF_LTZ:
527 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_LT);
528 break;
529
530 case Instruction::IF_GEZ:
531 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_GE);
532 break;
533
534 case Instruction::IF_GTZ:
535 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_GT);
536 break;
537
538 case Instruction::IF_LEZ:
539 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_LE);
540 break;
541
542 case Instruction::AGET:
543 EmitInsn_AGet(ARGS, kInt);
544 break;
545
546 case Instruction::AGET_WIDE:
547 EmitInsn_AGet(ARGS, kLong);
548 break;
549
550 case Instruction::AGET_OBJECT:
551 EmitInsn_AGet(ARGS, kObject);
552 break;
553
554 case Instruction::AGET_BOOLEAN:
555 EmitInsn_AGet(ARGS, kBoolean);
556 break;
557
558 case Instruction::AGET_BYTE:
559 EmitInsn_AGet(ARGS, kByte);
560 break;
561
562 case Instruction::AGET_CHAR:
563 EmitInsn_AGet(ARGS, kChar);
564 break;
565
566 case Instruction::AGET_SHORT:
567 EmitInsn_AGet(ARGS, kShort);
568 break;
569
570 case Instruction::APUT:
571 EmitInsn_APut(ARGS, kInt);
572 break;
573
574 case Instruction::APUT_WIDE:
575 EmitInsn_APut(ARGS, kLong);
576 break;
577
578 case Instruction::APUT_OBJECT:
579 EmitInsn_APut(ARGS, kObject);
580 break;
581
582 case Instruction::APUT_BOOLEAN:
583 EmitInsn_APut(ARGS, kBoolean);
584 break;
585
586 case Instruction::APUT_BYTE:
587 EmitInsn_APut(ARGS, kByte);
588 break;
589
590 case Instruction::APUT_CHAR:
591 EmitInsn_APut(ARGS, kChar);
592 break;
593
594 case Instruction::APUT_SHORT:
595 EmitInsn_APut(ARGS, kShort);
596 break;
597
598 case Instruction::IGET:
599 EmitInsn_IGet(ARGS, kInt);
600 break;
601
602 case Instruction::IGET_WIDE:
603 EmitInsn_IGet(ARGS, kLong);
604 break;
605
606 case Instruction::IGET_OBJECT:
607 EmitInsn_IGet(ARGS, kObject);
608 break;
609
610 case Instruction::IGET_BOOLEAN:
611 EmitInsn_IGet(ARGS, kBoolean);
612 break;
613
614 case Instruction::IGET_BYTE:
615 EmitInsn_IGet(ARGS, kByte);
616 break;
617
618 case Instruction::IGET_CHAR:
619 EmitInsn_IGet(ARGS, kChar);
620 break;
621
622 case Instruction::IGET_SHORT:
623 EmitInsn_IGet(ARGS, kShort);
624 break;
625
626 case Instruction::IPUT:
627 EmitInsn_IPut(ARGS, kInt);
628 break;
629
630 case Instruction::IPUT_WIDE:
631 EmitInsn_IPut(ARGS, kLong);
632 break;
633
634 case Instruction::IPUT_OBJECT:
635 EmitInsn_IPut(ARGS, kObject);
636 break;
637
638 case Instruction::IPUT_BOOLEAN:
639 EmitInsn_IPut(ARGS, kBoolean);
640 break;
641
642 case Instruction::IPUT_BYTE:
643 EmitInsn_IPut(ARGS, kByte);
644 break;
645
646 case Instruction::IPUT_CHAR:
647 EmitInsn_IPut(ARGS, kChar);
648 break;
649
650 case Instruction::IPUT_SHORT:
651 EmitInsn_IPut(ARGS, kShort);
652 break;
653
654 case Instruction::SGET:
655 EmitInsn_SGet(ARGS, kInt);
656 break;
657
658 case Instruction::SGET_WIDE:
659 EmitInsn_SGet(ARGS, kLong);
660 break;
661
662 case Instruction::SGET_OBJECT:
663 EmitInsn_SGet(ARGS, kObject);
664 break;
665
666 case Instruction::SGET_BOOLEAN:
667 EmitInsn_SGet(ARGS, kBoolean);
668 break;
669
670 case Instruction::SGET_BYTE:
671 EmitInsn_SGet(ARGS, kByte);
672 break;
673
674 case Instruction::SGET_CHAR:
675 EmitInsn_SGet(ARGS, kChar);
676 break;
677
678 case Instruction::SGET_SHORT:
679 EmitInsn_SGet(ARGS, kShort);
680 break;
681
682 case Instruction::SPUT:
683 EmitInsn_SPut(ARGS, kInt);
684 break;
685
686 case Instruction::SPUT_WIDE:
687 EmitInsn_SPut(ARGS, kLong);
688 break;
689
690 case Instruction::SPUT_OBJECT:
691 EmitInsn_SPut(ARGS, kObject);
692 break;
693
694 case Instruction::SPUT_BOOLEAN:
695 EmitInsn_SPut(ARGS, kBoolean);
696 break;
697
698 case Instruction::SPUT_BYTE:
699 EmitInsn_SPut(ARGS, kByte);
700 break;
701
702 case Instruction::SPUT_CHAR:
703 EmitInsn_SPut(ARGS, kChar);
704 break;
705
706 case Instruction::SPUT_SHORT:
707 EmitInsn_SPut(ARGS, kShort);
708 break;
709
710
711 case Instruction::INVOKE_VIRTUAL:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800712 EmitInsn_Invoke(ARGS, kVirtual, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800713 break;
714
715 case Instruction::INVOKE_SUPER:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800716 EmitInsn_Invoke(ARGS, kSuper, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800717 break;
718
719 case Instruction::INVOKE_DIRECT:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800720 EmitInsn_Invoke(ARGS, kDirect, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800721 break;
722
723 case Instruction::INVOKE_STATIC:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800724 EmitInsn_Invoke(ARGS, kStatic, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800725 break;
726
727 case Instruction::INVOKE_INTERFACE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800728 EmitInsn_Invoke(ARGS, kInterface, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800729 break;
730
731 case Instruction::INVOKE_VIRTUAL_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800732 EmitInsn_Invoke(ARGS, kVirtual, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800733 break;
734
735 case Instruction::INVOKE_SUPER_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800736 EmitInsn_Invoke(ARGS, kSuper, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800737 break;
738
739 case Instruction::INVOKE_DIRECT_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800740 EmitInsn_Invoke(ARGS, kDirect, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800741 break;
742
743 case Instruction::INVOKE_STATIC_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800744 EmitInsn_Invoke(ARGS, kStatic, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800745 break;
746
747 case Instruction::INVOKE_INTERFACE_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800748 EmitInsn_Invoke(ARGS, kInterface, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800749 break;
750
751 case Instruction::NEG_INT:
752 EmitInsn_Neg(ARGS, kInt);
753 break;
754
755 case Instruction::NOT_INT:
756 EmitInsn_Not(ARGS, kInt);
757 break;
758
759 case Instruction::NEG_LONG:
760 EmitInsn_Neg(ARGS, kLong);
761 break;
762
763 case Instruction::NOT_LONG:
764 EmitInsn_Not(ARGS, kLong);
765 break;
766
767 case Instruction::NEG_FLOAT:
768 EmitInsn_FNeg(ARGS, kFloat);
769 break;
770
771 case Instruction::NEG_DOUBLE:
772 EmitInsn_FNeg(ARGS, kDouble);
773 break;
774
775 case Instruction::INT_TO_LONG:
776 EmitInsn_SExt(ARGS);
777 break;
778
779 case Instruction::INT_TO_FLOAT:
780 EmitInsn_IntToFP(ARGS, kInt, kFloat);
781 break;
782
783 case Instruction::INT_TO_DOUBLE:
784 EmitInsn_IntToFP(ARGS, kInt, kDouble);
785 break;
786
787 case Instruction::LONG_TO_INT:
788 EmitInsn_Trunc(ARGS);
789 break;
790
791 case Instruction::LONG_TO_FLOAT:
792 EmitInsn_IntToFP(ARGS, kLong, kFloat);
793 break;
794
795 case Instruction::LONG_TO_DOUBLE:
796 EmitInsn_IntToFP(ARGS, kLong, kDouble);
797 break;
798
799 case Instruction::FLOAT_TO_INT:
TDYa127a4746872012-04-11 23:48:55 -0700800 EmitInsn_FPToInt(ARGS, kFloat, kInt, F2I);
Logan Chien70f94b42011-12-27 17:49:11 +0800801 break;
802
803 case Instruction::FLOAT_TO_LONG:
TDYa127a4746872012-04-11 23:48:55 -0700804 EmitInsn_FPToInt(ARGS, kFloat, kLong, F2L);
Logan Chien70f94b42011-12-27 17:49:11 +0800805 break;
806
807 case Instruction::FLOAT_TO_DOUBLE:
808 EmitInsn_FExt(ARGS);
809 break;
810
811 case Instruction::DOUBLE_TO_INT:
TDYa127a4746872012-04-11 23:48:55 -0700812 EmitInsn_FPToInt(ARGS, kDouble, kInt, D2I);
Logan Chien70f94b42011-12-27 17:49:11 +0800813 break;
814
815 case Instruction::DOUBLE_TO_LONG:
TDYa127a4746872012-04-11 23:48:55 -0700816 EmitInsn_FPToInt(ARGS, kDouble, kLong, D2L);
Logan Chien70f94b42011-12-27 17:49:11 +0800817 break;
818
819 case Instruction::DOUBLE_TO_FLOAT:
820 EmitInsn_FTrunc(ARGS);
821 break;
822
823 case Instruction::INT_TO_BYTE:
824 EmitInsn_TruncAndSExt(ARGS, 8);
825 break;
826
827 case Instruction::INT_TO_CHAR:
828 EmitInsn_TruncAndZExt(ARGS, 16);
829 break;
830
831 case Instruction::INT_TO_SHORT:
832 EmitInsn_TruncAndSExt(ARGS, 16);
833 break;
834
835 case Instruction::ADD_INT:
836 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kInt, false);
837 break;
838
839 case Instruction::SUB_INT:
840 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kInt, false);
841 break;
842
843 case Instruction::MUL_INT:
844 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kInt, false);
845 break;
846
847 case Instruction::DIV_INT:
848 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kInt, false);
849 break;
850
851 case Instruction::REM_INT:
852 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kInt, false);
853 break;
854
855 case Instruction::AND_INT:
856 EmitInsn_IntArithm(ARGS, kIntArithm_And, kInt, false);
857 break;
858
859 case Instruction::OR_INT:
860 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kInt, false);
861 break;
862
863 case Instruction::XOR_INT:
864 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kInt, false);
865 break;
866
867 case Instruction::SHL_INT:
Logan Chien5539ad02012-04-02 14:36:55 +0800868 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kInt, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800869 break;
870
871 case Instruction::SHR_INT:
Logan Chien5539ad02012-04-02 14:36:55 +0800872 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kInt, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800873 break;
874
875 case Instruction::USHR_INT:
Logan Chien5539ad02012-04-02 14:36:55 +0800876 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kInt, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800877 break;
878
879 case Instruction::ADD_LONG:
880 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kLong, false);
881 break;
882
883 case Instruction::SUB_LONG:
884 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kLong, false);
885 break;
886
887 case Instruction::MUL_LONG:
888 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kLong, false);
889 break;
890
891 case Instruction::DIV_LONG:
892 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kLong, false);
893 break;
894
895 case Instruction::REM_LONG:
896 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kLong, false);
897 break;
898
899 case Instruction::AND_LONG:
900 EmitInsn_IntArithm(ARGS, kIntArithm_And, kLong, false);
901 break;
902
903 case Instruction::OR_LONG:
904 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kLong, false);
905 break;
906
907 case Instruction::XOR_LONG:
908 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kLong, false);
909 break;
910
911 case Instruction::SHL_LONG:
Logan Chien5539ad02012-04-02 14:36:55 +0800912 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kLong, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800913 break;
914
915 case Instruction::SHR_LONG:
Logan Chien5539ad02012-04-02 14:36:55 +0800916 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kLong, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800917 break;
918
919 case Instruction::USHR_LONG:
Logan Chien5539ad02012-04-02 14:36:55 +0800920 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kLong, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800921 break;
922
923 case Instruction::ADD_FLOAT:
924 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kFloat, false);
925 break;
926
927 case Instruction::SUB_FLOAT:
928 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kFloat, false);
929 break;
930
931 case Instruction::MUL_FLOAT:
932 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kFloat, false);
933 break;
934
935 case Instruction::DIV_FLOAT:
936 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kFloat, false);
937 break;
938
939 case Instruction::REM_FLOAT:
940 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kFloat, false);
941 break;
942
943 case Instruction::ADD_DOUBLE:
944 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kDouble, false);
945 break;
946
947 case Instruction::SUB_DOUBLE:
948 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kDouble, false);
949 break;
950
951 case Instruction::MUL_DOUBLE:
952 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kDouble, false);
953 break;
954
955 case Instruction::DIV_DOUBLE:
956 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kDouble, false);
957 break;
958
959 case Instruction::REM_DOUBLE:
960 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kDouble, false);
961 break;
962
963 case Instruction::ADD_INT_2ADDR:
964 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kInt, true);
965 break;
966
967 case Instruction::SUB_INT_2ADDR:
968 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kInt, true);
969 break;
970
971 case Instruction::MUL_INT_2ADDR:
972 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kInt, true);
973 break;
974
975 case Instruction::DIV_INT_2ADDR:
976 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kInt, true);
977 break;
978
979 case Instruction::REM_INT_2ADDR:
980 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kInt, true);
981 break;
982
983 case Instruction::AND_INT_2ADDR:
984 EmitInsn_IntArithm(ARGS, kIntArithm_And, kInt, true);
985 break;
986
987 case Instruction::OR_INT_2ADDR:
988 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kInt, true);
989 break;
990
991 case Instruction::XOR_INT_2ADDR:
992 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kInt, true);
993 break;
994
995 case Instruction::SHL_INT_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +0800996 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kInt, true);
Logan Chien70f94b42011-12-27 17:49:11 +0800997 break;
998
999 case Instruction::SHR_INT_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001000 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kInt, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001001 break;
1002
1003 case Instruction::USHR_INT_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001004 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kInt, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001005 break;
1006
1007 case Instruction::ADD_LONG_2ADDR:
1008 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kLong, true);
1009 break;
1010
1011 case Instruction::SUB_LONG_2ADDR:
1012 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kLong, true);
1013 break;
1014
1015 case Instruction::MUL_LONG_2ADDR:
1016 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kLong, true);
1017 break;
1018
1019 case Instruction::DIV_LONG_2ADDR:
1020 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kLong, true);
1021 break;
1022
1023 case Instruction::REM_LONG_2ADDR:
1024 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kLong, true);
1025 break;
1026
1027 case Instruction::AND_LONG_2ADDR:
1028 EmitInsn_IntArithm(ARGS, kIntArithm_And, kLong, true);
1029 break;
1030
1031 case Instruction::OR_LONG_2ADDR:
1032 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kLong, true);
1033 break;
1034
1035 case Instruction::XOR_LONG_2ADDR:
1036 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kLong, true);
1037 break;
1038
1039 case Instruction::SHL_LONG_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001040 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kLong, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001041 break;
1042
1043 case Instruction::SHR_LONG_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001044 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kLong, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001045 break;
1046
1047 case Instruction::USHR_LONG_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001048 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kLong, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001049 break;
1050
1051 case Instruction::ADD_FLOAT_2ADDR:
1052 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kFloat, true);
1053 break;
1054
1055 case Instruction::SUB_FLOAT_2ADDR:
1056 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kFloat, true);
1057 break;
1058
1059 case Instruction::MUL_FLOAT_2ADDR:
1060 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kFloat, true);
1061 break;
1062
1063 case Instruction::DIV_FLOAT_2ADDR:
1064 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kFloat, true);
1065 break;
1066
1067 case Instruction::REM_FLOAT_2ADDR:
1068 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kFloat, true);
1069 break;
1070
1071 case Instruction::ADD_DOUBLE_2ADDR:
1072 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kDouble, true);
1073 break;
1074
1075 case Instruction::SUB_DOUBLE_2ADDR:
1076 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kDouble, true);
1077 break;
1078
1079 case Instruction::MUL_DOUBLE_2ADDR:
1080 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kDouble, true);
1081 break;
1082
1083 case Instruction::DIV_DOUBLE_2ADDR:
1084 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kDouble, true);
1085 break;
1086
1087 case Instruction::REM_DOUBLE_2ADDR:
1088 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kDouble, true);
1089 break;
1090
1091 case Instruction::ADD_INT_LIT16:
1092 case Instruction::ADD_INT_LIT8:
1093 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Add);
1094 break;
1095
1096 case Instruction::RSUB_INT:
1097 case Instruction::RSUB_INT_LIT8:
1098 EmitInsn_RSubImmediate(ARGS);
1099 break;
1100
1101 case Instruction::MUL_INT_LIT16:
1102 case Instruction::MUL_INT_LIT8:
1103 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Mul);
1104 break;
1105
1106 case Instruction::DIV_INT_LIT16:
1107 case Instruction::DIV_INT_LIT8:
1108 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Div);
1109 break;
1110
1111 case Instruction::REM_INT_LIT16:
1112 case Instruction::REM_INT_LIT8:
1113 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Rem);
1114 break;
1115
1116 case Instruction::AND_INT_LIT16:
1117 case Instruction::AND_INT_LIT8:
1118 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_And);
1119 break;
1120
1121 case Instruction::OR_INT_LIT16:
1122 case Instruction::OR_INT_LIT8:
1123 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Or);
1124 break;
1125
1126 case Instruction::XOR_INT_LIT16:
1127 case Instruction::XOR_INT_LIT8:
1128 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Xor);
1129 break;
1130
1131 case Instruction::SHL_INT_LIT8:
Logan Chien5539ad02012-04-02 14:36:55 +08001132 EmitInsn_IntShiftArithmImmediate(ARGS, kIntArithm_Shl);
Logan Chien70f94b42011-12-27 17:49:11 +08001133 break;
1134
1135 case Instruction::SHR_INT_LIT8:
Logan Chien5539ad02012-04-02 14:36:55 +08001136 EmitInsn_IntShiftArithmImmediate(ARGS, kIntArithm_Shr);
Logan Chien70f94b42011-12-27 17:49:11 +08001137 break;
1138
1139 case Instruction::USHR_INT_LIT8:
Logan Chien5539ad02012-04-02 14:36:55 +08001140 EmitInsn_IntShiftArithmImmediate(ARGS, kIntArithm_UShr);
Logan Chien70f94b42011-12-27 17:49:11 +08001141 break;
1142
Logan Chien9e5f5c12012-04-10 13:51:45 +08001143 case Instruction::THROW_VERIFICATION_ERROR:
1144 EmitInsn_ThrowVerificationError(ARGS);
1145 break;
1146
Logan Chien70f94b42011-12-27 17:49:11 +08001147 case Instruction::UNUSED_3E:
1148 case Instruction::UNUSED_3F:
1149 case Instruction::UNUSED_40:
1150 case Instruction::UNUSED_41:
1151 case Instruction::UNUSED_42:
1152 case Instruction::UNUSED_43:
1153 case Instruction::UNUSED_73:
1154 case Instruction::UNUSED_79:
1155 case Instruction::UNUSED_7A:
1156 case Instruction::UNUSED_E3:
1157 case Instruction::UNUSED_E4:
1158 case Instruction::UNUSED_E5:
1159 case Instruction::UNUSED_E6:
1160 case Instruction::UNUSED_E7:
1161 case Instruction::UNUSED_E8:
1162 case Instruction::UNUSED_E9:
1163 case Instruction::UNUSED_EA:
1164 case Instruction::UNUSED_EB:
1165 case Instruction::UNUSED_EC:
Logan Chien70f94b42011-12-27 17:49:11 +08001166 case Instruction::UNUSED_EE:
1167 case Instruction::UNUSED_EF:
1168 case Instruction::UNUSED_F0:
1169 case Instruction::UNUSED_F1:
1170 case Instruction::UNUSED_F2:
1171 case Instruction::UNUSED_F3:
1172 case Instruction::UNUSED_F4:
1173 case Instruction::UNUSED_F5:
1174 case Instruction::UNUSED_F6:
1175 case Instruction::UNUSED_F7:
1176 case Instruction::UNUSED_F8:
1177 case Instruction::UNUSED_F9:
1178 case Instruction::UNUSED_FA:
1179 case Instruction::UNUSED_FB:
1180 case Instruction::UNUSED_FC:
1181 case Instruction::UNUSED_FD:
1182 case Instruction::UNUSED_FE:
1183 case Instruction::UNUSED_FF:
1184 LOG(FATAL) << "Dex file contains UNUSED bytecode: " << insn->Opcode();
1185 break;
1186 }
1187
1188#undef ARGS
1189}
1190
1191
1192void MethodCompiler::EmitInsn_Nop(uint32_t dex_pc,
1193 Instruction const* insn) {
Logan Chiene09a6b72011-12-27 17:50:21 +08001194
1195 uint16_t insn_signature = code_item_->insns_[dex_pc];
1196
1197 if (insn_signature == Instruction::kPackedSwitchSignature ||
1198 insn_signature == Instruction::kSparseSwitchSignature ||
1199 insn_signature == Instruction::kArrayDataSignature) {
1200 irb_.CreateUnreachable();
Elliott Hughesb25c3f62012-03-26 16:35:06 -07001201 } else {
Logan Chiene09a6b72011-12-27 17:50:21 +08001202 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1203 }
Shih-wei Liaod1fec812012-02-13 09:51:10 -08001204}
1205
1206
Logan Chien70f94b42011-12-27 17:49:11 +08001207void MethodCompiler::EmitInsn_Move(uint32_t dex_pc,
1208 Instruction const* insn,
1209 JType jty) {
Logan Chien48173132011-12-27 17:51:13 +08001210
Elliott Hughesadb8c672012-03-06 16:49:32 -08001211 DecodedInstruction dec_insn(insn);
Logan Chien48173132011-12-27 17:51:13 +08001212
Elliott Hughesadb8c672012-03-06 16:49:32 -08001213 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, jty, kReg);
1214 EmitStoreDalvikReg(dec_insn.vA, jty, kReg, src_value);
Logan Chien48173132011-12-27 17:51:13 +08001215
Logan Chien70f94b42011-12-27 17:49:11 +08001216 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1217}
1218
1219
1220void MethodCompiler::EmitInsn_MoveResult(uint32_t dex_pc,
1221 Instruction const* insn,
1222 JType jty) {
Logan Chien48173132011-12-27 17:51:13 +08001223
Elliott Hughesadb8c672012-03-06 16:49:32 -08001224 DecodedInstruction dec_insn(insn);
Logan Chien48173132011-12-27 17:51:13 +08001225
1226 llvm::Value* src_value = EmitLoadDalvikRetValReg(jty, kReg);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001227 EmitStoreDalvikReg(dec_insn.vA, jty, kReg, src_value);
Logan Chien48173132011-12-27 17:51:13 +08001228
Logan Chien70f94b42011-12-27 17:49:11 +08001229 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1230}
1231
1232
1233void MethodCompiler::EmitInsn_MoveException(uint32_t dex_pc,
1234 Instruction const* insn) {
Logan Chien3354cec2012-01-13 14:29:03 +08001235
Elliott Hughesadb8c672012-03-06 16:49:32 -08001236 DecodedInstruction dec_insn(insn);
Logan Chien3354cec2012-01-13 14:29:03 +08001237
1238 // Get thread-local exception field address
1239 llvm::Constant* exception_field_offset =
1240 irb_.getPtrEquivInt(Thread::ExceptionOffset().Int32Value());
1241
1242 llvm::Value* thread_object_addr =
1243 irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1244
1245 llvm::Value* exception_field_addr =
1246 irb_.CreatePtrDisp(thread_object_addr, exception_field_offset,
1247 irb_.getJObjectTy()->getPointerTo());
1248
1249 // Get exception object address
1250 llvm::Value* exception_object_addr = irb_.CreateLoad(exception_field_addr);
1251
1252 // Set thread-local exception field address to NULL
1253 irb_.CreateStore(irb_.getJNull(), exception_field_addr);
1254
1255 // Keep the exception object in the Dalvik register
Elliott Hughesadb8c672012-03-06 16:49:32 -08001256 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, exception_object_addr);
Logan Chien3354cec2012-01-13 14:29:03 +08001257
Logan Chien70f94b42011-12-27 17:49:11 +08001258 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1259}
1260
1261
1262void MethodCompiler::EmitInsn_ThrowException(uint32_t dex_pc,
1263 Instruction const* insn) {
Logan Chien6c6f12d2012-01-13 19:26:27 +08001264
Elliott Hughesadb8c672012-03-06 16:49:32 -08001265 DecodedInstruction dec_insn(insn);
Logan Chien6c6f12d2012-01-13 19:26:27 +08001266
1267 llvm::Value* exception_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001268 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chien6c6f12d2012-01-13 19:26:27 +08001269
Logan Chien8dfcbea2012-02-17 18:50:32 +08001270 EmitUpdateLineNumFromDexPC(dex_pc);
1271
Logan Chien6c6f12d2012-01-13 19:26:27 +08001272 irb_.CreateCall(irb_.GetRuntime(ThrowException), exception_addr);
1273
1274 EmitBranchExceptionLandingPad(dex_pc);
Logan Chien70f94b42011-12-27 17:49:11 +08001275}
1276
1277
Logan Chien9e5f5c12012-04-10 13:51:45 +08001278void MethodCompiler::EmitInsn_ThrowVerificationError(uint32_t dex_pc,
1279 Instruction const* insn) {
1280
1281 DecodedInstruction dec_insn(insn);
1282
1283 EmitUpdateLineNumFromDexPC(dex_pc);
1284
1285 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1286 llvm::Value* kind_value = irb_.getInt32(dec_insn.vA);
1287 llvm::Value* ref_value = irb_.getInt32(dec_insn.vB);
1288
1289 irb_.CreateCall3(irb_.GetRuntime(ThrowVerificationError),
1290 method_object_addr, kind_value, ref_value);
1291
1292 EmitBranchExceptionLandingPad(dex_pc);
1293}
1294
1295
Logan Chien70f94b42011-12-27 17:49:11 +08001296void MethodCompiler::EmitInsn_ReturnVoid(uint32_t dex_pc,
1297 Instruction const* insn) {
Logan Chien8898a272011-12-27 17:51:56 +08001298 // Garbage collection safe-point
1299 EmitGuard_GarbageCollectionSuspend(dex_pc);
1300
Logan Chien8dfcbea2012-02-17 18:50:32 +08001301 // Pop the shadow frame
1302 EmitPopShadowFrame();
1303
Logan Chien8898a272011-12-27 17:51:56 +08001304 // Return!
1305 irb_.CreateRetVoid();
Logan Chien70f94b42011-12-27 17:49:11 +08001306}
1307
1308
1309void MethodCompiler::EmitInsn_Return(uint32_t dex_pc,
1310 Instruction const* insn) {
Logan Chien8898a272011-12-27 17:51:56 +08001311
Elliott Hughesadb8c672012-03-06 16:49:32 -08001312 DecodedInstruction dec_insn(insn);
Logan Chien8898a272011-12-27 17:51:56 +08001313
1314 // Garbage collection safe-point
1315 EmitGuard_GarbageCollectionSuspend(dex_pc);
1316
Logan Chien8dfcbea2012-02-17 18:50:32 +08001317 // Pop the shadow frame
1318 EmitPopShadowFrame();
1319 // NOTE: It is important to keep this AFTER the GC safe-point. Otherwise,
1320 // the return value might be collected since the shadow stack is popped.
1321
Logan Chien8898a272011-12-27 17:51:56 +08001322 // Return!
Logan Chiendd361c92012-04-10 23:40:37 +08001323 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
Elliott Hughesadb8c672012-03-06 16:49:32 -08001324 llvm::Value* retval = EmitLoadDalvikReg(dec_insn.vA, ret_shorty, kAccurate);
Logan Chien8898a272011-12-27 17:51:56 +08001325
1326 irb_.CreateRet(retval);
Logan Chien70f94b42011-12-27 17:49:11 +08001327}
1328
1329
1330void MethodCompiler::EmitInsn_LoadConstant(uint32_t dex_pc,
1331 Instruction const* insn,
1332 JType imm_jty) {
Shih-wei Liao798366e2012-02-16 09:25:33 -08001333
Elliott Hughesadb8c672012-03-06 16:49:32 -08001334 DecodedInstruction dec_insn(insn);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001335
1336 DCHECK(imm_jty == kInt || imm_jty == kLong) << imm_jty;
1337
1338 int64_t imm = 0;
1339
1340 switch (insn->Opcode()) {
1341 // 32-bit Immediate
1342 case Instruction::CONST_4:
1343 case Instruction::CONST_16:
1344 case Instruction::CONST:
1345 case Instruction::CONST_WIDE_16:
1346 case Instruction::CONST_WIDE_32:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001347 imm = static_cast<int64_t>(static_cast<int32_t>(dec_insn.vB));
Shih-wei Liao798366e2012-02-16 09:25:33 -08001348 break;
1349
1350 case Instruction::CONST_HIGH16:
1351 imm = static_cast<int64_t>(static_cast<int32_t>(
Elliott Hughesadb8c672012-03-06 16:49:32 -08001352 static_cast<uint32_t>(static_cast<uint16_t>(dec_insn.vB)) << 16));
Shih-wei Liao798366e2012-02-16 09:25:33 -08001353 break;
1354
1355 // 64-bit Immediate
1356 case Instruction::CONST_WIDE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001357 imm = static_cast<int64_t>(dec_insn.vB_wide);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001358 break;
1359
1360 case Instruction::CONST_WIDE_HIGH16:
1361 imm = static_cast<int64_t>(
Elliott Hughesadb8c672012-03-06 16:49:32 -08001362 static_cast<uint64_t>(static_cast<uint16_t>(dec_insn.vB)) << 48);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001363 break;
1364
1365 // Unknown opcode for load constant (unreachable)
1366 default:
1367 LOG(FATAL) << "Unknown opcode for load constant: " << insn->Opcode();
1368 break;
1369 }
1370
1371 // Store the non-object register
1372 llvm::Type* imm_type = irb_.getJType(imm_jty, kAccurate);
1373 llvm::Constant* imm_value = llvm::ConstantInt::getSigned(imm_type, imm);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001374 EmitStoreDalvikReg(dec_insn.vA, imm_jty, kAccurate, imm_value);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001375
1376 // Store the object register if it is possible to be null.
1377 if (imm_jty == kInt && imm == 0) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001378 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, irb_.getJNull());
Shih-wei Liao798366e2012-02-16 09:25:33 -08001379 }
1380
Logan Chien70f94b42011-12-27 17:49:11 +08001381 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1382}
1383
1384
1385void MethodCompiler::EmitInsn_LoadConstantString(uint32_t dex_pc,
1386 Instruction const* insn) {
Logan Chienc3b4ba12012-01-16 19:52:53 +08001387
Elliott Hughesadb8c672012-03-06 16:49:32 -08001388 DecodedInstruction dec_insn(insn);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001389
Elliott Hughesadb8c672012-03-06 16:49:32 -08001390 uint32_t string_idx = dec_insn.vB;
Logan Chienc3b4ba12012-01-16 19:52:53 +08001391
1392 llvm::Value* string_field_addr = EmitLoadDexCacheStringFieldAddr(string_idx);
1393
1394 llvm::Value* string_addr = irb_.CreateLoad(string_field_addr);
1395
1396 if (!compiler_->CanAssumeStringIsPresentInDexCache(dex_cache_, string_idx)) {
1397 llvm::BasicBlock* block_str_exist =
1398 CreateBasicBlockWithDexPC(dex_pc, "str_exist");
1399
1400 llvm::BasicBlock* block_str_resolve =
1401 CreateBasicBlockWithDexPC(dex_pc, "str_resolve");
1402
1403 // Test: Is the string resolved and in the dex cache?
1404 llvm::Value* equal_null = irb_.CreateICmpEQ(string_addr, irb_.getJNull());
1405
TDYa127a849cb62012-04-01 05:59:34 -07001406 irb_.CreateCondBr(equal_null, block_str_resolve, block_str_exist);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001407
1408 // String is resolved, go to next basic block.
1409 irb_.SetInsertPoint(block_str_exist);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001410 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, string_addr);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001411 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1412
1413 // String is not resolved yet, resolve it now.
1414 irb_.SetInsertPoint(block_str_resolve);
1415
1416 llvm::Function* runtime_func = irb_.GetRuntime(ResolveString);
1417
1418 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1419
1420 llvm::Value* string_idx_value = irb_.getInt32(string_idx);
1421
Logan Chien8dfcbea2012-02-17 18:50:32 +08001422 EmitUpdateLineNumFromDexPC(dex_pc);
1423
1424 string_addr = irb_.CreateCall2(runtime_func, method_object_addr,
1425 string_idx_value);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001426
1427 EmitGuard_ExceptionLandingPad(dex_pc);
1428 }
1429
1430 // Store the string object to the Dalvik register
Elliott Hughesadb8c672012-03-06 16:49:32 -08001431 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, string_addr);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001432
Logan Chien70f94b42011-12-27 17:49:11 +08001433 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1434}
1435
1436
Logan Chien27b30252012-01-14 03:43:35 +08001437llvm::Value* MethodCompiler::EmitLoadConstantClass(uint32_t dex_pc,
1438 uint32_t type_idx) {
1439 if (!compiler_->CanAccessTypeWithoutChecks(method_idx_, dex_cache_,
1440 *dex_file_, type_idx)) {
1441 llvm::Value* type_idx_value = irb_.getInt32(type_idx);
1442
1443 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1444
1445 llvm::Function* runtime_func =
1446 irb_.GetRuntime(InitializeTypeAndVerifyAccess);
1447
Logan Chien8dfcbea2012-02-17 18:50:32 +08001448 EmitUpdateLineNumFromDexPC(dex_pc);
1449
Logan Chien27b30252012-01-14 03:43:35 +08001450 llvm::Value* type_object_addr =
1451 irb_.CreateCall2(runtime_func, type_idx_value, method_object_addr);
1452
1453 EmitGuard_ExceptionLandingPad(dex_pc);
1454
1455 return type_object_addr;
1456
1457 } else {
1458 // Try to load the class (type) object from the test cache.
1459 llvm::Value* type_field_addr =
1460 EmitLoadDexCacheResolvedTypeFieldAddr(type_idx);
1461
1462 llvm::Value* type_object_addr = irb_.CreateLoad(type_field_addr);
1463
1464 if (compiler_->CanAssumeTypeIsPresentInDexCache(dex_cache_, type_idx)) {
1465 return type_object_addr;
1466 }
1467
1468 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
1469
1470 // Test whether class (type) object is in the dex cache or not
1471 llvm::Value* equal_null =
1472 irb_.CreateICmpEQ(type_object_addr, irb_.getJNull());
1473
1474 llvm::BasicBlock* block_cont =
1475 CreateBasicBlockWithDexPC(dex_pc, "cont");
1476
1477 llvm::BasicBlock* block_load_class =
1478 CreateBasicBlockWithDexPC(dex_pc, "load_class");
1479
1480 irb_.CreateCondBr(equal_null, block_load_class, block_cont);
1481
1482 // Failback routine to load the class object
1483 irb_.SetInsertPoint(block_load_class);
1484
1485 llvm::Function* runtime_func = irb_.GetRuntime(InitializeType);
1486
1487 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
1488
1489 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1490
Logan Chien8dfcbea2012-02-17 18:50:32 +08001491 EmitUpdateLineNumFromDexPC(dex_pc);
1492
Logan Chien27b30252012-01-14 03:43:35 +08001493 llvm::Value* loaded_type_object_addr =
1494 irb_.CreateCall2(runtime_func, type_idx_value, method_object_addr);
1495
1496 EmitGuard_ExceptionLandingPad(dex_pc);
1497
1498 llvm::BasicBlock* block_after_load_class = irb_.GetInsertBlock();
1499
1500 irb_.CreateBr(block_cont);
1501
1502 // Now the class object must be loaded
1503 irb_.SetInsertPoint(block_cont);
1504
1505 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1506
1507 phi->addIncoming(type_object_addr, block_original);
1508 phi->addIncoming(loaded_type_object_addr, block_after_load_class);
1509
1510 return phi;
1511 }
1512}
1513
1514
Logan Chien70f94b42011-12-27 17:49:11 +08001515void MethodCompiler::EmitInsn_LoadConstantClass(uint32_t dex_pc,
1516 Instruction const* insn) {
Logan Chien27b30252012-01-14 03:43:35 +08001517
Elliott Hughesadb8c672012-03-06 16:49:32 -08001518 DecodedInstruction dec_insn(insn);
Logan Chien27b30252012-01-14 03:43:35 +08001519
Elliott Hughesadb8c672012-03-06 16:49:32 -08001520 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vB);
1521 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, type_object_addr);
Logan Chien27b30252012-01-14 03:43:35 +08001522
Logan Chien70f94b42011-12-27 17:49:11 +08001523 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1524}
1525
1526
1527void MethodCompiler::EmitInsn_MonitorEnter(uint32_t dex_pc,
1528 Instruction const* insn) {
Logan Chien9e0dbe42012-01-13 12:11:37 +08001529
Elliott Hughesadb8c672012-03-06 16:49:32 -08001530 DecodedInstruction dec_insn(insn);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001531
1532 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001533 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001534
1535 // TODO: Slow path always. May not need NullPointerException check.
1536 EmitGuard_NullPointerException(dex_pc, object_addr);
1537
Logan Chien8dfcbea2012-02-17 18:50:32 +08001538 EmitUpdateLineNumFromDexPC(dex_pc);
1539
Logan Chien9e0dbe42012-01-13 12:11:37 +08001540 irb_.CreateCall(irb_.GetRuntime(LockObject), object_addr);
1541 EmitGuard_ExceptionLandingPad(dex_pc);
1542
Logan Chien70f94b42011-12-27 17:49:11 +08001543 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1544}
1545
1546
1547void MethodCompiler::EmitInsn_MonitorExit(uint32_t dex_pc,
1548 Instruction const* insn) {
Logan Chien9e0dbe42012-01-13 12:11:37 +08001549
Elliott Hughesadb8c672012-03-06 16:49:32 -08001550 DecodedInstruction dec_insn(insn);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001551
1552 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001553 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001554
1555 EmitGuard_NullPointerException(dex_pc, object_addr);
1556
Logan Chien8dfcbea2012-02-17 18:50:32 +08001557 EmitUpdateLineNumFromDexPC(dex_pc);
1558
Logan Chien9e0dbe42012-01-13 12:11:37 +08001559 irb_.CreateCall(irb_.GetRuntime(UnlockObject), object_addr);
1560 EmitGuard_ExceptionLandingPad(dex_pc);
1561
Logan Chien70f94b42011-12-27 17:49:11 +08001562 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1563}
1564
1565
1566void MethodCompiler::EmitInsn_CheckCast(uint32_t dex_pc,
1567 Instruction const* insn) {
Logan Chienfc880952012-01-15 23:53:10 +08001568
Elliott Hughesadb8c672012-03-06 16:49:32 -08001569 DecodedInstruction dec_insn(insn);
Logan Chienfc880952012-01-15 23:53:10 +08001570
1571 llvm::BasicBlock* block_test_class =
1572 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1573
1574 llvm::BasicBlock* block_test_sub_class =
1575 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
1576
1577 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001578 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chienfc880952012-01-15 23:53:10 +08001579
1580 // Test: Is the reference equal to null? Act as no-op when it is null.
1581 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
1582
1583 irb_.CreateCondBr(equal_null,
1584 GetNextBasicBlock(dex_pc),
1585 block_test_class);
1586
1587 // Test: Is the object instantiated from the given class?
1588 irb_.SetInsertPoint(block_test_class);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001589 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vB);
Logan Chienfc880952012-01-15 23:53:10 +08001590 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1591
1592 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
1593
1594 llvm::Value* object_type_field_addr =
1595 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
1596
1597 llvm::Value* object_type_object_addr =
1598 irb_.CreateLoad(object_type_field_addr);
1599
1600 llvm::Value* equal_class =
1601 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
1602
1603 irb_.CreateCondBr(equal_class,
1604 GetNextBasicBlock(dex_pc),
1605 block_test_sub_class);
1606
1607 // Test: Is the object instantiated from the subclass of the given class?
1608 irb_.SetInsertPoint(block_test_sub_class);
1609
Logan Chien8dfcbea2012-02-17 18:50:32 +08001610 EmitUpdateLineNumFromDexPC(dex_pc);
1611
Logan Chienfc880952012-01-15 23:53:10 +08001612 irb_.CreateCall2(irb_.GetRuntime(CheckCast),
1613 type_object_addr, object_type_object_addr);
1614
1615 EmitGuard_ExceptionLandingPad(dex_pc);
1616
Logan Chien70f94b42011-12-27 17:49:11 +08001617 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1618}
1619
1620
1621void MethodCompiler::EmitInsn_InstanceOf(uint32_t dex_pc,
1622 Instruction const* insn) {
Logan Chien68725e22012-01-15 22:25:34 +08001623
Elliott Hughesadb8c672012-03-06 16:49:32 -08001624 DecodedInstruction dec_insn(insn);
Logan Chien68725e22012-01-15 22:25:34 +08001625
1626 llvm::Constant* zero = irb_.getJInt(0);
1627 llvm::Constant* one = irb_.getJInt(1);
1628
1629 llvm::BasicBlock* block_nullp = CreateBasicBlockWithDexPC(dex_pc, "nullp");
1630
1631 llvm::BasicBlock* block_test_class =
1632 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1633
1634 llvm::BasicBlock* block_class_equals =
1635 CreateBasicBlockWithDexPC(dex_pc, "class_eq");
1636
1637 llvm::BasicBlock* block_test_sub_class =
1638 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
1639
1640 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001641 EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
Logan Chien68725e22012-01-15 22:25:34 +08001642
1643 // Overview of the following code :
1644 // We check for null, if so, then false, otherwise check for class == . If so
1645 // then true, otherwise do callout slowpath.
1646 //
1647 // Test: Is the reference equal to null? Set 0 when it is null.
1648 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
1649
1650 irb_.CreateCondBr(equal_null, block_nullp, block_test_class);
1651
1652 irb_.SetInsertPoint(block_nullp);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001653 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, zero);
Logan Chien68725e22012-01-15 22:25:34 +08001654 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1655
1656 // Test: Is the object instantiated from the given class?
1657 irb_.SetInsertPoint(block_test_class);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001658 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vC);
Logan Chien68725e22012-01-15 22:25:34 +08001659 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1660
1661 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
1662
1663 llvm::Value* object_type_field_addr =
1664 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
1665
1666 llvm::Value* object_type_object_addr =
1667 irb_.CreateLoad(object_type_field_addr);
1668
1669 llvm::Value* equal_class =
1670 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
1671
1672 irb_.CreateCondBr(equal_class, block_class_equals, block_test_sub_class);
1673
1674 irb_.SetInsertPoint(block_class_equals);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001675 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, one);
Logan Chien68725e22012-01-15 22:25:34 +08001676 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1677
1678 // Test: Is the object instantiated from the subclass of the given class?
1679 irb_.SetInsertPoint(block_test_sub_class);
1680
1681 llvm::Value* result =
1682 irb_.CreateCall2(irb_.GetRuntime(IsAssignable),
1683 type_object_addr, object_type_object_addr);
1684
Elliott Hughesadb8c672012-03-06 16:49:32 -08001685 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result);
Logan Chien68725e22012-01-15 22:25:34 +08001686
Logan Chien70f94b42011-12-27 17:49:11 +08001687 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1688}
1689
1690
Logan Chien61bb6142012-02-03 15:34:53 +08001691llvm::Value* MethodCompiler::EmitLoadArrayLength(llvm::Value* array) {
1692 // Load array length field address
1693 llvm::Constant* array_len_field_offset =
1694 irb_.getPtrEquivInt(Array::LengthOffset().Int32Value());
1695
1696 llvm::Value* array_len_field_addr =
1697 irb_.CreatePtrDisp(array, array_len_field_offset,
1698 irb_.getJIntTy()->getPointerTo());
1699
1700 // Load array length
1701 return irb_.CreateLoad(array_len_field_addr);
1702}
1703
1704
Logan Chien70f94b42011-12-27 17:49:11 +08001705void MethodCompiler::EmitInsn_ArrayLength(uint32_t dex_pc,
1706 Instruction const* insn) {
Logan Chien61bb6142012-02-03 15:34:53 +08001707
Elliott Hughesadb8c672012-03-06 16:49:32 -08001708 DecodedInstruction dec_insn(insn);
Logan Chien61bb6142012-02-03 15:34:53 +08001709
1710 // Get the array object address
Elliott Hughesadb8c672012-03-06 16:49:32 -08001711 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
Logan Chien61bb6142012-02-03 15:34:53 +08001712 EmitGuard_NullPointerException(dex_pc, array_addr);
1713
1714 // Get the array length and store it to the register
1715 llvm::Value* array_len = EmitLoadArrayLength(array_addr);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001716 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, array_len);
Logan Chien61bb6142012-02-03 15:34:53 +08001717
Logan Chien70f94b42011-12-27 17:49:11 +08001718 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1719}
1720
1721
1722void MethodCompiler::EmitInsn_NewInstance(uint32_t dex_pc,
1723 Instruction const* insn) {
Logan Chien032bdad2012-01-16 09:59:23 +08001724
Elliott Hughesadb8c672012-03-06 16:49:32 -08001725 DecodedInstruction dec_insn(insn);
Logan Chien032bdad2012-01-16 09:59:23 +08001726
1727 llvm::Function* runtime_func;
Logan Chien1a032b12012-04-11 11:43:04 +08001728 if (compiler_->CanAccessInstantiableTypeWithoutChecks(
1729 method_idx_, dex_cache_, *dex_file_, dec_insn.vB)) {
Logan Chien032bdad2012-01-16 09:59:23 +08001730 runtime_func = irb_.GetRuntime(AllocObject);
1731 } else {
1732 runtime_func = irb_.GetRuntime(AllocObjectWithAccessCheck);
1733 }
1734
Elliott Hughesadb8c672012-03-06 16:49:32 -08001735 llvm::Constant* type_index_value = irb_.getInt32(dec_insn.vB);
Logan Chien032bdad2012-01-16 09:59:23 +08001736
1737 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1738
TDYa127da83d972012-04-18 00:21:49 -07001739 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1740
Logan Chien8dfcbea2012-02-17 18:50:32 +08001741 EmitUpdateLineNumFromDexPC(dex_pc);
1742
Logan Chien032bdad2012-01-16 09:59:23 +08001743 llvm::Value* object_addr =
TDYa127da83d972012-04-18 00:21:49 -07001744 irb_.CreateCall3(runtime_func, type_index_value, method_object_addr, thread_object_addr);
Logan Chien032bdad2012-01-16 09:59:23 +08001745
1746 EmitGuard_ExceptionLandingPad(dex_pc);
1747
Elliott Hughesadb8c672012-03-06 16:49:32 -08001748 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, object_addr);
Logan Chien032bdad2012-01-16 09:59:23 +08001749
Logan Chien70f94b42011-12-27 17:49:11 +08001750 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1751}
1752
1753
Logan Chiena2cc6a32012-01-16 10:38:41 +08001754llvm::Value* MethodCompiler::EmitAllocNewArray(uint32_t dex_pc,
1755 int32_t length,
1756 uint32_t type_idx,
1757 bool is_filled_new_array) {
1758 llvm::Function* runtime_func;
1759
1760 bool skip_access_check =
1761 compiler_->CanAccessTypeWithoutChecks(method_idx_, dex_cache_,
1762 *dex_file_, type_idx);
1763
TDYa127a849cb62012-04-01 05:59:34 -07001764 llvm::Value* array_length_value;
1765
Logan Chiena2cc6a32012-01-16 10:38:41 +08001766 if (is_filled_new_array) {
1767 runtime_func = skip_access_check ?
1768 irb_.GetRuntime(CheckAndAllocArray) :
1769 irb_.GetRuntime(CheckAndAllocArrayWithAccessCheck);
TDYa127a849cb62012-04-01 05:59:34 -07001770 array_length_value = irb_.getInt32(length);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001771 } else {
1772 runtime_func = skip_access_check ?
1773 irb_.GetRuntime(AllocArray) :
1774 irb_.GetRuntime(AllocArrayWithAccessCheck);
TDYa127a849cb62012-04-01 05:59:34 -07001775 array_length_value = EmitLoadDalvikReg(length, kInt, kAccurate);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001776 }
1777
1778 llvm::Constant* type_index_value = irb_.getInt32(type_idx);
1779
1780 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1781
TDYa127da83d972012-04-18 00:21:49 -07001782 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1783
Logan Chien8dfcbea2012-02-17 18:50:32 +08001784 EmitUpdateLineNumFromDexPC(dex_pc);
1785
Logan Chiena2cc6a32012-01-16 10:38:41 +08001786 llvm::Value* object_addr =
TDYa127da83d972012-04-18 00:21:49 -07001787 irb_.CreateCall4(runtime_func, type_index_value, method_object_addr,
1788 array_length_value, thread_object_addr);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001789
1790 EmitGuard_ExceptionLandingPad(dex_pc);
1791
1792 return object_addr;
1793}
1794
1795
Logan Chien70f94b42011-12-27 17:49:11 +08001796void MethodCompiler::EmitInsn_NewArray(uint32_t dex_pc,
1797 Instruction const* insn) {
Logan Chiena2cc6a32012-01-16 10:38:41 +08001798
Elliott Hughesadb8c672012-03-06 16:49:32 -08001799 DecodedInstruction dec_insn(insn);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001800
1801 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001802 EmitAllocNewArray(dex_pc, dec_insn.vB, dec_insn.vC, false);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001803
Elliott Hughesadb8c672012-03-06 16:49:32 -08001804 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, object_addr);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001805
Logan Chien70f94b42011-12-27 17:49:11 +08001806 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1807}
1808
1809
1810void MethodCompiler::EmitInsn_FilledNewArray(uint32_t dex_pc,
1811 Instruction const* insn,
1812 bool is_range) {
Logan Chiena85fb2f2012-01-16 12:52:56 +08001813
Elliott Hughesadb8c672012-03-06 16:49:32 -08001814 DecodedInstruction dec_insn(insn);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001815
1816 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001817 EmitAllocNewArray(dex_pc, dec_insn.vA, dec_insn.vB, true);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001818
Elliott Hughesadb8c672012-03-06 16:49:32 -08001819 if (dec_insn.vA > 0) {
Logan Chiendd361c92012-04-10 23:40:37 +08001820 // Resolve the element type
TDYa127183cf262012-04-11 07:53:21 -07001821 Class* klass = dex_cache_->GetResolvedType(dec_insn.vB)->GetComponentType();
Logan Chiendd361c92012-04-10 23:40:37 +08001822 // TODO: Avoid the usage of the dex_cache_. Try to figure out a better
1823 // way to distinguish [I and [L.
Logan Chiena85fb2f2012-01-16 12:52:56 +08001824 CHECK_NE(klass, static_cast<Class*>(NULL));
Logan Chiena85fb2f2012-01-16 12:52:56 +08001825
Logan Chiendd361c92012-04-10 23:40:37 +08001826 uint32_t alignment;
1827 llvm::Constant* elem_size;
1828 llvm::PointerType* field_type;
Logan Chiena85fb2f2012-01-16 12:52:56 +08001829
Logan Chiendd361c92012-04-10 23:40:37 +08001830 // NOTE: Currently filled-new-array only supports 'L', '[', and 'I'
1831 // as the element, thus we are only checking 2 cases: primitive int and
1832 // non-primitive type.
Logan Chiena85fb2f2012-01-16 12:52:56 +08001833 if (klass->IsPrimitiveInt()) {
Logan Chiendd361c92012-04-10 23:40:37 +08001834 alignment = sizeof(int32_t);
1835 elem_size = irb_.getPtrEquivInt(sizeof(int32_t));
Logan Chiena85fb2f2012-01-16 12:52:56 +08001836 field_type = irb_.getJIntTy()->getPointerTo();
1837 } else {
1838 CHECK(!klass->IsPrimitive());
Logan Chiendd361c92012-04-10 23:40:37 +08001839 alignment = irb_.getSizeOfPtrEquivInt();
1840 elem_size = irb_.getSizeOfPtrEquivIntValue();
Logan Chiena85fb2f2012-01-16 12:52:56 +08001841 field_type = irb_.getJObjectTy()->getPointerTo();
1842 }
1843
Logan Chiendd361c92012-04-10 23:40:37 +08001844 llvm::Value* data_field_offset =
1845 irb_.getPtrEquivInt(Array::DataOffset(alignment).Int32Value());
1846
1847 llvm::Value* data_field_addr =
1848 irb_.CreatePtrDisp(object_addr, data_field_offset, field_type);
1849
Logan Chiena85fb2f2012-01-16 12:52:56 +08001850 // TODO: Tune this code. Currently we are generating one instruction for
1851 // one element which may be very space consuming. Maybe changing to use
1852 // memcpy may help; however, since we can't guarantee that the alloca of
1853 // dalvik register are continuous, we can't perform such optimization yet.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001854 for (uint32_t i = 0; i < dec_insn.vA; ++i) {
Logan Chiena85fb2f2012-01-16 12:52:56 +08001855 int reg_index;
1856 if (is_range) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001857 reg_index = dec_insn.vC + i;
Logan Chiena85fb2f2012-01-16 12:52:56 +08001858 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001859 reg_index = dec_insn.arg[i];
Logan Chiena85fb2f2012-01-16 12:52:56 +08001860 }
1861
1862 llvm::Value* reg_value;
1863 if (klass->IsPrimitiveInt()) {
1864 reg_value = EmitLoadDalvikReg(reg_index, kInt, kAccurate);
1865 } else {
1866 reg_value = EmitLoadDalvikReg(reg_index, kObject, kAccurate);
1867 }
1868
1869 irb_.CreateStore(reg_value, data_field_addr);
1870
Logan Chiendd361c92012-04-10 23:40:37 +08001871 data_field_addr =
1872 irb_.CreatePtrDisp(data_field_addr, elem_size, field_type);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001873 }
1874 }
1875
1876 EmitStoreDalvikRetValReg(kObject, kAccurate, object_addr);
1877
Logan Chien70f94b42011-12-27 17:49:11 +08001878 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1879}
1880
1881
1882void MethodCompiler::EmitInsn_FillArrayData(uint32_t dex_pc,
1883 Instruction const* insn) {
Logan Chiene58b6582012-01-16 17:13:13 +08001884
Elliott Hughesadb8c672012-03-06 16:49:32 -08001885 DecodedInstruction dec_insn(insn);
Logan Chiene58b6582012-01-16 17:13:13 +08001886
1887 // Read the payload
1888 struct PACKED Payload {
1889 uint16_t ident_;
1890 uint16_t elem_width_;
1891 uint32_t num_elems_;
1892 uint8_t data_[];
1893 };
1894
1895 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
Elliott Hughesadb8c672012-03-06 16:49:32 -08001896 static_cast<int32_t>(dec_insn.vB);
Logan Chiene58b6582012-01-16 17:13:13 +08001897
1898 Payload const* payload =
1899 reinterpret_cast<Payload const*>(code_item_->insns_ + payload_offset);
1900
1901 uint32_t size_in_bytes = payload->elem_width_ * payload->num_elems_;
1902
1903 // Load and check the array
Elliott Hughesadb8c672012-03-06 16:49:32 -08001904 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiene58b6582012-01-16 17:13:13 +08001905
1906 EmitGuard_NullPointerException(dex_pc, array_addr);
1907
1908 if (payload->num_elems_ > 0) {
1909 // Test: Is array length big enough?
1910 llvm::Constant* last_index = irb_.getJInt(payload->num_elems_ - 1);
1911
1912 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array_addr, last_index);
1913
1914 // Get array data field
1915 llvm::Value* data_field_offset_value =
TDYa127b77799d2012-04-06 22:16:31 -07001916 irb_.getPtrEquivInt(Array::DataOffset(payload->elem_width_).Int32Value());
Logan Chiene58b6582012-01-16 17:13:13 +08001917
1918 llvm::Value* data_field_addr =
1919 irb_.CreatePtrDisp(array_addr, data_field_offset_value,
1920 irb_.getInt8Ty()->getPointerTo());
1921
1922 // Emit payload to bitcode constant pool
1923 std::vector<llvm::Constant*> const_pool_data;
1924 for (uint32_t i = 0; i < size_in_bytes; ++i) {
1925 const_pool_data.push_back(irb_.getInt8(payload->data_[i]));
1926 }
1927
1928 llvm::Constant* const_pool_data_array_value = llvm::ConstantArray::get(
1929 llvm::ArrayType::get(irb_.getInt8Ty(), size_in_bytes), const_pool_data);
1930
1931 llvm::Value* const_pool_data_array_addr =
1932 new llvm::GlobalVariable(*module_,
1933 const_pool_data_array_value->getType(),
1934 false, llvm::GlobalVariable::InternalLinkage,
1935 const_pool_data_array_value,
1936 "array_data_payload");
1937
1938 // Find the memcpy intrinsic
1939 llvm::Type* memcpy_arg_types[] = {
1940 llvm::Type::getInt8Ty(*context_)->getPointerTo(),
1941 llvm::Type::getInt8Ty(*context_)->getPointerTo(),
1942 llvm::Type::getInt32Ty(*context_)
1943 };
1944
1945 llvm::Function* memcpy_intrinsic =
1946 llvm::Intrinsic::getDeclaration(module_,
1947 llvm::Intrinsic::memcpy,
1948 memcpy_arg_types);
1949
1950 // Copy now!
1951 llvm::Value *args[] = {
1952 data_field_addr,
1953 irb_.CreateConstGEP2_32(const_pool_data_array_addr, 0, 0),
1954 irb_.getInt32(size_in_bytes),
1955 irb_.getInt32(0), // alignment: no guarantee
1956 irb_.getFalse() // is_volatile: false
1957 };
1958
1959 irb_.CreateCall(memcpy_intrinsic, args);
1960 }
1961
Logan Chien70f94b42011-12-27 17:49:11 +08001962 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1963}
1964
1965
1966void MethodCompiler::EmitInsn_UnconditionalBranch(uint32_t dex_pc,
1967 Instruction const* insn) {
Logan Chiena466c162011-12-27 17:55:46 +08001968
Elliott Hughesadb8c672012-03-06 16:49:32 -08001969 DecodedInstruction dec_insn(insn);
Logan Chiena466c162011-12-27 17:55:46 +08001970
Elliott Hughesadb8c672012-03-06 16:49:32 -08001971 int32_t branch_offset = dec_insn.vA;
Logan Chiena466c162011-12-27 17:55:46 +08001972
1973 if (branch_offset <= 0) {
1974 // Garbage collection safe-point on backward branch
1975 EmitGuard_GarbageCollectionSuspend(dex_pc);
1976 }
1977
1978 irb_.CreateBr(GetBasicBlock(dex_pc + branch_offset));
Logan Chien70f94b42011-12-27 17:49:11 +08001979}
1980
1981
1982void MethodCompiler::EmitInsn_PackedSwitch(uint32_t dex_pc,
1983 Instruction const* insn) {
Logan Chien7a89b6d2011-12-27 17:56:56 +08001984
Elliott Hughesadb8c672012-03-06 16:49:32 -08001985 DecodedInstruction dec_insn(insn);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001986
1987 struct PACKED Payload {
1988 uint16_t ident_;
1989 uint16_t num_cases_;
1990 int32_t first_key_;
1991 int32_t targets_[];
1992 };
1993
1994 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
Elliott Hughesadb8c672012-03-06 16:49:32 -08001995 static_cast<int32_t>(dec_insn.vB);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001996
1997 Payload const* payload =
1998 reinterpret_cast<Payload const*>(code_item_->insns_ + payload_offset);
1999
Elliott Hughesadb8c672012-03-06 16:49:32 -08002000 llvm::Value* value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chien7a89b6d2011-12-27 17:56:56 +08002001
2002 llvm::SwitchInst* sw =
2003 irb_.CreateSwitch(value, GetNextBasicBlock(dex_pc), payload->num_cases_);
2004
2005 for (uint16_t i = 0; i < payload->num_cases_; ++i) {
2006 sw->addCase(irb_.getInt32(payload->first_key_ + i),
2007 GetBasicBlock(dex_pc + payload->targets_[i]));
2008 }
Logan Chien70f94b42011-12-27 17:49:11 +08002009}
2010
2011
2012void MethodCompiler::EmitInsn_SparseSwitch(uint32_t dex_pc,
2013 Instruction const* insn) {
Logan Chien7a89b6d2011-12-27 17:56:56 +08002014
Elliott Hughesadb8c672012-03-06 16:49:32 -08002015 DecodedInstruction dec_insn(insn);
Logan Chien7a89b6d2011-12-27 17:56:56 +08002016
2017 struct PACKED Payload {
2018 uint16_t ident_;
2019 uint16_t num_cases_;
2020 int32_t keys_and_targets_[];
2021 };
2022
2023 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
Elliott Hughesadb8c672012-03-06 16:49:32 -08002024 static_cast<int32_t>(dec_insn.vB);
Logan Chien7a89b6d2011-12-27 17:56:56 +08002025
2026 Payload const* payload =
2027 reinterpret_cast<Payload const*>(code_item_->insns_ + payload_offset);
2028
2029 int32_t const* keys = payload->keys_and_targets_;
2030 int32_t const* targets = payload->keys_and_targets_ + payload->num_cases_;
2031
Elliott Hughesadb8c672012-03-06 16:49:32 -08002032 llvm::Value* value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chien7a89b6d2011-12-27 17:56:56 +08002033
2034 llvm::SwitchInst* sw =
2035 irb_.CreateSwitch(value, GetNextBasicBlock(dex_pc), payload->num_cases_);
2036
2037 for (size_t i = 0; i < payload->num_cases_; ++i) {
2038 sw->addCase(irb_.getInt32(keys[i]), GetBasicBlock(dex_pc + targets[i]));
2039 }
Logan Chien70f94b42011-12-27 17:49:11 +08002040}
2041
2042
2043void MethodCompiler::EmitInsn_FPCompare(uint32_t dex_pc,
2044 Instruction const* insn,
2045 JType fp_jty,
2046 bool gt_bias) {
Logan Chien2c37e8e2011-12-27 17:58:46 +08002047
Elliott Hughesadb8c672012-03-06 16:49:32 -08002048 DecodedInstruction dec_insn(insn);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002049
2050 DCHECK(fp_jty == kFloat || fp_jty == kDouble) << "JType: " << fp_jty;
2051
Elliott Hughesadb8c672012-03-06 16:49:32 -08002052 llvm::Value* src1_value = EmitLoadDalvikReg(dec_insn.vB, fp_jty, kAccurate);
2053 llvm::Value* src2_value = EmitLoadDalvikReg(dec_insn.vC, fp_jty, kAccurate);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002054
2055 llvm::Value* cmp_eq = irb_.CreateFCmpOEQ(src1_value, src2_value);
2056 llvm::Value* cmp_lt;
2057
2058 if (gt_bias) {
2059 cmp_lt = irb_.CreateFCmpOLT(src1_value, src2_value);
2060 } else {
2061 cmp_lt = irb_.CreateFCmpULT(src1_value, src2_value);
2062 }
2063
2064 llvm::Value* result = EmitCompareResultSelection(cmp_eq, cmp_lt);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002065 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002066
Logan Chien70f94b42011-12-27 17:49:11 +08002067 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2068}
2069
2070
2071void MethodCompiler::EmitInsn_LongCompare(uint32_t dex_pc,
2072 Instruction const* insn) {
Logan Chien2c37e8e2011-12-27 17:58:46 +08002073
Elliott Hughesadb8c672012-03-06 16:49:32 -08002074 DecodedInstruction dec_insn(insn);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002075
Elliott Hughesadb8c672012-03-06 16:49:32 -08002076 llvm::Value* src1_value = EmitLoadDalvikReg(dec_insn.vB, kLong, kAccurate);
2077 llvm::Value* src2_value = EmitLoadDalvikReg(dec_insn.vC, kLong, kAccurate);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002078
2079 llvm::Value* cmp_eq = irb_.CreateICmpEQ(src1_value, src2_value);
2080 llvm::Value* cmp_lt = irb_.CreateICmpSLT(src1_value, src2_value);
2081
2082 llvm::Value* result = EmitCompareResultSelection(cmp_eq, cmp_lt);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002083 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002084
Logan Chien70f94b42011-12-27 17:49:11 +08002085 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2086}
2087
2088
Logan Chien2c37e8e2011-12-27 17:58:46 +08002089llvm::Value* MethodCompiler::EmitCompareResultSelection(llvm::Value* cmp_eq,
2090 llvm::Value* cmp_lt) {
2091
2092 llvm::Constant* zero = irb_.getJInt(0);
2093 llvm::Constant* pos1 = irb_.getJInt(1);
2094 llvm::Constant* neg1 = irb_.getJInt(-1);
2095
2096 llvm::Value* result_lt = irb_.CreateSelect(cmp_lt, neg1, pos1);
2097 llvm::Value* result_eq = irb_.CreateSelect(cmp_eq, zero, result_lt);
2098
2099 return result_eq;
2100}
2101
2102
Logan Chien70f94b42011-12-27 17:49:11 +08002103void MethodCompiler::EmitInsn_BinaryConditionalBranch(uint32_t dex_pc,
2104 Instruction const* insn,
2105 CondBranchKind cond) {
Logan Chiena78e3c82011-12-27 17:59:35 +08002106
Elliott Hughesadb8c672012-03-06 16:49:32 -08002107 DecodedInstruction dec_insn(insn);
Logan Chiena78e3c82011-12-27 17:59:35 +08002108
Elliott Hughesadb8c672012-03-06 16:49:32 -08002109 int8_t src1_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vA);
2110 int8_t src2_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vB);
Logan Chiena78e3c82011-12-27 17:59:35 +08002111
2112 DCHECK_NE(kRegUnknown, src1_reg_cat);
2113 DCHECK_NE(kRegUnknown, src2_reg_cat);
2114 DCHECK_NE(kRegCat2, src1_reg_cat);
2115 DCHECK_NE(kRegCat2, src2_reg_cat);
2116
Elliott Hughesadb8c672012-03-06 16:49:32 -08002117 int32_t branch_offset = dec_insn.vC;
Logan Chiena78e3c82011-12-27 17:59:35 +08002118
2119 if (branch_offset <= 0) {
2120 // Garbage collection safe-point on backward branch
2121 EmitGuard_GarbageCollectionSuspend(dex_pc);
2122 }
2123
2124 if (src1_reg_cat == kRegZero && src2_reg_cat == kRegZero) {
2125 irb_.CreateBr(GetBasicBlock(dex_pc + branch_offset));
2126 return;
2127 }
2128
2129 llvm::Value* src1_value;
2130 llvm::Value* src2_value;
2131
2132 if (src1_reg_cat != kRegZero && src2_reg_cat != kRegZero) {
2133 CHECK_EQ(src1_reg_cat, src2_reg_cat);
2134
2135 if (src1_reg_cat == kRegCat1nr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002136 src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
2137 src2_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002138 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002139 src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
2140 src2_value = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002141 }
2142 } else {
2143 DCHECK(src1_reg_cat == kRegZero ||
2144 src2_reg_cat == kRegZero);
2145
2146 if (src1_reg_cat == kRegZero) {
2147 if (src2_reg_cat == kRegCat1nr) {
2148 src1_value = irb_.getJInt(0);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002149 src2_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002150 } else {
2151 src1_value = irb_.getJNull();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002152 src2_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002153 }
2154 } else { // src2_reg_cat == kRegZero
2155 if (src2_reg_cat == kRegCat1nr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002156 src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002157 src2_value = irb_.getJInt(0);
2158 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002159 src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002160 src2_value = irb_.getJNull();
2161 }
2162 }
2163 }
2164
2165 llvm::Value* cond_value =
2166 EmitConditionResult(src1_value, src2_value, cond);
2167
2168 irb_.CreateCondBr(cond_value,
2169 GetBasicBlock(dex_pc + branch_offset),
2170 GetNextBasicBlock(dex_pc));
Logan Chien70f94b42011-12-27 17:49:11 +08002171}
2172
2173
2174void MethodCompiler::EmitInsn_UnaryConditionalBranch(uint32_t dex_pc,
2175 Instruction const* insn,
2176 CondBranchKind cond) {
Logan Chiena78e3c82011-12-27 17:59:35 +08002177
Elliott Hughesadb8c672012-03-06 16:49:32 -08002178 DecodedInstruction dec_insn(insn);
Logan Chiena78e3c82011-12-27 17:59:35 +08002179
Elliott Hughesadb8c672012-03-06 16:49:32 -08002180 int8_t src_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vA);
Logan Chiena78e3c82011-12-27 17:59:35 +08002181
2182 DCHECK_NE(kRegUnknown, src_reg_cat);
2183 DCHECK_NE(kRegCat2, src_reg_cat);
2184
Elliott Hughesadb8c672012-03-06 16:49:32 -08002185 int32_t branch_offset = dec_insn.vB;
Logan Chiena78e3c82011-12-27 17:59:35 +08002186
2187 if (branch_offset <= 0) {
2188 // Garbage collection safe-point on backward branch
2189 EmitGuard_GarbageCollectionSuspend(dex_pc);
2190 }
2191
2192 if (src_reg_cat == kRegZero) {
2193 irb_.CreateBr(GetBasicBlock(dex_pc + branch_offset));
2194 return;
2195 }
2196
2197 llvm::Value* src1_value;
2198 llvm::Value* src2_value;
2199
2200 if (src_reg_cat == kRegCat1nr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002201 src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002202 src2_value = irb_.getInt32(0);
2203 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002204 src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002205 src2_value = irb_.getJNull();
2206 }
2207
2208 llvm::Value* cond_value =
2209 EmitConditionResult(src1_value, src2_value, cond);
2210
2211 irb_.CreateCondBr(cond_value,
2212 GetBasicBlock(dex_pc + branch_offset),
2213 GetNextBasicBlock(dex_pc));
2214}
2215
2216
2217RegCategory MethodCompiler::GetInferredRegCategory(uint32_t dex_pc,
2218 uint16_t reg_idx) {
Logan Chiendd361c92012-04-10 23:40:37 +08002219
2220 Compiler::MethodReference mref(dex_file_, method_idx_);
2221
2222 InferredRegCategoryMap const* map =
Ian Rogers776ac1f2012-04-13 23:36:36 -07002223 verifier::MethodVerifier::GetInferredRegCategoryMap(mref);
Logan Chiendd361c92012-04-10 23:40:37 +08002224
Logan Chiena78e3c82011-12-27 17:59:35 +08002225 CHECK_NE(map, static_cast<InferredRegCategoryMap*>(NULL));
2226
2227 return map->GetRegCategory(dex_pc, reg_idx);
2228}
2229
2230
2231llvm::Value* MethodCompiler::EmitConditionResult(llvm::Value* lhs,
2232 llvm::Value* rhs,
2233 CondBranchKind cond) {
2234 switch (cond) {
2235 case kCondBranch_EQ:
2236 return irb_.CreateICmpEQ(lhs, rhs);
2237
2238 case kCondBranch_NE:
2239 return irb_.CreateICmpNE(lhs, rhs);
2240
2241 case kCondBranch_LT:
2242 return irb_.CreateICmpSLT(lhs, rhs);
2243
2244 case kCondBranch_GE:
2245 return irb_.CreateICmpSGE(lhs, rhs);
2246
2247 case kCondBranch_GT:
2248 return irb_.CreateICmpSGT(lhs, rhs);
2249
2250 case kCondBranch_LE:
2251 return irb_.CreateICmpSLE(lhs, rhs);
2252
2253 default: // Unreachable
2254 LOG(FATAL) << "Unknown conditional branch kind: " << cond;
2255 return NULL;
2256 }
Logan Chien70f94b42011-12-27 17:49:11 +08002257}
2258
TDYa12783bb6622012-04-17 02:20:34 -07002259void MethodCompiler::EmitMarkGCCard(llvm::Value* value, llvm::Value* target_addr) {
2260 // Using runtime support, let the target can override by InlineAssembly.
2261 llvm::Function* runtime_func = irb_.GetRuntime(MarkGCCard);
2262
2263 irb_.CreateCall2(runtime_func, value, target_addr);
2264}
Logan Chien70f94b42011-12-27 17:49:11 +08002265
Logan Chiene27fdbb2012-01-02 23:27:26 +08002266void
2267MethodCompiler::EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc,
2268 llvm::Value* array,
2269 llvm::Value* index) {
2270 llvm::Value* array_len = EmitLoadArrayLength(array);
2271
2272 llvm::Value* cmp = irb_.CreateICmpUGE(index, array_len);
2273
2274 llvm::BasicBlock* block_exception =
2275 CreateBasicBlockWithDexPC(dex_pc, "overflow");
2276
2277 llvm::BasicBlock* block_continue =
2278 CreateBasicBlockWithDexPC(dex_pc, "cont");
2279
2280 irb_.CreateCondBr(cmp, block_exception, block_continue);
2281
2282 irb_.SetInsertPoint(block_exception);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002283
2284 EmitUpdateLineNumFromDexPC(dex_pc);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002285 irb_.CreateCall2(irb_.GetRuntime(ThrowIndexOutOfBounds), index, array_len);
2286 EmitBranchExceptionLandingPad(dex_pc);
2287
2288 irb_.SetInsertPoint(block_continue);
2289}
2290
2291
2292void MethodCompiler::EmitGuard_ArrayException(uint32_t dex_pc,
2293 llvm::Value* array,
2294 llvm::Value* index) {
2295 EmitGuard_NullPointerException(dex_pc, array);
2296 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array, index);
2297}
2298
2299
2300// Emit Array GetElementPtr
2301llvm::Value* MethodCompiler::EmitArrayGEP(llvm::Value* array_addr,
2302 llvm::Value* index_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08002303 llvm::Type* elem_type,
2304 JType elem_jty) {
2305
2306 int data_offset;
2307 if (elem_jty == kLong || elem_jty == kDouble ||
2308 (elem_jty == kObject && sizeof(uint64_t) == sizeof(Object*))) {
2309 data_offset = Array::DataOffset(sizeof(int64_t)).Int32Value();
2310 } else {
2311 data_offset = Array::DataOffset(sizeof(int32_t)).Int32Value();
2312 }
Logan Chiene27fdbb2012-01-02 23:27:26 +08002313
2314 llvm::Constant* data_offset_value =
Ian Rogers04ec04e2012-02-28 16:15:33 -08002315 irb_.getPtrEquivInt(data_offset);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002316
2317 llvm::Value* array_data_addr =
2318 irb_.CreatePtrDisp(array_addr, data_offset_value,
2319 elem_type->getPointerTo());
2320
2321 return irb_.CreateGEP(array_data_addr, index_value);
2322}
2323
2324
Logan Chien70f94b42011-12-27 17:49:11 +08002325void MethodCompiler::EmitInsn_AGet(uint32_t dex_pc,
2326 Instruction const* insn,
2327 JType elem_jty) {
Logan Chiene27fdbb2012-01-02 23:27:26 +08002328
Elliott Hughesadb8c672012-03-06 16:49:32 -08002329 DecodedInstruction dec_insn(insn);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002330
Elliott Hughesadb8c672012-03-06 16:49:32 -08002331 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
2332 llvm::Value* index_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002333
2334 EmitGuard_ArrayException(dex_pc, array_addr, index_value);
2335
2336 llvm::Type* elem_type = irb_.getJType(elem_jty, kArray);
2337
2338 llvm::Value* array_elem_addr =
Ian Rogers04ec04e2012-02-28 16:15:33 -08002339 EmitArrayGEP(array_addr, index_value, elem_type, elem_jty);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002340
2341 llvm::Value* array_elem_value = irb_.CreateLoad(array_elem_addr);
2342
Elliott Hughesadb8c672012-03-06 16:49:32 -08002343 EmitStoreDalvikReg(dec_insn.vA, elem_jty, kArray, array_elem_value);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002344
Logan Chien70f94b42011-12-27 17:49:11 +08002345 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2346}
2347
2348
2349void MethodCompiler::EmitInsn_APut(uint32_t dex_pc,
2350 Instruction const* insn,
2351 JType elem_jty) {
Logan Chien8dabb432012-01-02 23:29:32 +08002352
Elliott Hughesadb8c672012-03-06 16:49:32 -08002353 DecodedInstruction dec_insn(insn);
Logan Chien8dabb432012-01-02 23:29:32 +08002354
Elliott Hughesadb8c672012-03-06 16:49:32 -08002355 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
2356 llvm::Value* index_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate);
Logan Chien8dabb432012-01-02 23:29:32 +08002357
2358 EmitGuard_ArrayException(dex_pc, array_addr, index_value);
2359
2360 llvm::Type* elem_type = irb_.getJType(elem_jty, kArray);
2361
2362 llvm::Value* array_elem_addr =
Ian Rogers04ec04e2012-02-28 16:15:33 -08002363 EmitArrayGEP(array_addr, index_value, elem_type, elem_jty);
Logan Chien8dabb432012-01-02 23:29:32 +08002364
Elliott Hughesadb8c672012-03-06 16:49:32 -08002365 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, elem_jty, kArray);
Logan Chien8dabb432012-01-02 23:29:32 +08002366
TDYa12783bb6622012-04-17 02:20:34 -07002367 if (elem_jty == kObject) { // If put an object, check the type, and mark GC card table.
TDYa1271b86d072012-04-05 17:38:56 -07002368 llvm::Function* runtime_func = irb_.GetRuntime(CheckPutArrayElement);
2369
2370 irb_.CreateCall2(runtime_func, new_value, array_addr);
2371
2372 EmitGuard_ExceptionLandingPad(dex_pc);
TDYa12783bb6622012-04-17 02:20:34 -07002373
2374 EmitMarkGCCard(new_value, array_addr);
TDYa1271b86d072012-04-05 17:38:56 -07002375 }
2376
Logan Chien8dabb432012-01-02 23:29:32 +08002377 irb_.CreateStore(new_value, array_elem_addr);
2378
Logan Chien70f94b42011-12-27 17:49:11 +08002379 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2380}
2381
2382
2383void MethodCompiler::EmitInsn_IGet(uint32_t dex_pc,
2384 Instruction const* insn,
2385 JType field_jty) {
Logan Chien48f1d2a2012-01-02 22:49:53 +08002386
Elliott Hughesadb8c672012-03-06 16:49:32 -08002387 DecodedInstruction dec_insn(insn);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002388
Elliott Hughesadb8c672012-03-06 16:49:32 -08002389 uint32_t reg_idx = dec_insn.vB;
2390 uint32_t field_idx = dec_insn.vC;
Logan Chien48f1d2a2012-01-02 22:49:53 +08002391
Logan Chien48f1d2a2012-01-02 22:49:53 +08002392 llvm::Value* object_addr = EmitLoadDalvikReg(reg_idx, kObject, kAccurate);
2393
2394 EmitGuard_NullPointerException(dex_pc, object_addr);
2395
2396 llvm::Value* field_value;
2397
Logan Chien933abf82012-04-11 12:24:31 +08002398 int field_offset;
2399 bool is_volatile;
2400 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
2401 field_idx, oat_compilation_unit_, field_offset, is_volatile, false);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002402
Logan Chien933abf82012-04-11 12:24:31 +08002403 if (!is_fast_path) {
Logan Chien48f1d2a2012-01-02 22:49:53 +08002404 llvm::Function* runtime_func;
2405
2406 if (field_jty == kObject) {
Logan Chien3b2b2e72012-03-06 16:11:45 +08002407 runtime_func = irb_.GetRuntime(GetObjectInstance);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002408 } else if (field_jty == kLong || field_jty == kDouble) {
Logan Chien3b2b2e72012-03-06 16:11:45 +08002409 runtime_func = irb_.GetRuntime(Get64Instance);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002410 } else {
Logan Chien3b2b2e72012-03-06 16:11:45 +08002411 runtime_func = irb_.GetRuntime(Get32Instance);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002412 }
2413
2414 llvm::ConstantInt* field_idx_value = irb_.getInt32(field_idx);
2415
2416 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2417
Logan Chien8dfcbea2012-02-17 18:50:32 +08002418 EmitUpdateLineNumFromDexPC(dex_pc);
2419
Logan Chien3b2b2e72012-03-06 16:11:45 +08002420 field_value = irb_.CreateCall3(runtime_func, field_idx_value,
2421 method_object_addr, object_addr);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002422
2423 EmitGuard_ExceptionLandingPad(dex_pc);
2424
2425 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002426 DCHECK_GE(field_offset, 0);
2427
Logan Chien48f1d2a2012-01-02 22:49:53 +08002428 llvm::PointerType* field_type =
2429 irb_.getJType(field_jty, kField)->getPointerTo();
2430
Logan Chien933abf82012-04-11 12:24:31 +08002431 llvm::ConstantInt* field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002432
2433 llvm::Value* field_addr =
Logan Chien933abf82012-04-11 12:24:31 +08002434 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002435
Logan Chien933abf82012-04-11 12:24:31 +08002436 // TODO: Check is_volatile. We need to generate atomic load instruction
2437 // when is_volatile is true.
Logan Chien48f1d2a2012-01-02 22:49:53 +08002438 field_value = irb_.CreateLoad(field_addr);
2439 }
2440
Elliott Hughesadb8c672012-03-06 16:49:32 -08002441 EmitStoreDalvikReg(dec_insn.vA, field_jty, kField, field_value);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002442
Logan Chien70f94b42011-12-27 17:49:11 +08002443 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2444}
2445
2446
2447void MethodCompiler::EmitInsn_IPut(uint32_t dex_pc,
2448 Instruction const* insn,
2449 JType field_jty) {
Logan Chiendd6aa872012-01-03 16:06:32 +08002450
Elliott Hughesadb8c672012-03-06 16:49:32 -08002451 DecodedInstruction dec_insn(insn);
Logan Chiendd6aa872012-01-03 16:06:32 +08002452
Elliott Hughesadb8c672012-03-06 16:49:32 -08002453 uint32_t reg_idx = dec_insn.vB;
2454 uint32_t field_idx = dec_insn.vC;
Logan Chiendd6aa872012-01-03 16:06:32 +08002455
Logan Chiendd6aa872012-01-03 16:06:32 +08002456 llvm::Value* object_addr = EmitLoadDalvikReg(reg_idx, kObject, kAccurate);
2457
2458 EmitGuard_NullPointerException(dex_pc, object_addr);
2459
Elliott Hughesadb8c672012-03-06 16:49:32 -08002460 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, field_jty, kField);
Logan Chiendd6aa872012-01-03 16:06:32 +08002461
Logan Chien933abf82012-04-11 12:24:31 +08002462 int field_offset;
2463 bool is_volatile;
2464 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
2465 field_idx, oat_compilation_unit_, field_offset, is_volatile, true);
Logan Chiendd6aa872012-01-03 16:06:32 +08002466
Logan Chien933abf82012-04-11 12:24:31 +08002467 if (!is_fast_path) {
Logan Chiendd6aa872012-01-03 16:06:32 +08002468 llvm::Function* runtime_func;
2469
2470 if (field_jty == kObject) {
2471 runtime_func = irb_.GetRuntime(SetObjectInstance);
2472 } else if (field_jty == kLong || field_jty == kDouble) {
2473 runtime_func = irb_.GetRuntime(Set64Instance);
2474 } else {
2475 runtime_func = irb_.GetRuntime(Set32Instance);
2476 }
2477
2478 llvm::Value* field_idx_value = irb_.getInt32(field_idx);
2479
2480 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2481
Logan Chien8dfcbea2012-02-17 18:50:32 +08002482 EmitUpdateLineNumFromDexPC(dex_pc);
2483
Logan Chien3b2b2e72012-03-06 16:11:45 +08002484 irb_.CreateCall4(runtime_func, field_idx_value,
2485 method_object_addr, object_addr, new_value);
Logan Chiendd6aa872012-01-03 16:06:32 +08002486
2487 EmitGuard_ExceptionLandingPad(dex_pc);
2488
2489 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002490 DCHECK_GE(field_offset, 0);
2491
Logan Chiendd6aa872012-01-03 16:06:32 +08002492 llvm::PointerType* field_type =
2493 irb_.getJType(field_jty, kField)->getPointerTo();
2494
Logan Chien933abf82012-04-11 12:24:31 +08002495 llvm::Value* field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chiendd6aa872012-01-03 16:06:32 +08002496
2497 llvm::Value* field_addr =
Logan Chien933abf82012-04-11 12:24:31 +08002498 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
Logan Chiendd6aa872012-01-03 16:06:32 +08002499
Logan Chien933abf82012-04-11 12:24:31 +08002500 // TODO: Check is_volatile. We need to generate atomic store instruction
2501 // when is_volatile is true.
Logan Chiendd6aa872012-01-03 16:06:32 +08002502 irb_.CreateStore(new_value, field_addr);
TDYa12783bb6622012-04-17 02:20:34 -07002503
2504 if (field_jty == kObject) { // If put an object, mark the GC card table.
2505 EmitMarkGCCard(new_value, object_addr);
2506 }
Logan Chiendd6aa872012-01-03 16:06:32 +08002507 }
2508
Logan Chien70f94b42011-12-27 17:49:11 +08002509 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2510}
2511
2512
Logan Chien438c4b62012-01-17 16:06:00 +08002513llvm::Value* MethodCompiler::EmitLoadStaticStorage(uint32_t dex_pc,
2514 uint32_t type_idx) {
2515 llvm::BasicBlock* block_load_static =
2516 CreateBasicBlockWithDexPC(dex_pc, "load_static");
2517
2518 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
2519
2520 // Load static storage from dex cache
2521 llvm::Value* storage_field_addr =
2522 EmitLoadDexCacheStaticStorageFieldAddr(type_idx);
2523
2524 llvm::Value* storage_object_addr = irb_.CreateLoad(storage_field_addr);
2525
2526 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
2527
2528 // Test: Is the static storage of this class initialized?
2529 llvm::Value* equal_null =
2530 irb_.CreateICmpEQ(storage_object_addr, irb_.getJNull());
2531
2532 irb_.CreateCondBr(equal_null, block_load_static, block_cont);
2533
2534 // Failback routine to load the class object
2535 irb_.SetInsertPoint(block_load_static);
2536
2537 llvm::Function* runtime_func =
2538 irb_.GetRuntime(InitializeStaticStorage);
2539
2540 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
2541
2542 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2543
Logan Chien8dfcbea2012-02-17 18:50:32 +08002544 EmitUpdateLineNumFromDexPC(dex_pc);
2545
Logan Chien438c4b62012-01-17 16:06:00 +08002546 llvm::Value* loaded_storage_object_addr =
2547 irb_.CreateCall2(runtime_func, type_idx_value, method_object_addr);
2548
2549 EmitGuard_ExceptionLandingPad(dex_pc);
2550
2551 llvm::BasicBlock* block_after_load_static = irb_.GetInsertBlock();
2552
2553 irb_.CreateBr(block_cont);
2554
2555 // Now the class object must be loaded
2556 irb_.SetInsertPoint(block_cont);
2557
2558 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
2559
2560 phi->addIncoming(storage_object_addr, block_original);
2561 phi->addIncoming(loaded_storage_object_addr, block_after_load_static);
2562
2563 return phi;
2564}
2565
2566
Logan Chien70f94b42011-12-27 17:49:11 +08002567void MethodCompiler::EmitInsn_SGet(uint32_t dex_pc,
2568 Instruction const* insn,
2569 JType field_jty) {
Logan Chien438c4b62012-01-17 16:06:00 +08002570
Elliott Hughesadb8c672012-03-06 16:49:32 -08002571 DecodedInstruction dec_insn(insn);
Logan Chien438c4b62012-01-17 16:06:00 +08002572
Logan Chien933abf82012-04-11 12:24:31 +08002573 uint32_t field_idx = dec_insn.vB;
Logan Chien438c4b62012-01-17 16:06:00 +08002574
Logan Chien933abf82012-04-11 12:24:31 +08002575 int field_offset;
2576 int ssb_index;
2577 bool is_referrers_class;
2578 bool is_volatile;
2579
2580 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
2581 field_idx, oat_compilation_unit_, field_offset, ssb_index,
2582 is_referrers_class, is_volatile, false);
Logan Chien438c4b62012-01-17 16:06:00 +08002583
2584 llvm::Value* static_field_value;
2585
Logan Chien933abf82012-04-11 12:24:31 +08002586 if (!is_fast_path) {
Logan Chien438c4b62012-01-17 16:06:00 +08002587 llvm::Function* runtime_func;
2588
2589 if (field_jty == kObject) {
2590 runtime_func = irb_.GetRuntime(GetObjectStatic);
2591 } else if (field_jty == kLong || field_jty == kDouble) {
2592 runtime_func = irb_.GetRuntime(Get64Static);
2593 } else {
2594 runtime_func = irb_.GetRuntime(Get32Static);
2595 }
2596
Elliott Hughesadb8c672012-03-06 16:49:32 -08002597 llvm::Constant* field_idx_value = irb_.getInt32(dec_insn.vB);
Logan Chien438c4b62012-01-17 16:06:00 +08002598
2599 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2600
Logan Chien8dfcbea2012-02-17 18:50:32 +08002601 EmitUpdateLineNumFromDexPC(dex_pc);
2602
Logan Chien438c4b62012-01-17 16:06:00 +08002603 static_field_value =
2604 irb_.CreateCall2(runtime_func, field_idx_value, method_object_addr);
2605
2606 EmitGuard_ExceptionLandingPad(dex_pc);
2607
2608 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002609 DCHECK_GE(field_offset, 0);
Logan Chien438c4b62012-01-17 16:06:00 +08002610
Logan Chien933abf82012-04-11 12:24:31 +08002611 llvm::Value* static_storage_addr = NULL;
2612
2613 if (is_referrers_class) {
2614 // Fast path, static storage base is this method's class
2615 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2616
2617 llvm::Constant* declaring_class_offset_value =
2618 irb_.getPtrEquivInt(Method::DeclaringClassOffset().Int32Value());
2619
2620 llvm::Value* static_storage_field_addr =
2621 irb_.CreatePtrDisp(method_object_addr, declaring_class_offset_value,
2622 irb_.getJObjectTy()->getPointerTo());
2623
2624 static_storage_addr = irb_.CreateLoad(static_storage_field_addr);
2625 } else {
2626 // Medium path, static storage base in a different class which
2627 // requires checks that the other class is initialized
2628 DCHECK_GE(ssb_index, 0);
2629 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
2630 }
2631
2632 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chien438c4b62012-01-17 16:06:00 +08002633
2634 llvm::Value* static_field_addr =
2635 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
2636 irb_.getJType(field_jty, kField)->getPointerTo());
2637
Logan Chien933abf82012-04-11 12:24:31 +08002638 // TODO: Check is_volatile. We need to generate atomic load instruction
2639 // when is_volatile is true.
Logan Chien438c4b62012-01-17 16:06:00 +08002640 static_field_value = irb_.CreateLoad(static_field_addr);
2641 }
2642
Elliott Hughesadb8c672012-03-06 16:49:32 -08002643 EmitStoreDalvikReg(dec_insn.vA, field_jty, kField, static_field_value);
Logan Chien438c4b62012-01-17 16:06:00 +08002644
Logan Chien70f94b42011-12-27 17:49:11 +08002645 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2646}
2647
2648
2649void MethodCompiler::EmitInsn_SPut(uint32_t dex_pc,
2650 Instruction const* insn,
2651 JType field_jty) {
Logan Chien14179c82012-01-17 17:06:34 +08002652
Elliott Hughesadb8c672012-03-06 16:49:32 -08002653 DecodedInstruction dec_insn(insn);
Logan Chien14179c82012-01-17 17:06:34 +08002654
Logan Chien933abf82012-04-11 12:24:31 +08002655 uint32_t field_idx = dec_insn.vB;
Logan Chien14179c82012-01-17 17:06:34 +08002656
Elliott Hughesadb8c672012-03-06 16:49:32 -08002657 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, field_jty, kField);
Logan Chien14179c82012-01-17 17:06:34 +08002658
Logan Chien933abf82012-04-11 12:24:31 +08002659 int field_offset;
2660 int ssb_index;
2661 bool is_referrers_class;
2662 bool is_volatile;
2663
2664 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
2665 field_idx, oat_compilation_unit_, field_offset, ssb_index,
2666 is_referrers_class, is_volatile, true);
2667
2668 if (!is_fast_path) {
Logan Chien14179c82012-01-17 17:06:34 +08002669 llvm::Function* runtime_func;
2670
2671 if (field_jty == kObject) {
2672 runtime_func = irb_.GetRuntime(SetObjectStatic);
2673 } else if (field_jty == kLong || field_jty == kDouble) {
2674 runtime_func = irb_.GetRuntime(Set64Static);
2675 } else {
2676 runtime_func = irb_.GetRuntime(Set32Static);
2677 }
2678
Elliott Hughesadb8c672012-03-06 16:49:32 -08002679 llvm::Constant* field_idx_value = irb_.getInt32(dec_insn.vB);
Logan Chien14179c82012-01-17 17:06:34 +08002680
2681 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2682
Logan Chien8dfcbea2012-02-17 18:50:32 +08002683 EmitUpdateLineNumFromDexPC(dex_pc);
2684
Logan Chien14179c82012-01-17 17:06:34 +08002685 irb_.CreateCall3(runtime_func, field_idx_value,
2686 method_object_addr, new_value);
2687
2688 EmitGuard_ExceptionLandingPad(dex_pc);
2689
2690 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002691 DCHECK_GE(field_offset, 0);
Logan Chien14179c82012-01-17 17:06:34 +08002692
Logan Chien933abf82012-04-11 12:24:31 +08002693 llvm::Value* static_storage_addr = NULL;
2694
2695 if (is_referrers_class) {
2696 // Fast path, static storage base is this method's class
2697 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2698
2699 llvm::Constant* declaring_class_offset_value =
2700 irb_.getPtrEquivInt(Method::DeclaringClassOffset().Int32Value());
2701
2702 llvm::Value* static_storage_field_addr =
2703 irb_.CreatePtrDisp(method_object_addr, declaring_class_offset_value,
2704 irb_.getJObjectTy()->getPointerTo());
2705
2706 static_storage_addr = irb_.CreateLoad(static_storage_field_addr);
2707 } else {
2708 // Medium path, static storage base in a different class which
2709 // requires checks that the other class is initialized
2710 DCHECK_GE(ssb_index, 0);
2711 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
2712 }
2713
2714 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chien14179c82012-01-17 17:06:34 +08002715
2716 llvm::Value* static_field_addr =
2717 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
2718 irb_.getJType(field_jty, kField)->getPointerTo());
2719
Logan Chien933abf82012-04-11 12:24:31 +08002720 // TODO: Check is_volatile. We need to generate atomic store instruction
2721 // when is_volatile is true.
Logan Chien14179c82012-01-17 17:06:34 +08002722 irb_.CreateStore(new_value, static_field_addr);
TDYa12783bb6622012-04-17 02:20:34 -07002723
2724 if (field_jty == kObject) { // If put an object, mark the GC card table.
2725 EmitMarkGCCard(new_value, static_storage_addr);
2726 }
Logan Chien14179c82012-01-17 17:06:34 +08002727 }
2728
Logan Chien70f94b42011-12-27 17:49:11 +08002729 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2730}
2731
2732
Logan Chien1a121b92012-02-15 22:23:42 +08002733void MethodCompiler::
2734EmitLoadActualParameters(std::vector<llvm::Value*>& args,
2735 uint32_t callee_method_idx,
Elliott Hughesadb8c672012-03-06 16:49:32 -08002736 DecodedInstruction const& dec_insn,
Logan Chien7e7fabc2012-04-10 18:59:11 +08002737 InvokeArgFmt arg_fmt,
Logan Chien1a121b92012-02-15 22:23:42 +08002738 bool is_static) {
2739
2740 // Get method signature
2741 DexFile::MethodId const& method_id =
2742 dex_file_->GetMethodId(callee_method_idx);
2743
Logan Chien8faf8022012-02-24 12:25:29 +08002744 uint32_t shorty_size;
Logan Chien1a121b92012-02-15 22:23:42 +08002745 char const* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size);
Logan Chien8faf8022012-02-24 12:25:29 +08002746 CHECK_GE(shorty_size, 1u);
Logan Chien1a121b92012-02-15 22:23:42 +08002747
2748 // Load argument values according to the shorty (without "this")
2749 uint16_t reg_count = 0;
2750
2751 if (!is_static) {
2752 ++reg_count; // skip the "this" pointer
2753 }
2754
Logan Chien7e7fabc2012-04-10 18:59:11 +08002755 bool is_range = (arg_fmt == kArgRange);
2756
Logan Chien8faf8022012-02-24 12:25:29 +08002757 for (uint32_t i = 1; i < shorty_size; ++i) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002758 uint32_t reg_idx = (is_range) ? (dec_insn.vC + reg_count)
2759 : (dec_insn.arg[reg_count]);
Logan Chien1a121b92012-02-15 22:23:42 +08002760
2761 args.push_back(EmitLoadDalvikReg(reg_idx, shorty[i], kAccurate));
2762
2763 ++reg_count;
2764 if (shorty[i] == 'J' || shorty[i] == 'D') {
2765 // Wide types, such as long and double, are using a pair of registers
2766 // to store the value, so we have to increase arg_reg again.
2767 ++reg_count;
2768 }
2769 }
2770
Elliott Hughesadb8c672012-03-06 16:49:32 -08002771 DCHECK_EQ(reg_count, dec_insn.vA)
Logan Chien1a121b92012-02-15 22:23:42 +08002772 << "Actual argument mismatch for callee: "
2773 << PrettyMethod(callee_method_idx, *dex_file_);
2774}
2775
TDYa12785321912012-04-01 15:24:56 -07002776llvm::Value* MethodCompiler::EmitEnsureResolved(llvm::Value* callee,
2777 llvm::Value* caller,
2778 uint32_t dex_method_idx,
TDYa1270b686e52012-04-09 22:43:35 -07002779 bool is_virtual) {
2780 // TODO: Remove this after we solve the trampoline related problems.
2781 return irb_.CreateCall4(irb_.GetRuntime(EnsureResolved),
2782 callee,
2783 caller,
2784 irb_.getInt32(dex_method_idx),
2785 irb_.getInt1(is_virtual));
TDYa12785321912012-04-01 15:24:56 -07002786}
2787
TDYa1270b686e52012-04-09 22:43:35 -07002788llvm::Value* MethodCompiler::EmitFixStub(llvm::Value* callee_method_object_addr,
2789 uint32_t method_idx,
2790 bool is_static) {
2791 // TODO: Remove this after we solve the link and trampoline related problems.
2792 llvm::Value* code_addr = irb_.CreateCall(irb_.GetRuntime(FixStub), callee_method_object_addr);
2793
2794 llvm::FunctionType* method_type = GetFunctionType(method_idx, is_static);
2795
2796 return irb_.CreatePointerCast(code_addr, method_type->getPointerTo());
TDYa12785321912012-04-01 15:24:56 -07002797}
Logan Chien1a121b92012-02-15 22:23:42 +08002798
Shih-wei Liao399ed3f2012-03-08 01:27:04 -08002799
Logan Chien7e7fabc2012-04-10 18:59:11 +08002800void MethodCompiler::EmitInsn_Invoke(uint32_t dex_pc,
2801 Instruction const* insn,
2802 InvokeType invoke_type,
2803 InvokeArgFmt arg_fmt) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002804 DecodedInstruction dec_insn(insn);
Logan Chien46fbb412012-02-15 22:29:08 +08002805
Logan Chien61c65dc2012-02-29 03:22:30 +08002806 bool is_static = (invoke_type == kStatic);
Logan Chien7e7fabc2012-04-10 18:59:11 +08002807 uint32_t callee_method_idx = dec_insn.vB;
Logan Chien61c65dc2012-02-29 03:22:30 +08002808
Logan Chien7e7fabc2012-04-10 18:59:11 +08002809 // Compute invoke related information for compiler decision
2810 int vtable_idx = -1;
Logan Chien92ad16d2012-03-18 05:48:55 +08002811 uintptr_t direct_code = 0; // Currently unused
2812 uintptr_t direct_method = 0; // Currently unused
Logan Chien61c65dc2012-02-29 03:22:30 +08002813 bool is_fast_path = compiler_->
Logan Chien7e7fabc2012-04-10 18:59:11 +08002814 ComputeInvokeInfo(callee_method_idx, oat_compilation_unit_,
Logan Chien92ad16d2012-03-18 05:48:55 +08002815 invoke_type, vtable_idx, direct_code, direct_method);
Logan Chien61c65dc2012-02-29 03:22:30 +08002816
Logan Chien7e7fabc2012-04-10 18:59:11 +08002817 // Load *this* actual parameter
Logan Chien1a121b92012-02-15 22:23:42 +08002818 llvm::Value* this_addr = NULL;
2819
2820 if (!is_static) {
2821 // Test: Is *this* parameter equal to null?
Logan Chien7e7fabc2012-04-10 18:59:11 +08002822 this_addr = (arg_fmt == kArgReg) ?
2823 EmitLoadDalvikReg(dec_insn.arg[0], kObject, kAccurate):
2824 EmitLoadDalvikReg(dec_insn.vC + 0, kObject, kAccurate);
2825
Logan Chien1a121b92012-02-15 22:23:42 +08002826 EmitGuard_NullPointerException(dex_pc, this_addr);
2827 }
2828
Logan Chien7e7fabc2012-04-10 18:59:11 +08002829 // Load the method object
TDYa1274e42a592012-04-10 20:13:54 -07002830 llvm::Value* callee_method_object_addr = NULL;
Logan Chien7e7fabc2012-04-10 18:59:11 +08002831
2832 if (!is_fast_path) {
TDYa1274e42a592012-04-10 20:13:54 -07002833 callee_method_object_addr =
Logan Chien7e7fabc2012-04-10 18:59:11 +08002834 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx, invoke_type,
2835 this_addr, dex_pc, is_fast_path);
2836 } else {
2837 switch (invoke_type) {
2838 case kStatic:
2839 case kDirect:
TDYa1274e42a592012-04-10 20:13:54 -07002840 callee_method_object_addr =
Logan Chien7e7fabc2012-04-10 18:59:11 +08002841 EmitLoadSDCalleeMethodObjectAddr(callee_method_idx);
2842 break;
2843
2844 case kVirtual:
2845 DCHECK(vtable_idx != -1);
TDYa1274e42a592012-04-10 20:13:54 -07002846 callee_method_object_addr =
Logan Chien7e7fabc2012-04-10 18:59:11 +08002847 EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr);
2848 break;
2849
2850 case kSuper:
2851 LOG(FATAL) << "invoke-super should be promoted to invoke-direct in "
2852 "the fast path.";
2853 break;
2854
2855 case kInterface:
TDYa1274e42a592012-04-10 20:13:54 -07002856 callee_method_object_addr =
Logan Chien7e7fabc2012-04-10 18:59:11 +08002857 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx,
2858 invoke_type, this_addr,
2859 dex_pc, is_fast_path);
2860 break;
2861 }
TDYa1274e42a592012-04-10 20:13:54 -07002862
2863 // Ensure the callee method object is resolved.
2864 bool is_virtual = (dec_insn.opcode == Instruction::INVOKE_VIRTUAL) ||
2865 (dec_insn.opcode == Instruction::INVOKE_VIRTUAL_RANGE) ||
2866 (dec_insn.opcode == Instruction::INVOKE_SUPER) ||
2867 (dec_insn.opcode == Instruction::INVOKE_SUPER_RANGE);
2868
2869 llvm::Value* caller_method_object_addr = EmitLoadMethodObjectAddr();
2870
2871 callee_method_object_addr = EmitEnsureResolved(callee_method_object_addr,
2872 caller_method_object_addr,
2873 callee_method_idx,
2874 is_virtual);
2875
2876 EmitGuard_ExceptionLandingPad(dex_pc);
Logan Chien7e7fabc2012-04-10 18:59:11 +08002877 }
2878
TDYa1270b686e52012-04-09 22:43:35 -07002879#if 0
Logan Chien7e7fabc2012-04-10 18:59:11 +08002880 llvm::Value* code_field_offset_value =
2881 irb_.getPtrEquivInt(Method::GetCodeOffset().Int32Value());
TDYa12785321912012-04-01 15:24:56 -07002882
Logan Chien7e7fabc2012-04-10 18:59:11 +08002883 llvm::Value* code_field_addr =
2884 irb_.CreatePtrDisp(callee_method_object_addr, code_field_offset_value,
2885 irb_.getInt8Ty()->getPointerTo()->getPointerTo());
2886
2887 llvm::Value* code_addr = irb_.CreateLoad(code_field_addr);
2888#else
2889 // TODO: Remove this after we solve the link and trampoline related problems.
2890 llvm::Value* code_addr =
2891 EmitFixStub(callee_method_object_addr, callee_method_idx, is_static);
2892
2893 EmitGuard_ExceptionLandingPad(dex_pc);
TDYa1270b686e52012-04-09 22:43:35 -07002894#endif
TDYa12785321912012-04-01 15:24:56 -07002895
Logan Chien1a121b92012-02-15 22:23:42 +08002896 // Load the actual parameter
2897 std::vector<llvm::Value*> args;
2898
2899 args.push_back(callee_method_object_addr); // method object for callee
2900
2901 if (!is_static) {
Logan Chien7e7fabc2012-04-10 18:59:11 +08002902 DCHECK(this_addr != NULL);
Logan Chien1a121b92012-02-15 22:23:42 +08002903 args.push_back(this_addr); // "this" object for callee
2904 }
2905
2906 EmitLoadActualParameters(args, callee_method_idx, dec_insn,
Logan Chien7e7fabc2012-04-10 18:59:11 +08002907 arg_fmt, is_static);
Logan Chien1a121b92012-02-15 22:23:42 +08002908
TDYa1275bb86012012-04-11 05:57:28 -07002909#if 0
Logan Chien8dfcbea2012-02-17 18:50:32 +08002910 // Invoke callee
2911 EmitUpdateLineNumFromDexPC(dex_pc);
Logan Chien1a121b92012-02-15 22:23:42 +08002912 llvm::Value* retval = irb_.CreateCall(code_addr, args);
2913 EmitGuard_ExceptionLandingPad(dex_pc);
2914
Logan Chien7e7fabc2012-04-10 18:59:11 +08002915 uint32_t callee_access_flags = is_static ? kAccStatic : 0;
2916 UniquePtr<OatCompilationUnit> callee_oat_compilation_unit(
2917 oat_compilation_unit_->GetCallee(callee_method_idx, callee_access_flags));
2918
2919 char ret_shorty = callee_oat_compilation_unit->GetShorty()[0];
Shih-wei Liao90d50992012-02-19 03:32:05 -08002920 if (ret_shorty != 'V') {
Logan Chien1a121b92012-02-15 22:23:42 +08002921 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
2922 }
TDYa1275bb86012012-04-11 05:57:28 -07002923#else
2924 uint32_t callee_access_flags = is_static ? kAccStatic : 0;
2925 UniquePtr<OatCompilationUnit> callee_oat_compilation_unit(
2926 oat_compilation_unit_->GetCallee(callee_method_idx, callee_access_flags));
2927
2928 char ret_shorty = callee_oat_compilation_unit->GetShorty()[0];
2929
2930
2931 EmitUpdateLineNumFromDexPC(dex_pc);
2932
2933
2934 llvm::BasicBlock* block_normal = CreateBasicBlockWithDexPC(dex_pc, "normal");
2935 llvm::BasicBlock* block_proxy_stub = CreateBasicBlockWithDexPC(dex_pc, "proxy");
2936 llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont");
2937
2938 llvm::Type* accurate_ret_type = irb_.getJType(ret_shorty, kAccurate);
2939 llvm::Value* retval_addr = NULL;
2940 if (ret_shorty != 'V') {
2941 retval_addr = irb_.CreateAlloca(accurate_ret_type);
2942 }
2943
2944
2945 // TODO: Remove this after we solve the proxy trampoline calling convention problem.
2946 llvm::Value* code_addr_int = irb_.CreatePtrToInt(code_addr, irb_.getPtrEquivIntTy());
2947 llvm::Value* proxy_stub_int = irb_.getPtrEquivInt(special_stub::kProxyStub);
2948 llvm::Value* is_proxy_stub = irb_.CreateICmpEQ(code_addr_int, proxy_stub_int);
2949 irb_.CreateCondBr(is_proxy_stub, block_proxy_stub, block_normal);
2950
2951
2952 irb_.SetInsertPoint(block_normal);
2953 {
2954 // Invoke callee
2955 llvm::Value* result = irb_.CreateCall(code_addr, args);
2956 if (ret_shorty != 'V') {
2957 irb_.CreateStore(result, retval_addr);
2958 }
2959 }
2960 irb_.CreateBr(block_continue);
2961
2962
2963 irb_.SetInsertPoint(block_proxy_stub);
2964 {
2965 llvm::Value* temp_space_addr;
2966 if (ret_shorty != 'V') {
2967 temp_space_addr = irb_.CreateAlloca(irb_.getJValueTy());
2968 args.push_back(temp_space_addr);
2969 }
2970 irb_.CreateCall(irb_.GetRuntime(ProxyInvokeHandler), args);
2971 if (ret_shorty != 'V') {
2972 llvm::Value* result_addr =
2973 irb_.CreateBitCast(temp_space_addr, accurate_ret_type->getPointerTo());
2974 llvm::Value* retval = irb_.CreateLoad(result_addr);
2975 irb_.CreateStore(retval, retval_addr);
2976 }
2977 }
2978 irb_.CreateBr(block_continue);
2979
2980
2981 irb_.SetInsertPoint(block_continue);
2982
2983 if (ret_shorty != 'V') {
2984 llvm::Value* retval = irb_.CreateLoad(retval_addr);
2985 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
2986 }
2987 EmitGuard_ExceptionLandingPad(dex_pc);
2988#endif
Logan Chien1a121b92012-02-15 22:23:42 +08002989
Logan Chien70f94b42011-12-27 17:49:11 +08002990 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2991}
2992
2993
Logan Chien7e7fabc2012-04-10 18:59:11 +08002994llvm::Value* MethodCompiler::
2995EmitLoadSDCalleeMethodObjectAddr(uint32_t callee_method_idx) {
2996 llvm::Value* callee_method_object_field_addr =
2997 EmitLoadDexCacheResolvedMethodFieldAddr(callee_method_idx);
Logan Chien7caf37e2012-02-03 22:56:04 +08002998
Logan Chien7e7fabc2012-04-10 18:59:11 +08002999 return irb_.CreateLoad(callee_method_object_field_addr);
3000}
Logan Chien7caf37e2012-02-03 22:56:04 +08003001
Logan Chien7caf37e2012-02-03 22:56:04 +08003002
Logan Chien7e7fabc2012-04-10 18:59:11 +08003003llvm::Value* MethodCompiler::
3004EmitLoadVirtualCalleeMethodObjectAddr(int vtable_idx,
3005 llvm::Value* this_addr) {
3006 // Load class object of *this* pointer
3007 llvm::Constant* class_field_offset_value =
3008 irb_.getPtrEquivInt(Object::ClassOffset().Int32Value());
Logan Chien7caf37e2012-02-03 22:56:04 +08003009
Logan Chien7e7fabc2012-04-10 18:59:11 +08003010 llvm::Value* class_field_addr =
3011 irb_.CreatePtrDisp(this_addr, class_field_offset_value,
3012 irb_.getJObjectTy()->getPointerTo());
Logan Chien7caf37e2012-02-03 22:56:04 +08003013
Logan Chien7e7fabc2012-04-10 18:59:11 +08003014 llvm::Value* class_object_addr = irb_.CreateLoad(class_field_addr);
Logan Chien7caf37e2012-02-03 22:56:04 +08003015
Logan Chien7e7fabc2012-04-10 18:59:11 +08003016 // Load vtable address
3017 llvm::Constant* vtable_offset_value =
3018 irb_.getPtrEquivInt(Class::VTableOffset().Int32Value());
3019
3020 llvm::Value* vtable_field_addr =
3021 irb_.CreatePtrDisp(class_object_addr, vtable_offset_value,
3022 irb_.getJObjectTy()->getPointerTo());
3023
3024 llvm::Value* vtable_addr = irb_.CreateLoad(vtable_field_addr);
3025
3026 // Load callee method object
3027 llvm::Value* vtable_idx_value =
3028 irb_.getPtrEquivInt(static_cast<uint64_t>(vtable_idx));
3029
3030 llvm::Value* method_field_addr =
3031 EmitArrayGEP(vtable_addr, vtable_idx_value, irb_.getJObjectTy(), kObject);
3032
3033 return irb_.CreateLoad(method_field_addr);
3034}
3035
3036
3037llvm::Value* MethodCompiler::
3038EmitCallRuntimeForCalleeMethodObjectAddr(uint32_t callee_method_idx,
3039 InvokeType invoke_type,
3040 llvm::Value* this_addr,
3041 uint32_t dex_pc,
3042 bool is_fast_path) {
3043
3044 llvm::Function* runtime_func = NULL;
3045
3046 switch (invoke_type) {
3047 case kStatic:
3048 runtime_func = irb_.GetRuntime(FindStaticMethodWithAccessCheck);
3049 break;
3050
3051 case kDirect:
3052 runtime_func = irb_.GetRuntime(FindDirectMethodWithAccessCheck);
3053 break;
3054
3055 case kVirtual:
3056 runtime_func = irb_.GetRuntime(FindVirtualMethodWithAccessCheck);
3057 break;
3058
3059 case kSuper:
3060 runtime_func = irb_.GetRuntime(FindSuperMethodWithAccessCheck);
3061 break;
3062
3063 case kInterface:
3064 if (is_fast_path) {
3065 runtime_func = irb_.GetRuntime(FindInterfaceMethod);
3066 } else {
3067 runtime_func = irb_.GetRuntime(FindInterfaceMethodWithAccessCheck);
3068 }
3069 break;
3070 }
Logan Chien7caf37e2012-02-03 22:56:04 +08003071
3072 llvm::Value* callee_method_idx_value = irb_.getInt32(callee_method_idx);
3073
Logan Chien7e7fabc2012-04-10 18:59:11 +08003074 if (this_addr == NULL) {
3075 DCHECK_EQ(invoke_type, kStatic);
3076 this_addr = irb_.getJNull();
3077 }
3078
3079 llvm::Value* caller_method_object_addr = EmitLoadMethodObjectAddr();
3080
TDYa127da83d972012-04-18 00:21:49 -07003081 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
3082
Logan Chien8dfcbea2012-02-17 18:50:32 +08003083 EmitUpdateLineNumFromDexPC(dex_pc);
3084
TDYa1270b686e52012-04-09 22:43:35 -07003085 llvm::Value* callee_method_object_addr =
TDYa127da83d972012-04-18 00:21:49 -07003086 irb_.CreateCall4(runtime_func,
Logan Chien7e7fabc2012-04-10 18:59:11 +08003087 callee_method_idx_value,
3088 this_addr,
TDYa127da83d972012-04-18 00:21:49 -07003089 caller_method_object_addr,
3090 thread_object_addr);
Logan Chien7caf37e2012-02-03 22:56:04 +08003091
3092 EmitGuard_ExceptionLandingPad(dex_pc);
3093
Logan Chien7e7fabc2012-04-10 18:59:11 +08003094 return callee_method_object_addr;
Logan Chien70f94b42011-12-27 17:49:11 +08003095}
3096
3097
3098void MethodCompiler::EmitInsn_Neg(uint32_t dex_pc,
3099 Instruction const* insn,
3100 JType op_jty) {
Logan Chien1b5685f2011-12-27 18:01:14 +08003101
Elliott Hughesadb8c672012-03-06 16:49:32 -08003102 DecodedInstruction dec_insn(insn);
Logan Chien1b5685f2011-12-27 18:01:14 +08003103
3104 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3105
Elliott Hughesadb8c672012-03-06 16:49:32 -08003106 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chien1b5685f2011-12-27 18:01:14 +08003107 llvm::Value* result_value = irb_.CreateNeg(src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003108 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chien1b5685f2011-12-27 18:01:14 +08003109
Logan Chien70f94b42011-12-27 17:49:11 +08003110 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3111}
3112
3113
3114void MethodCompiler::EmitInsn_Not(uint32_t dex_pc,
3115 Instruction const* insn,
3116 JType op_jty) {
Logan Chiene53750d2011-12-27 18:02:27 +08003117
Elliott Hughesadb8c672012-03-06 16:49:32 -08003118 DecodedInstruction dec_insn(insn);
Logan Chiene53750d2011-12-27 18:02:27 +08003119
3120 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3121
Elliott Hughesadb8c672012-03-06 16:49:32 -08003122 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chiene53750d2011-12-27 18:02:27 +08003123 llvm::Value* result_value =
3124 irb_.CreateXor(src_value, static_cast<uint64_t>(-1));
3125
Elliott Hughesadb8c672012-03-06 16:49:32 -08003126 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chiene53750d2011-12-27 18:02:27 +08003127
Logan Chien70f94b42011-12-27 17:49:11 +08003128 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3129}
3130
3131
3132void MethodCompiler::EmitInsn_SExt(uint32_t dex_pc,
3133 Instruction const* insn) {
Logan Chien61752ad2011-12-27 18:03:51 +08003134
Elliott Hughesadb8c672012-03-06 16:49:32 -08003135 DecodedInstruction dec_insn(insn);
Logan Chien61752ad2011-12-27 18:03:51 +08003136
Elliott Hughesadb8c672012-03-06 16:49:32 -08003137 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chien61752ad2011-12-27 18:03:51 +08003138 llvm::Value* result_value = irb_.CreateSExt(src_value, irb_.getJLongTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003139 EmitStoreDalvikReg(dec_insn.vA, kLong, kAccurate, result_value);
Logan Chien61752ad2011-12-27 18:03:51 +08003140
Logan Chien70f94b42011-12-27 17:49:11 +08003141 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3142}
3143
3144
3145void MethodCompiler::EmitInsn_Trunc(uint32_t dex_pc,
3146 Instruction const* insn) {
Logan Chien17a57662011-12-27 18:05:14 +08003147
Elliott Hughesadb8c672012-03-06 16:49:32 -08003148 DecodedInstruction dec_insn(insn);
Logan Chien17a57662011-12-27 18:05:14 +08003149
Elliott Hughesadb8c672012-03-06 16:49:32 -08003150 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kLong, kAccurate);
Logan Chien17a57662011-12-27 18:05:14 +08003151 llvm::Value* result_value = irb_.CreateTrunc(src_value, irb_.getJIntTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003152 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chien17a57662011-12-27 18:05:14 +08003153
Logan Chien70f94b42011-12-27 17:49:11 +08003154 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3155}
3156
3157
3158void MethodCompiler::EmitInsn_TruncAndSExt(uint32_t dex_pc,
3159 Instruction const* insn,
3160 unsigned N) {
Logan Chienb6744c52011-12-27 18:06:26 +08003161
Elliott Hughesadb8c672012-03-06 16:49:32 -08003162 DecodedInstruction dec_insn(insn);
Logan Chienb6744c52011-12-27 18:06:26 +08003163
Elliott Hughesadb8c672012-03-06 16:49:32 -08003164 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chienb6744c52011-12-27 18:06:26 +08003165
3166 llvm::Value* trunc_value =
3167 irb_.CreateTrunc(src_value, llvm::Type::getIntNTy(*context_, N));
3168
3169 llvm::Value* result_value = irb_.CreateSExt(trunc_value, irb_.getJIntTy());
3170
Elliott Hughesadb8c672012-03-06 16:49:32 -08003171 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chienb6744c52011-12-27 18:06:26 +08003172
Logan Chien70f94b42011-12-27 17:49:11 +08003173 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3174}
3175
3176
3177void MethodCompiler::EmitInsn_TruncAndZExt(uint32_t dex_pc,
3178 Instruction const* insn,
3179 unsigned N) {
Logan Chienb6744c52011-12-27 18:06:26 +08003180
Elliott Hughesadb8c672012-03-06 16:49:32 -08003181 DecodedInstruction dec_insn(insn);
Logan Chienb6744c52011-12-27 18:06:26 +08003182
Elliott Hughesadb8c672012-03-06 16:49:32 -08003183 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chienb6744c52011-12-27 18:06:26 +08003184
3185 llvm::Value* trunc_value =
3186 irb_.CreateTrunc(src_value, llvm::Type::getIntNTy(*context_, N));
3187
3188 llvm::Value* result_value = irb_.CreateZExt(trunc_value, irb_.getJIntTy());
3189
Elliott Hughesadb8c672012-03-06 16:49:32 -08003190 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chienb6744c52011-12-27 18:06:26 +08003191
Logan Chien70f94b42011-12-27 17:49:11 +08003192 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3193}
3194
3195
3196void MethodCompiler::EmitInsn_FNeg(uint32_t dex_pc,
3197 Instruction const* insn,
3198 JType op_jty) {
Logan Chien7a48b092011-12-27 18:07:45 +08003199
Elliott Hughesadb8c672012-03-06 16:49:32 -08003200 DecodedInstruction dec_insn(insn);
Logan Chien7a48b092011-12-27 18:07:45 +08003201
3202 DCHECK(op_jty == kFloat || op_jty == kDouble) << op_jty;
3203
Elliott Hughesadb8c672012-03-06 16:49:32 -08003204 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chien7a48b092011-12-27 18:07:45 +08003205 llvm::Value* result_value = irb_.CreateFNeg(src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003206 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chien7a48b092011-12-27 18:07:45 +08003207
Logan Chien70f94b42011-12-27 17:49:11 +08003208 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3209}
3210
3211
3212void MethodCompiler::EmitInsn_IntToFP(uint32_t dex_pc,
3213 Instruction const* insn,
3214 JType src_jty,
3215 JType dest_jty) {
Logan Chien62dd4532011-12-27 18:09:00 +08003216
Elliott Hughesadb8c672012-03-06 16:49:32 -08003217 DecodedInstruction dec_insn(insn);
Logan Chien62dd4532011-12-27 18:09:00 +08003218
3219 DCHECK(src_jty == kInt || src_jty == kLong) << src_jty;
3220 DCHECK(dest_jty == kFloat || dest_jty == kDouble) << dest_jty;
3221
Elliott Hughesadb8c672012-03-06 16:49:32 -08003222 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, src_jty, kAccurate);
Logan Chien62dd4532011-12-27 18:09:00 +08003223 llvm::Type* dest_type = irb_.getJType(dest_jty, kAccurate);
3224 llvm::Value* dest_value = irb_.CreateSIToFP(src_value, dest_type);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003225 EmitStoreDalvikReg(dec_insn.vA, dest_jty, kAccurate, dest_value);
Logan Chien62dd4532011-12-27 18:09:00 +08003226
Logan Chien70f94b42011-12-27 17:49:11 +08003227 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3228}
3229
3230
3231void MethodCompiler::EmitInsn_FPToInt(uint32_t dex_pc,
3232 Instruction const* insn,
3233 JType src_jty,
TDYa127a4746872012-04-11 23:48:55 -07003234 JType dest_jty,
3235 runtime_support::RuntimeId runtime_func_id) {
Logan Chien12dc1752011-12-27 18:10:15 +08003236
Elliott Hughesadb8c672012-03-06 16:49:32 -08003237 DecodedInstruction dec_insn(insn);
Logan Chien12dc1752011-12-27 18:10:15 +08003238
3239 DCHECK(src_jty == kFloat || src_jty == kDouble) << src_jty;
3240 DCHECK(dest_jty == kInt || dest_jty == kLong) << dest_jty;
3241
Elliott Hughesadb8c672012-03-06 16:49:32 -08003242 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, src_jty, kAccurate);
TDYa127a4746872012-04-11 23:48:55 -07003243 llvm::Value* dest_value = irb_.CreateCall(irb_.GetRuntime(runtime_func_id), src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003244 EmitStoreDalvikReg(dec_insn.vA, dest_jty, kAccurate, dest_value);
Logan Chien12dc1752011-12-27 18:10:15 +08003245
Logan Chien70f94b42011-12-27 17:49:11 +08003246 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3247}
3248
3249
3250void MethodCompiler::EmitInsn_FExt(uint32_t dex_pc,
3251 Instruction const* insn) {
Logan Chienc56ded92011-12-27 18:10:57 +08003252
Elliott Hughesadb8c672012-03-06 16:49:32 -08003253 DecodedInstruction dec_insn(insn);
Logan Chienc56ded92011-12-27 18:10:57 +08003254
Elliott Hughesadb8c672012-03-06 16:49:32 -08003255 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kFloat, kAccurate);
Logan Chienc56ded92011-12-27 18:10:57 +08003256 llvm::Value* result_value = irb_.CreateFPExt(src_value, irb_.getJDoubleTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003257 EmitStoreDalvikReg(dec_insn.vA, kDouble, kAccurate, result_value);
Logan Chienc56ded92011-12-27 18:10:57 +08003258
Logan Chien70f94b42011-12-27 17:49:11 +08003259 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3260}
3261
3262
3263void MethodCompiler::EmitInsn_FTrunc(uint32_t dex_pc,
3264 Instruction const* insn) {
Logan Chien927744f2011-12-27 18:11:52 +08003265
Elliott Hughesadb8c672012-03-06 16:49:32 -08003266 DecodedInstruction dec_insn(insn);
Logan Chien927744f2011-12-27 18:11:52 +08003267
Elliott Hughesadb8c672012-03-06 16:49:32 -08003268 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kDouble, kAccurate);
Logan Chien927744f2011-12-27 18:11:52 +08003269 llvm::Value* result_value = irb_.CreateFPTrunc(src_value, irb_.getJFloatTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003270 EmitStoreDalvikReg(dec_insn.vA, kFloat, kAccurate, result_value);
Logan Chien927744f2011-12-27 18:11:52 +08003271
Logan Chien70f94b42011-12-27 17:49:11 +08003272 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3273}
3274
3275
3276void MethodCompiler::EmitInsn_IntArithm(uint32_t dex_pc,
3277 Instruction const* insn,
3278 IntArithmKind arithm,
3279 JType op_jty,
3280 bool is_2addr) {
Logan Chienc3f7d962011-12-27 18:13:18 +08003281
Elliott Hughesadb8c672012-03-06 16:49:32 -08003282 DecodedInstruction dec_insn(insn);
Logan Chienc3f7d962011-12-27 18:13:18 +08003283
3284 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3285
3286 llvm::Value* src1_value;
3287 llvm::Value* src2_value;
3288
3289 if (is_2addr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003290 src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate);
3291 src2_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chienc3f7d962011-12-27 18:13:18 +08003292 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003293 src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
3294 src2_value = EmitLoadDalvikReg(dec_insn.vC, op_jty, kAccurate);
Logan Chienc3f7d962011-12-27 18:13:18 +08003295 }
3296
3297 llvm::Value* result_value =
3298 EmitIntArithmResultComputation(dex_pc, src1_value, src2_value,
3299 arithm, op_jty);
3300
Elliott Hughesadb8c672012-03-06 16:49:32 -08003301 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chienc3f7d962011-12-27 18:13:18 +08003302
Logan Chien70f94b42011-12-27 17:49:11 +08003303 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3304}
3305
3306
3307void MethodCompiler::EmitInsn_IntArithmImmediate(uint32_t dex_pc,
3308 Instruction const* insn,
3309 IntArithmKind arithm) {
Logan Chienc3f7d962011-12-27 18:13:18 +08003310
Elliott Hughesadb8c672012-03-06 16:49:32 -08003311 DecodedInstruction dec_insn(insn);
Logan Chienc3f7d962011-12-27 18:13:18 +08003312
Elliott Hughesadb8c672012-03-06 16:49:32 -08003313 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chienc3f7d962011-12-27 18:13:18 +08003314
Elliott Hughesadb8c672012-03-06 16:49:32 -08003315 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC);
Logan Chienc3f7d962011-12-27 18:13:18 +08003316
3317 llvm::Value* result_value =
3318 EmitIntArithmResultComputation(dex_pc, src_value, imm_value, arithm, kInt);
3319
Elliott Hughesadb8c672012-03-06 16:49:32 -08003320 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chienc3f7d962011-12-27 18:13:18 +08003321
Logan Chien70f94b42011-12-27 17:49:11 +08003322 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3323}
3324
3325
Logan Chienc3f7d962011-12-27 18:13:18 +08003326llvm::Value*
3327MethodCompiler::EmitIntArithmResultComputation(uint32_t dex_pc,
3328 llvm::Value* lhs,
3329 llvm::Value* rhs,
3330 IntArithmKind arithm,
3331 JType op_jty) {
3332 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3333
3334 switch (arithm) {
3335 case kIntArithm_Add:
3336 return irb_.CreateAdd(lhs, rhs);
3337
3338 case kIntArithm_Sub:
3339 return irb_.CreateSub(lhs, rhs);
3340
3341 case kIntArithm_Mul:
3342 return irb_.CreateMul(lhs, rhs);
3343
3344 case kIntArithm_Div:
Logan Chienc3f7d962011-12-27 18:13:18 +08003345 case kIntArithm_Rem:
TDYa127f8641ce2012-04-02 06:40:40 -07003346 return EmitIntDivRemResultComputation(dex_pc, lhs, rhs, arithm, op_jty);
Logan Chienc3f7d962011-12-27 18:13:18 +08003347
3348 case kIntArithm_And:
3349 return irb_.CreateAnd(lhs, rhs);
3350
3351 case kIntArithm_Or:
3352 return irb_.CreateOr(lhs, rhs);
3353
3354 case kIntArithm_Xor:
3355 return irb_.CreateXor(lhs, rhs);
3356
Logan Chienc3f7d962011-12-27 18:13:18 +08003357 default:
3358 LOG(FATAL) << "Unknown integer arithmetic kind: " << arithm;
3359 return NULL;
3360 }
3361}
3362
3363
TDYa127f8641ce2012-04-02 06:40:40 -07003364llvm::Value*
3365MethodCompiler::EmitIntDivRemResultComputation(uint32_t dex_pc,
3366 llvm::Value* dividend,
3367 llvm::Value* divisor,
3368 IntArithmKind arithm,
3369 JType op_jty) {
3370 // Throw exception if the divisor is 0.
3371 EmitGuard_DivZeroException(dex_pc, divisor, op_jty);
3372
3373 // Check the special case: MININT / -1 = MININT
3374 // That case will cause overflow, which is undefined behavior in llvm.
3375 // So we check the divisor is -1 or not, if the divisor is -1, we do
3376 // the special path to avoid undefined behavior.
3377 llvm::Type* op_type = irb_.getJType(op_jty, kAccurate);
3378 llvm::Value* zero = irb_.getJZero(op_jty);
3379 llvm::Value* neg_one = llvm::ConstantInt::getSigned(op_type, -1);
3380 llvm::Value* result = irb_.CreateAlloca(op_type);
3381
3382 llvm::BasicBlock* eq_neg_one = CreateBasicBlockWithDexPC(dex_pc, "eq_neg_one");
3383 llvm::BasicBlock* ne_neg_one = CreateBasicBlockWithDexPC(dex_pc, "ne_neg_one");
3384 llvm::BasicBlock* neg_one_cont = CreateBasicBlockWithDexPC(dex_pc, "neg_one_cont");
3385
3386 llvm::Value* is_equal_neg_one = EmitConditionResult(divisor, neg_one, kCondBranch_EQ);
3387 irb_.CreateCondBr(is_equal_neg_one, eq_neg_one, ne_neg_one);
3388
3389 // If divisor == -1
3390 irb_.SetInsertPoint(eq_neg_one);
3391 llvm::Value* eq_result;
3392 if (arithm == kIntArithm_Div) {
3393 // We can just change from "dividend div -1" to "neg dividend".
3394 // The sub don't care the sign/unsigned because of two's complement representation.
3395 // And the behavior is what we want:
3396 // -(2^n) (2^n)-1
3397 // MININT < k <= MAXINT -> mul k -1 = -k
3398 // MININT == k -> mul k -1 = k
3399 //
3400 // LLVM use sub to represent 'neg'
3401 eq_result = irb_.CreateSub(zero, dividend);
3402 } else {
3403 // Everything modulo -1 will be 0.
3404 eq_result = zero;
3405 }
3406 irb_.CreateStore(eq_result, result);
3407 irb_.CreateBr(neg_one_cont);
3408
3409 // If divisor != -1, just do the division.
3410 irb_.SetInsertPoint(ne_neg_one);
3411 llvm::Value* ne_result;
3412 if (arithm == kIntArithm_Div) {
3413 ne_result = irb_.CreateSDiv(dividend, divisor);
3414 } else {
3415 ne_result = irb_.CreateSRem(dividend, divisor);
3416 }
3417 irb_.CreateStore(ne_result, result);
3418 irb_.CreateBr(neg_one_cont);
3419
3420 irb_.SetInsertPoint(neg_one_cont);
3421 return irb_.CreateLoad(result);
3422}
3423
3424
Logan Chien5539ad02012-04-02 14:36:55 +08003425void MethodCompiler::EmitInsn_IntShiftArithm(uint32_t dex_pc,
3426 Instruction const* insn,
3427 IntShiftArithmKind arithm,
3428 JType op_jty,
3429 bool is_2addr) {
3430
3431 DecodedInstruction dec_insn(insn);
3432
3433 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3434
3435 llvm::Value* src1_value;
3436 llvm::Value* src2_value;
3437
3438 // NOTE: The 2nd operand of the shift arithmetic instruction is
3439 // 32-bit integer regardless of the 1st operand.
3440 if (is_2addr) {
3441 src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate);
3442 src2_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
3443 } else {
3444 src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
3445 src2_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate);
3446 }
3447
3448 llvm::Value* result_value =
3449 EmitIntShiftArithmResultComputation(dex_pc, src1_value, src2_value,
3450 arithm, op_jty);
3451
3452 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
3453
3454 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3455}
3456
3457
3458void MethodCompiler::
3459EmitInsn_IntShiftArithmImmediate(uint32_t dex_pc,
3460 Instruction const* insn,
3461 IntShiftArithmKind arithm) {
3462
3463 DecodedInstruction dec_insn(insn);
3464
3465 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
3466
3467 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC);
3468
3469 llvm::Value* result_value =
3470 EmitIntShiftArithmResultComputation(dex_pc, src_value, imm_value,
3471 arithm, kInt);
3472
3473 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
3474
3475 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3476}
3477
3478
3479llvm::Value*
3480MethodCompiler::EmitIntShiftArithmResultComputation(uint32_t dex_pc,
3481 llvm::Value* lhs,
3482 llvm::Value* rhs,
3483 IntShiftArithmKind arithm,
3484 JType op_jty) {
3485 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3486
3487 if (op_jty == kInt) {
3488 rhs = irb_.CreateAnd(rhs, 0x1f);
3489 } else {
3490 llvm::Value* masked_rhs = irb_.CreateAnd(rhs, 0x3f);
3491 rhs = irb_.CreateZExt(masked_rhs, irb_.getJLongTy());
3492 }
3493
3494 switch (arithm) {
3495 case kIntArithm_Shl:
3496 return irb_.CreateShl(lhs, rhs);
3497
3498 case kIntArithm_Shr:
3499 return irb_.CreateAShr(lhs, rhs);
3500
3501 case kIntArithm_UShr:
3502 return irb_.CreateLShr(lhs, rhs);
3503
3504 default:
3505 LOG(FATAL) << "Unknown integer shift arithmetic kind: " << arithm;
3506 return NULL;
3507 }
3508}
3509
3510
Logan Chien70f94b42011-12-27 17:49:11 +08003511void MethodCompiler::EmitInsn_RSubImmediate(uint32_t dex_pc,
3512 Instruction const* insn) {
Logan Chien65c62d42011-12-27 18:14:18 +08003513
Elliott Hughesadb8c672012-03-06 16:49:32 -08003514 DecodedInstruction dec_insn(insn);
Logan Chien65c62d42011-12-27 18:14:18 +08003515
Elliott Hughesadb8c672012-03-06 16:49:32 -08003516 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
3517 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC);
Logan Chien65c62d42011-12-27 18:14:18 +08003518 llvm::Value* result_value = irb_.CreateSub(imm_value, src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003519 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chien65c62d42011-12-27 18:14:18 +08003520
Logan Chien70f94b42011-12-27 17:49:11 +08003521 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3522}
3523
3524
3525void MethodCompiler::EmitInsn_FPArithm(uint32_t dex_pc,
3526 Instruction const* insn,
3527 FPArithmKind arithm,
3528 JType op_jty,
3529 bool is_2addr) {
Logan Chien76e1c792011-12-27 18:15:01 +08003530
Elliott Hughesadb8c672012-03-06 16:49:32 -08003531 DecodedInstruction dec_insn(insn);
Logan Chien76e1c792011-12-27 18:15:01 +08003532
3533 DCHECK(op_jty == kFloat || op_jty == kDouble) << op_jty;
3534
3535 llvm::Value* src1_value;
3536 llvm::Value* src2_value;
3537
3538 if (is_2addr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003539 src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate);
3540 src2_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chien76e1c792011-12-27 18:15:01 +08003541 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003542 src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
3543 src2_value = EmitLoadDalvikReg(dec_insn.vC, op_jty, kAccurate);
Logan Chien76e1c792011-12-27 18:15:01 +08003544 }
3545
3546 llvm::Value* result_value =
3547 EmitFPArithmResultComputation(dex_pc, src1_value, src2_value, arithm);
3548
Elliott Hughesadb8c672012-03-06 16:49:32 -08003549 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chien76e1c792011-12-27 18:15:01 +08003550
Logan Chien70f94b42011-12-27 17:49:11 +08003551 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3552}
3553
3554
Logan Chien76e1c792011-12-27 18:15:01 +08003555llvm::Value*
3556MethodCompiler::EmitFPArithmResultComputation(uint32_t dex_pc,
3557 llvm::Value *lhs,
3558 llvm::Value *rhs,
3559 FPArithmKind arithm) {
3560 switch (arithm) {
3561 case kFPArithm_Add:
3562 return irb_.CreateFAdd(lhs, rhs);
3563
3564 case kFPArithm_Sub:
3565 return irb_.CreateFSub(lhs, rhs);
3566
3567 case kFPArithm_Mul:
3568 return irb_.CreateFMul(lhs, rhs);
3569
3570 case kFPArithm_Div:
3571 return irb_.CreateFDiv(lhs, rhs);
3572
3573 case kFPArithm_Rem:
3574 return irb_.CreateFRem(lhs, rhs);
3575
3576 default:
3577 LOG(FATAL) << "Unknown floating-point arithmetic kind: " << arithm;
3578 return NULL;
3579 }
3580}
3581
3582
Logan Chienc3f7d962011-12-27 18:13:18 +08003583void MethodCompiler::EmitGuard_DivZeroException(uint32_t dex_pc,
3584 llvm::Value* denominator,
3585 JType op_jty) {
3586 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3587
3588 llvm::Constant* zero = irb_.getJZero(op_jty);
3589
3590 llvm::Value* equal_zero = irb_.CreateICmpEQ(denominator, zero);
3591
3592 llvm::BasicBlock* block_exception = CreateBasicBlockWithDexPC(dex_pc, "div0");
3593
3594 llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont");
3595
3596 irb_.CreateCondBr(equal_zero, block_exception, block_continue);
3597
3598 irb_.SetInsertPoint(block_exception);
Logan Chien8dfcbea2012-02-17 18:50:32 +08003599 EmitUpdateLineNumFromDexPC(dex_pc);
Logan Chienc3f7d962011-12-27 18:13:18 +08003600 irb_.CreateCall(irb_.GetRuntime(ThrowDivZeroException));
3601 EmitBranchExceptionLandingPad(dex_pc);
3602
3603 irb_.SetInsertPoint(block_continue);
3604}
3605
3606
Logan Chien61bb6142012-02-03 15:34:53 +08003607void MethodCompiler::EmitGuard_NullPointerException(uint32_t dex_pc,
3608 llvm::Value* object) {
3609 llvm::Value* equal_null = irb_.CreateICmpEQ(object, irb_.getJNull());
3610
3611 llvm::BasicBlock* block_exception =
3612 CreateBasicBlockWithDexPC(dex_pc, "nullp");
3613
3614 llvm::BasicBlock* block_continue =
3615 CreateBasicBlockWithDexPC(dex_pc, "cont");
3616
3617 irb_.CreateCondBr(equal_null, block_exception, block_continue);
3618
3619 irb_.SetInsertPoint(block_exception);
Logan Chien8dfcbea2012-02-17 18:50:32 +08003620 EmitUpdateLineNumFromDexPC(dex_pc);
TDYa1273f9137d2012-04-08 15:59:19 -07003621 irb_.CreateCall(irb_.GetRuntime(ThrowNullPointerException), irb_.getInt32(dex_pc));
Logan Chien61bb6142012-02-03 15:34:53 +08003622 EmitBranchExceptionLandingPad(dex_pc);
3623
3624 irb_.SetInsertPoint(block_continue);
3625}
3626
3627
Logan Chienbb4d12a2012-02-17 14:10:01 +08003628llvm::Value* MethodCompiler::EmitLoadDexCacheAddr(MemberOffset offset) {
3629 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
3630
3631 llvm::Value* dex_cache_offset_value =
3632 irb_.getPtrEquivInt(offset.Int32Value());
3633
3634 llvm::Value* dex_cache_field_addr =
3635 irb_.CreatePtrDisp(method_object_addr, dex_cache_offset_value,
3636 irb_.getJObjectTy()->getPointerTo());
3637
3638 return irb_.CreateLoad(dex_cache_field_addr);
3639}
3640
3641
Logan Chienbb4d12a2012-02-17 14:10:01 +08003642llvm::Value* MethodCompiler::
3643EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx) {
3644 llvm::Value* static_storage_dex_cache_addr =
3645 EmitLoadDexCacheAddr(Method::DexCacheInitializedStaticStorageOffset());
3646
3647 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
3648
3649 return EmitArrayGEP(static_storage_dex_cache_addr, type_idx_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08003650 irb_.getJObjectTy(), kObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003651}
3652
3653
3654llvm::Value* MethodCompiler::
3655EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx) {
3656 llvm::Value* resolved_type_dex_cache_addr =
3657 EmitLoadDexCacheAddr(Method::DexCacheResolvedTypesOffset());
3658
3659 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
3660
3661 return EmitArrayGEP(resolved_type_dex_cache_addr, type_idx_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08003662 irb_.getJObjectTy(), kObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003663}
3664
3665
3666llvm::Value* MethodCompiler::
Logan Chien61c65dc2012-02-29 03:22:30 +08003667EmitLoadDexCacheResolvedMethodFieldAddr(uint32_t method_idx) {
3668 llvm::Value* resolved_method_dex_cache_addr =
3669 EmitLoadDexCacheAddr(Method::DexCacheResolvedMethodsOffset());
3670
3671 llvm::Value* method_idx_value = irb_.getPtrEquivInt(method_idx);
3672
3673 return EmitArrayGEP(resolved_method_dex_cache_addr, method_idx_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08003674 irb_.getJObjectTy(), kObject);
Logan Chien61c65dc2012-02-29 03:22:30 +08003675}
3676
3677
3678llvm::Value* MethodCompiler::
Logan Chienbb4d12a2012-02-17 14:10:01 +08003679EmitLoadDexCacheStringFieldAddr(uint32_t string_idx) {
3680 llvm::Value* string_dex_cache_addr =
3681 EmitLoadDexCacheAddr(Method::DexCacheStringsOffset());
3682
3683 llvm::Value* string_idx_value = irb_.getPtrEquivInt(string_idx);
3684
3685 return EmitArrayGEP(string_dex_cache_addr, string_idx_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08003686 irb_.getJObjectTy(), kObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003687}
3688
3689
Logan Chien83426162011-12-09 09:29:50 +08003690CompiledMethod *MethodCompiler::Compile() {
Logan Chien0b827102011-12-20 19:46:14 +08003691 // Code generation
3692 CreateFunction();
3693
3694 EmitPrologue();
3695 EmitInstructions();
Logan Chienc670a8d2011-12-20 21:25:56 +08003696 EmitPrologueLastBranch();
Logan Chien0b827102011-12-20 19:46:14 +08003697
Logan Chiend6c239a2011-12-23 15:11:45 +08003698 // Verify the generated bitcode
3699 llvm::verifyFunction(*func_, llvm::PrintMessageAction);
3700
Logan Chien8b977d32012-02-21 19:14:55 +08003701 // Add the memory usage approximation of the compilation unit
3702 cunit_->AddMemUsageApproximation(code_item_->insns_size_in_code_units_ * 900);
3703 // NOTE: From statistic, the bitcode size is 4.5 times bigger than the
3704 // Dex file. Besides, we have to convert the code unit into bytes.
3705 // Thus, we got our magic number 9.
3706
Logan Chien110bcba2012-04-16 19:11:28 +08003707 CompiledMethod* compiled_method =
3708 new CompiledMethod(cunit_->GetInstructionSet(),
3709 cunit_->GetElfIndex(),
3710 elf_func_idx_);
3711
3712 cunit_->RegisterCompiledMethod(func_, compiled_method);
3713
3714 return compiled_method;
Logan Chien0b827102011-12-20 19:46:14 +08003715}
3716
3717
3718llvm::Value* MethodCompiler::EmitLoadMethodObjectAddr() {
3719 return func_->arg_begin();
Shih-wei Liaod1fec812012-02-13 09:51:10 -08003720}
Logan Chien83426162011-12-09 09:29:50 +08003721
3722
Logan Chien5bcc04e2012-01-30 14:15:12 +08003723void MethodCompiler::EmitBranchExceptionLandingPad(uint32_t dex_pc) {
3724 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
3725 irb_.CreateBr(lpad);
3726 } else {
3727 irb_.CreateBr(GetUnwindBasicBlock());
3728 }
3729}
3730
3731
3732void MethodCompiler::EmitGuard_ExceptionLandingPad(uint32_t dex_pc) {
3733 llvm::Value* exception_pending =
3734 irb_.CreateCall(irb_.GetRuntime(IsExceptionPending));
3735
3736 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
3737
3738 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
3739 irb_.CreateCondBr(exception_pending, lpad, block_cont);
3740 } else {
3741 irb_.CreateCondBr(exception_pending, GetUnwindBasicBlock(), block_cont);
3742 }
3743
3744 irb_.SetInsertPoint(block_cont);
3745}
3746
3747
Logan Chien924072f2012-01-30 15:07:24 +08003748void MethodCompiler::EmitGuard_GarbageCollectionSuspend(uint32_t dex_pc) {
3749 llvm::Value* runtime_func = irb_.GetRuntime(TestSuspend);
Logan Chien8dfcbea2012-02-17 18:50:32 +08003750 EmitUpdateLineNumFromDexPC(dex_pc);
Logan Chien924072f2012-01-30 15:07:24 +08003751 irb_.CreateCall(runtime_func);
3752
3753 EmitGuard_ExceptionLandingPad(dex_pc);
3754}
3755
3756
Logan Chiend6c239a2011-12-23 15:11:45 +08003757llvm::BasicBlock* MethodCompiler::
3758CreateBasicBlockWithDexPC(uint32_t dex_pc, char const* postfix) {
3759 std::string name;
3760
3761 if (postfix) {
3762 StringAppendF(&name, "B%u.%s", dex_pc, postfix);
3763 } else {
3764 StringAppendF(&name, "B%u", dex_pc);
3765 }
3766
3767 return llvm::BasicBlock::Create(*context_, name, func_);
3768}
3769
3770
3771llvm::BasicBlock* MethodCompiler::GetBasicBlock(uint32_t dex_pc) {
3772 DCHECK(dex_pc < code_item_->insns_size_in_code_units_);
3773
3774 llvm::BasicBlock* basic_block = basic_blocks_[dex_pc];
3775
3776 if (!basic_block) {
3777 basic_block = CreateBasicBlockWithDexPC(dex_pc);
3778 basic_blocks_[dex_pc] = basic_block;
3779 }
3780
3781 return basic_block;
3782}
3783
3784
3785llvm::BasicBlock*
3786MethodCompiler::GetNextBasicBlock(uint32_t dex_pc) {
3787 Instruction const* insn = Instruction::At(code_item_->insns_ + dex_pc);
3788 return GetBasicBlock(dex_pc + insn->SizeInCodeUnits());
3789}
3790
3791
Logan Chien5bcc04e2012-01-30 14:15:12 +08003792int32_t MethodCompiler::GetTryItemOffset(uint32_t dex_pc) {
3793 // TODO: Since we are emitting the dex instructions in ascending order
3794 // w.r.t. address, we can cache the lastest try item offset so that we
3795 // don't have to do binary search for every query.
3796
3797 int32_t min = 0;
3798 int32_t max = code_item_->tries_size_ - 1;
3799
3800 while (min <= max) {
3801 int32_t mid = min + (max - min) / 2;
3802
3803 DexFile::TryItem const* ti = DexFile::GetTryItems(*code_item_, mid);
3804 uint32_t start = ti->start_addr_;
3805 uint32_t end = start + ti->insn_count_;
3806
3807 if (dex_pc < start) {
3808 max = mid - 1;
3809 } else if (dex_pc >= end) {
3810 min = mid + 1;
3811 } else {
3812 return mid; // found
3813 }
3814 }
3815
3816 return -1; // not found
3817}
3818
3819
3820llvm::BasicBlock* MethodCompiler::GetLandingPadBasicBlock(uint32_t dex_pc) {
3821 // Find the try item for this address in this method
3822 int32_t ti_offset = GetTryItemOffset(dex_pc);
3823
3824 if (ti_offset == -1) {
3825 return NULL; // No landing pad is available for this address.
3826 }
3827
3828 // Check for the existing landing pad basic block
3829 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
3830 llvm::BasicBlock* block_lpad = basic_block_landing_pads_[ti_offset];
3831
3832 if (block_lpad) {
3833 // We have generated landing pad for this try item already. Return the
3834 // same basic block.
3835 return block_lpad;
3836 }
3837
3838 // Get try item from code item
3839 DexFile::TryItem const* ti = DexFile::GetTryItems(*code_item_, ti_offset);
3840
3841 // Create landing pad basic block
3842 block_lpad = llvm::BasicBlock::Create(*context_,
3843 StringPrintf("lpad%d", ti_offset),
3844 func_);
3845
3846 // Change IRBuilder insert point
3847 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3848 irb_.SetInsertPoint(block_lpad);
3849
3850 // Find catch block with matching type
3851 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
3852
3853 // TODO: Maybe passing try item offset will be a better idea? For now,
3854 // we are passing dex_pc, so that we can use existing runtime support
3855 // function directly. However, in the runtime supporting function we
3856 // have to search for try item with binary search which can be
3857 // eliminated.
3858 llvm::Value* dex_pc_value = irb_.getInt32(ti->start_addr_);
3859
3860 llvm::Value* catch_handler_index_value =
3861 irb_.CreateCall2(irb_.GetRuntime(FindCatchBlock),
3862 method_object_addr, dex_pc_value);
3863
3864 // Switch instruction (Go to unwind basic block by default)
3865 llvm::SwitchInst* sw =
3866 irb_.CreateSwitch(catch_handler_index_value, GetUnwindBasicBlock());
3867
3868 // Cases with matched catch block
3869 CatchHandlerIterator iter(*code_item_, ti->start_addr_);
3870
3871 for (uint32_t c = 0; iter.HasNext(); iter.Next(), ++c) {
3872 sw->addCase(irb_.getInt32(c), GetBasicBlock(iter.GetHandlerAddress()));
3873 }
3874
3875 // Restore the orignal insert point for IRBuilder
3876 irb_.restoreIP(irb_ip_original);
3877
3878 // Cache this landing pad
3879 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
3880 basic_block_landing_pads_[ti_offset] = block_lpad;
3881
3882 return block_lpad;
3883}
3884
3885
3886llvm::BasicBlock* MethodCompiler::GetUnwindBasicBlock() {
3887 // Check the existing unwinding baisc block block
3888 if (basic_block_unwind_ != NULL) {
3889 return basic_block_unwind_;
3890 }
3891
3892 // Create new basic block for unwinding
3893 basic_block_unwind_ =
3894 llvm::BasicBlock::Create(*context_, "exception_unwind", func_);
3895
3896 // Change IRBuilder insert point
3897 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3898 irb_.SetInsertPoint(basic_block_unwind_);
3899
Logan Chien8dfcbea2012-02-17 18:50:32 +08003900 // Pop the shadow frame
3901 EmitPopShadowFrame();
3902
Logan Chien5bcc04e2012-01-30 14:15:12 +08003903 // Emit the code to return default value (zero) for the given return type.
Logan Chiendd361c92012-04-10 23:40:37 +08003904 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
Logan Chien5bcc04e2012-01-30 14:15:12 +08003905 if (ret_shorty == 'V') {
3906 irb_.CreateRetVoid();
3907 } else {
3908 irb_.CreateRet(irb_.getJZero(ret_shorty));
3909 }
3910
3911 // Restore the orignal insert point for IRBuilder
3912 irb_.restoreIP(irb_ip_original);
3913
3914 return basic_block_unwind_;
3915}
3916
3917
Logan Chienc670a8d2011-12-20 21:25:56 +08003918llvm::Value* MethodCompiler::AllocDalvikLocalVarReg(RegCategory cat,
3919 uint32_t reg_idx) {
3920
3921 // Save current IR builder insert point
3922 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3923
3924 // Alloca
3925 llvm::Value* reg_addr = NULL;
3926
3927 switch (cat) {
3928 case kRegCat1nr:
3929 irb_.SetInsertPoint(basic_block_reg_alloca_);
3930 reg_addr = irb_.CreateAlloca(irb_.getJIntTy(), 0,
3931 StringPrintf("r%u", reg_idx));
3932
3933 irb_.SetInsertPoint(basic_block_reg_zero_init_);
3934 irb_.CreateStore(irb_.getJInt(0), reg_addr);
3935 break;
3936
3937 case kRegCat2:
3938 irb_.SetInsertPoint(basic_block_reg_alloca_);
3939 reg_addr = irb_.CreateAlloca(irb_.getJLongTy(), 0,
3940 StringPrintf("w%u", reg_idx));
3941
3942 irb_.SetInsertPoint(basic_block_reg_zero_init_);
3943 irb_.CreateStore(irb_.getJLong(0), reg_addr);
3944 break;
3945
3946 case kRegObject:
Logan Chien8dfcbea2012-02-17 18:50:32 +08003947 {
3948 irb_.SetInsertPoint(basic_block_shadow_frame_alloca_);
Logan Chienc670a8d2011-12-20 21:25:56 +08003949
Logan Chien8dfcbea2012-02-17 18:50:32 +08003950 llvm::Value* gep_index[] = {
3951 irb_.getInt32(0), // No pointer displacement
3952 irb_.getInt32(1), // SIRT
3953 irb_.getInt32(reg_idx) // Pointer field
3954 };
3955
3956 reg_addr = irb_.CreateGEP(shadow_frame_, gep_index,
3957 StringPrintf("p%u", reg_idx));
3958
3959 irb_.SetInsertPoint(basic_block_reg_zero_init_);
3960 irb_.CreateStore(irb_.getJNull(), reg_addr);
3961 }
Logan Chienc670a8d2011-12-20 21:25:56 +08003962 break;
3963
3964 default:
3965 LOG(FATAL) << "Unknown register category for allocation: " << cat;
3966 }
3967
3968 // Restore IRBuilder insert point
3969 irb_.restoreIP(irb_ip_original);
3970
3971 DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL));
3972 return reg_addr;
3973}
3974
3975
3976llvm::Value* MethodCompiler::AllocDalvikRetValReg(RegCategory cat) {
3977 // Save current IR builder insert point
3978 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3979
3980 // Alloca
3981 llvm::Value* reg_addr = NULL;
3982
3983 switch (cat) {
3984 case kRegCat1nr:
3985 irb_.SetInsertPoint(basic_block_reg_alloca_);
3986 reg_addr = irb_.CreateAlloca(irb_.getJIntTy(), 0, "r_res");
3987 break;
3988
3989 case kRegCat2:
3990 irb_.SetInsertPoint(basic_block_reg_alloca_);
3991 reg_addr = irb_.CreateAlloca(irb_.getJLongTy(), 0, "w_res");
3992 break;
3993
3994 case kRegObject:
3995 irb_.SetInsertPoint(basic_block_reg_alloca_);
3996 reg_addr = irb_.CreateAlloca(irb_.getJObjectTy(), 0, "p_res");
3997 break;
3998
3999 default:
4000 LOG(FATAL) << "Unknown register category for allocation: " << cat;
4001 }
4002
4003 // Restore IRBuilder insert point
4004 irb_.restoreIP(irb_ip_original);
4005
4006 DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL));
4007 return reg_addr;
4008}
4009
4010
Logan Chien8dfcbea2012-02-17 18:50:32 +08004011void MethodCompiler::EmitPopShadowFrame() {
4012 irb_.CreateCall(irb_.GetRuntime(PopShadowFrame));
4013}
4014
4015
4016void MethodCompiler::EmitUpdateLineNum(int32_t line_num) {
Logan Chien1b0a1b72012-03-15 06:20:17 +08004017 llvm::Value* line_num_field_addr =
4018 irb_.CreatePtrDisp(shadow_frame_,
4019 irb_.getPtrEquivInt(ShadowFrame::LineNumOffset()),
4020 irb_.getJIntTy()->getPointerTo());
Logan Chien8dfcbea2012-02-17 18:50:32 +08004021
Logan Chien8dfcbea2012-02-17 18:50:32 +08004022 llvm::ConstantInt* line_num_value = irb_.getInt32(line_num);
4023 irb_.CreateStore(line_num_value, line_num_field_addr);
4024}
4025
4026
4027void MethodCompiler::EmitUpdateLineNumFromDexPC(uint32_t dex_pc) {
Logan Chiendd361c92012-04-10 23:40:37 +08004028 EmitUpdateLineNum(
4029 dex_file_->GetLineNumFromPC(oat_compilation_unit_->IsStatic(),
4030 method_idx_, code_item_, dex_pc));
Logan Chien8dfcbea2012-02-17 18:50:32 +08004031}
4032
4033
Logan Chien83426162011-12-09 09:29:50 +08004034} // namespace compiler_llvm
4035} // namespace art