blob: 2d69290e8b6d3453912c82357624a7e007ab657c [file] [log] [blame]
Shih-wei Liaod1fec812012-02-13 09:51:10 -08001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "method_compiler.h"
18
Logan Chienfca7e872011-12-20 20:08:22 +080019#include "backend_types.h"
Logan Chien8b977d32012-02-21 19:14:55 +080020#include "compilation_unit.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080021#include "compiler.h"
Logan Chiena78e3c82011-12-27 17:59:35 +080022#include "inferred_reg_category_map.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080023#include "ir_builder.h"
24#include "logging.h"
Logan Chien4dd96f52012-02-29 01:26:58 +080025#include "oat_compilation_unit.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080026#include "object.h"
27#include "object_utils.h"
Logan Chien42e0e152012-01-13 15:42:36 +080028#include "runtime_support_func.h"
TDYa1275bb86012012-04-11 05:57:28 -070029#include "runtime_support_llvm.h"
Logan Chien1b0a1b72012-03-15 06:20:17 +080030#include "shadow_frame.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080031#include "stl_util.h"
Logan Chien0b827102011-12-20 19:46:14 +080032#include "stringprintf.h"
33#include "utils_llvm.h"
Ian Rogers776ac1f2012-04-13 23:36:36 -070034#include "verifier/method_verifier.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080035
36#include <iomanip>
37
Logan Chienc670a8d2011-12-20 21:25:56 +080038#include <llvm/BasicBlock.h>
Shih-wei Liaod1fec812012-02-13 09:51:10 -080039#include <llvm/Function.h>
Logan Chiena85fb2f2012-01-16 12:52:56 +080040#include <llvm/GlobalVariable.h>
41#include <llvm/Intrinsics.h>
Shih-wei Liaod1fec812012-02-13 09:51:10 -080042
Logan Chien83426162011-12-09 09:29:50 +080043namespace art {
44namespace compiler_llvm {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080045
Logan Chien42e0e152012-01-13 15:42:36 +080046using namespace runtime_support;
47
Shih-wei Liaod1fec812012-02-13 09:51:10 -080048
Logan Chien8b977d32012-02-21 19:14:55 +080049MethodCompiler::MethodCompiler(CompilationUnit* cunit,
Logan Chien83426162011-12-09 09:29:50 +080050 Compiler* compiler,
Logan Chien4dd96f52012-02-29 01:26:58 +080051 OatCompilationUnit* oat_compilation_unit)
Logan Chien8b977d32012-02-21 19:14:55 +080052 : cunit_(cunit), compiler_(compiler),
53 class_linker_(oat_compilation_unit->class_linker_),
54 class_loader_(oat_compilation_unit->class_loader_),
55 dex_file_(oat_compilation_unit->dex_file_),
56 dex_cache_(oat_compilation_unit->dex_cache_),
57 code_item_(oat_compilation_unit->code_item_),
58 oat_compilation_unit_(oat_compilation_unit),
Logan Chien8b977d32012-02-21 19:14:55 +080059 method_idx_(oat_compilation_unit->method_idx_),
60 access_flags_(oat_compilation_unit->access_flags_),
61 module_(cunit->GetModule()),
62 context_(cunit->GetLLVMContext()),
63 irb_(*cunit->GetIRBuilder()), func_(NULL), retval_reg_(NULL),
Ian Rogers776ac1f2012-04-13 23:36:36 -070064 basic_block_stack_overflow_(NULL),
Logan Chien8b977d32012-02-21 19:14:55 +080065 basic_block_reg_alloca_(NULL), basic_block_shadow_frame_alloca_(NULL),
Logan Chienef4a6562012-04-24 18:02:24 +080066 basic_block_reg_arg_init_(NULL),
Logan Chien8b977d32012-02-21 19:14:55 +080067 basic_blocks_(code_item_->insns_size_in_code_units_),
68 basic_block_landing_pads_(code_item_->tries_size_, NULL),
69 basic_block_unwind_(NULL), basic_block_unreachable_(NULL),
Logan Chien937105a2012-04-02 02:37:37 +080070 shadow_frame_(NULL), elf_func_idx_(cunit_->AcquireUniqueElfFuncIndex()) {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080071}
72
73
74MethodCompiler::~MethodCompiler() {
Logan Chienc670a8d2011-12-20 21:25:56 +080075 STLDeleteElements(&regs_);
Shih-wei Liaod1fec812012-02-13 09:51:10 -080076}
77
78
Logan Chien0b827102011-12-20 19:46:14 +080079void MethodCompiler::CreateFunction() {
80 // LLVM function name
Logan Chien937105a2012-04-02 02:37:37 +080081 std::string func_name(ElfFuncName(elf_func_idx_));
Logan Chien0b827102011-12-20 19:46:14 +080082
83 // Get function type
84 llvm::FunctionType* func_type =
Logan Chiendd361c92012-04-10 23:40:37 +080085 GetFunctionType(method_idx_, oat_compilation_unit_->IsStatic());
Logan Chien0b827102011-12-20 19:46:14 +080086
87 // Create function
88 func_ = llvm::Function::Create(func_type, llvm::Function::ExternalLinkage,
89 func_name, module_);
90
91 // Set argument name
92 llvm::Function::arg_iterator arg_iter(func_->arg_begin());
93 llvm::Function::arg_iterator arg_end(func_->arg_end());
94
95 DCHECK_NE(arg_iter, arg_end);
96 arg_iter->setName("method");
97 ++arg_iter;
98
Logan Chiendd361c92012-04-10 23:40:37 +080099 if (!oat_compilation_unit_->IsStatic()) {
Logan Chien0b827102011-12-20 19:46:14 +0800100 DCHECK_NE(arg_iter, arg_end);
101 arg_iter->setName("this");
102 ++arg_iter;
103 }
104
105 for (unsigned i = 0; arg_iter != arg_end; ++i, ++arg_iter) {
106 arg_iter->setName(StringPrintf("a%u", i));
107 }
108}
109
110
111llvm::FunctionType* MethodCompiler::GetFunctionType(uint32_t method_idx,
112 bool is_static) {
113 // Get method signature
114 DexFile::MethodId const& method_id = dex_file_->GetMethodId(method_idx);
115
Logan Chien8faf8022012-02-24 12:25:29 +0800116 uint32_t shorty_size;
Logan Chien0b827102011-12-20 19:46:14 +0800117 char const* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size);
Logan Chien8faf8022012-02-24 12:25:29 +0800118 CHECK_GE(shorty_size, 1u);
Logan Chien0b827102011-12-20 19:46:14 +0800119
120 // Get return type
121 llvm::Type* ret_type = irb_.getJType(shorty[0], kAccurate);
122
123 // Get argument type
124 std::vector<llvm::Type*> args_type;
125
126 args_type.push_back(irb_.getJObjectTy()); // method object pointer
127
128 if (!is_static) {
129 args_type.push_back(irb_.getJType('L', kAccurate)); // "this" object pointer
130 }
131
Logan Chien8faf8022012-02-24 12:25:29 +0800132 for (uint32_t i = 1; i < shorty_size; ++i) {
Logan Chien0b827102011-12-20 19:46:14 +0800133 args_type.push_back(irb_.getJType(shorty[i], kAccurate));
134 }
135
136 return llvm::FunctionType::get(ret_type, args_type, false);
137}
138
139
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800140void MethodCompiler::EmitPrologue() {
Logan Chienc670a8d2011-12-20 21:25:56 +0800141 // Create basic blocks for prologue
TDYa12799489132012-04-29 01:27:58 -0700142#if !defined(NDEBUG)
143 // Add a BasicBlock named as PrettyMethod for debugging.
144 llvm::BasicBlock* entry =
145 llvm::BasicBlock::Create(*context_, PrettyMethod(method_idx_, *dex_file_), func_);
146#endif
Logan Chienc670a8d2011-12-20 21:25:56 +0800147 basic_block_reg_alloca_ =
148 llvm::BasicBlock::Create(*context_, "prologue.alloca", func_);
149
TDYa12797339c42012-04-29 01:26:35 -0700150 basic_block_stack_overflow_ =
151 llvm::BasicBlock::Create(*context_, "prologue.stack_overflow_check", func_);
152
Logan Chien8dfcbea2012-02-17 18:50:32 +0800153 basic_block_shadow_frame_alloca_ =
154 llvm::BasicBlock::Create(*context_, "prologue.shadowframe", func_);
155
Logan Chiend6ececa2011-12-27 16:20:15 +0800156 basic_block_reg_arg_init_ =
157 llvm::BasicBlock::Create(*context_, "prologue.arginit", func_);
158
TDYa12799489132012-04-29 01:27:58 -0700159#if !defined(NDEBUG)
160 irb_.SetInsertPoint(entry);
161 irb_.CreateBr(basic_block_reg_alloca_);
162#endif
163
Logan Chienc670a8d2011-12-20 21:25:56 +0800164 // Create register array
165 for (uint16_t r = 0; r < code_item_->registers_size_; ++r) {
166 regs_.push_back(DalvikReg::CreateLocalVarReg(*this, r));
167 }
168
169 retval_reg_.reset(DalvikReg::CreateRetValReg(*this));
Logan Chiend6ececa2011-12-27 16:20:15 +0800170
Logan Chien8dfcbea2012-02-17 18:50:32 +0800171 // Create Shadow Frame
172 EmitPrologueAllocShadowFrame();
173
Logan Chiend6ececa2011-12-27 16:20:15 +0800174 // Store argument to dalvik register
175 irb_.SetInsertPoint(basic_block_reg_arg_init_);
176 EmitPrologueAssignArgRegister();
177
178 // Branch to start address
179 irb_.CreateBr(GetBasicBlock(0));
Logan Chienc670a8d2011-12-20 21:25:56 +0800180}
181
182
TDYa1274165a832012-04-03 17:47:16 -0700183void MethodCompiler::EmitStackOverflowCheck() {
184 irb_.SetInsertPoint(basic_block_stack_overflow_);
185
186 // Call llvm intrinsic function to get frame address.
187 llvm::Function* frameaddress =
188 llvm::Intrinsic::getDeclaration(module_, llvm::Intrinsic::frameaddress);
189
190 // The type of llvm::frameaddress is: i8* @llvm.frameaddress(i32)
191 llvm::Value* frame_address = irb_.CreateCall(frameaddress, irb_.getInt32(0));
192
193 // Cast i8* to int
194 frame_address = irb_.CreatePtrToInt(frame_address, irb_.getPtrEquivIntTy());
195
196 // Get thread.stack_end_
197 llvm::Value* thread_object_addr =
198 irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
199
TDYa127ee1f59b2012-04-25 00:56:40 -0700200 llvm::Value* stack_end =
201 irb_.LoadFromObjectOffset(thread_object_addr,
202 Thread::StackEndOffset().Int32Value(),
203 irb_.getPtrEquivIntTy());
TDYa1274165a832012-04-03 17:47:16 -0700204
205 // Check the frame address < thread.stack_end_ ?
206 llvm::Value* is_stack_overflow = irb_.CreateICmpULT(frame_address, stack_end);
207
208 llvm::BasicBlock* block_exception =
209 llvm::BasicBlock::Create(*context_, "stack_overflow", func_);
210
211 llvm::BasicBlock* block_continue =
212 llvm::BasicBlock::Create(*context_, "stack_overflow_cont", func_);
213
214 irb_.CreateCondBr(is_stack_overflow, block_exception, block_continue);
215
216 // If stack overflow, throw exception.
217 irb_.SetInsertPoint(block_exception);
218 irb_.CreateCall(irb_.GetRuntime(ThrowStackOverflowException));
219
220 // Unwind.
Logan Chiendd361c92012-04-10 23:40:37 +0800221 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
TDYa1274165a832012-04-03 17:47:16 -0700222 if (ret_shorty == 'V') {
223 irb_.CreateRetVoid();
224 } else {
225 irb_.CreateRet(irb_.getJZero(ret_shorty));
226 }
227
228 basic_block_stack_overflow_ = block_continue;
229}
230
231
Logan Chienc670a8d2011-12-20 21:25:56 +0800232void MethodCompiler::EmitPrologueLastBranch() {
233 irb_.SetInsertPoint(basic_block_reg_alloca_);
TDYa12797339c42012-04-29 01:26:35 -0700234 irb_.CreateBr(basic_block_stack_overflow_);
235
236 EmitStackOverflowCheck();
237
238 irb_.SetInsertPoint(basic_block_stack_overflow_);
Logan Chien8dfcbea2012-02-17 18:50:32 +0800239 irb_.CreateBr(basic_block_shadow_frame_alloca_);
240
241 irb_.SetInsertPoint(basic_block_shadow_frame_alloca_);
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
TDYa127ee1f59b2012-04-25 00:56:40 -0700264 // Get method object
Logan Chien8dfcbea2012-02-17 18:50:32 +0800265 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
TDYa127ee1f59b2012-04-25 00:56:40 -0700266
267 // Store the method pointer
268 irb_.StoreToObjectOffset(shadow_frame_,
269 ShadowFrame::MethodOffset(),
270 method_object_addr);
Logan Chien8dfcbea2012-02-17 18:50:32 +0800271
272 // Store the number of the pointer slots
TDYa127ee1f59b2012-04-25 00:56:40 -0700273 irb_.StoreToObjectOffset(shadow_frame_,
274 ShadowFrame::NumberOfReferencesOffset(),
275 irb_.getJInt(sirt_size));
Logan Chien8dfcbea2012-02-17 18:50:32 +0800276
277 // Push the shadow frame
278 llvm::Value* shadow_frame_upcast =
279 irb_.CreateConstGEP2_32(shadow_frame_, 0, 0);
280
281 irb_.CreateCall(irb_.GetRuntime(PushShadowFrame), shadow_frame_upcast);
282}
283
284
Logan Chiend6ececa2011-12-27 16:20:15 +0800285void MethodCompiler::EmitPrologueAssignArgRegister() {
286 uint16_t arg_reg = code_item_->registers_size_ - code_item_->ins_size_;
287
288 llvm::Function::arg_iterator arg_iter(func_->arg_begin());
289 llvm::Function::arg_iterator arg_end(func_->arg_end());
290
Logan Chiendd361c92012-04-10 23:40:37 +0800291 uint32_t shorty_size = 0;
292 char const* shorty = oat_compilation_unit_->GetShorty(&shorty_size);
293 CHECK_GE(shorty_size, 1u);
Logan Chiend6ececa2011-12-27 16:20:15 +0800294
295 ++arg_iter; // skip method object
296
Logan Chiendd361c92012-04-10 23:40:37 +0800297 if (!oat_compilation_unit_->IsStatic()) {
Logan Chiend6ececa2011-12-27 16:20:15 +0800298 EmitStoreDalvikReg(arg_reg, kObject, kAccurate, arg_iter);
299 ++arg_iter;
300 ++arg_reg;
301 }
302
Logan Chiendd361c92012-04-10 23:40:37 +0800303 for (uint32_t i = 1; i < shorty_size; ++i, ++arg_iter) {
Logan Chiend6ececa2011-12-27 16:20:15 +0800304 EmitStoreDalvikReg(arg_reg, shorty[i], kAccurate, arg_iter);
305
306 ++arg_reg;
307 if (shorty[i] == 'J' || shorty[i] == 'D') {
308 // Wide types, such as long and double, are using a pair of registers
309 // to store the value, so we have to increase arg_reg again.
310 ++arg_reg;
311 }
312 }
313
314 DCHECK_EQ(arg_end, arg_iter);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800315}
316
317
Logan Chien83426162011-12-09 09:29:50 +0800318void MethodCompiler::EmitInstructions() {
Logan Chiend6c239a2011-12-23 15:11:45 +0800319 uint32_t dex_pc = 0;
320 while (dex_pc < code_item_->insns_size_in_code_units_) {
321 Instruction const* insn = Instruction::At(code_item_->insns_ + dex_pc);
322 EmitInstruction(dex_pc, insn);
323 dex_pc += insn->SizeInCodeUnits();
324 }
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800325}
326
327
Logan Chien83426162011-12-09 09:29:50 +0800328void MethodCompiler::EmitInstruction(uint32_t dex_pc,
329 Instruction const* insn) {
Logan Chiend6c239a2011-12-23 15:11:45 +0800330
331 // Set the IRBuilder insertion point
332 irb_.SetInsertPoint(GetBasicBlock(dex_pc));
333
Logan Chien70f94b42011-12-27 17:49:11 +0800334#define ARGS dex_pc, insn
335
336 // Dispatch the instruction
337 switch (insn->Opcode()) {
338 case Instruction::NOP:
339 EmitInsn_Nop(ARGS);
340 break;
341
342 case Instruction::MOVE:
343 case Instruction::MOVE_FROM16:
344 case Instruction::MOVE_16:
345 EmitInsn_Move(ARGS, kInt);
346 break;
347
348 case Instruction::MOVE_WIDE:
349 case Instruction::MOVE_WIDE_FROM16:
350 case Instruction::MOVE_WIDE_16:
351 EmitInsn_Move(ARGS, kLong);
352 break;
353
354 case Instruction::MOVE_OBJECT:
355 case Instruction::MOVE_OBJECT_FROM16:
356 case Instruction::MOVE_OBJECT_16:
357 EmitInsn_Move(ARGS, kObject);
358 break;
359
360 case Instruction::MOVE_RESULT:
361 EmitInsn_MoveResult(ARGS, kInt);
362 break;
363
364 case Instruction::MOVE_RESULT_WIDE:
365 EmitInsn_MoveResult(ARGS, kLong);
366 break;
367
368 case Instruction::MOVE_RESULT_OBJECT:
369 EmitInsn_MoveResult(ARGS, kObject);
370 break;
371
372 case Instruction::MOVE_EXCEPTION:
373 EmitInsn_MoveException(ARGS);
374 break;
375
376 case Instruction::RETURN_VOID:
377 EmitInsn_ReturnVoid(ARGS);
378 break;
379
380 case Instruction::RETURN:
381 case Instruction::RETURN_WIDE:
382 case Instruction::RETURN_OBJECT:
383 EmitInsn_Return(ARGS);
384 break;
385
386 case Instruction::CONST_4:
387 case Instruction::CONST_16:
388 case Instruction::CONST:
389 case Instruction::CONST_HIGH16:
390 EmitInsn_LoadConstant(ARGS, kInt);
391 break;
392
393 case Instruction::CONST_WIDE_16:
394 case Instruction::CONST_WIDE_32:
395 case Instruction::CONST_WIDE:
396 case Instruction::CONST_WIDE_HIGH16:
397 EmitInsn_LoadConstant(ARGS, kLong);
398 break;
399
400 case Instruction::CONST_STRING:
401 case Instruction::CONST_STRING_JUMBO:
402 EmitInsn_LoadConstantString(ARGS);
403 break;
404
405 case Instruction::CONST_CLASS:
406 EmitInsn_LoadConstantClass(ARGS);
407 break;
408
409 case Instruction::MONITOR_ENTER:
410 EmitInsn_MonitorEnter(ARGS);
411 break;
412
413 case Instruction::MONITOR_EXIT:
414 EmitInsn_MonitorExit(ARGS);
415 break;
416
417 case Instruction::CHECK_CAST:
418 EmitInsn_CheckCast(ARGS);
419 break;
420
421 case Instruction::INSTANCE_OF:
422 EmitInsn_InstanceOf(ARGS);
423 break;
424
425 case Instruction::ARRAY_LENGTH:
426 EmitInsn_ArrayLength(ARGS);
427 break;
428
429 case Instruction::NEW_INSTANCE:
430 EmitInsn_NewInstance(ARGS);
431 break;
432
433 case Instruction::NEW_ARRAY:
434 EmitInsn_NewArray(ARGS);
435 break;
436
437 case Instruction::FILLED_NEW_ARRAY:
438 EmitInsn_FilledNewArray(ARGS, false);
439 break;
440
441 case Instruction::FILLED_NEW_ARRAY_RANGE:
442 EmitInsn_FilledNewArray(ARGS, true);
443 break;
444
445 case Instruction::FILL_ARRAY_DATA:
446 EmitInsn_FillArrayData(ARGS);
447 break;
448
449 case Instruction::THROW:
450 EmitInsn_ThrowException(ARGS);
451 break;
452
453 case Instruction::GOTO:
454 case Instruction::GOTO_16:
455 case Instruction::GOTO_32:
456 EmitInsn_UnconditionalBranch(ARGS);
457 break;
458
459 case Instruction::PACKED_SWITCH:
460 EmitInsn_PackedSwitch(ARGS);
461 break;
462
463 case Instruction::SPARSE_SWITCH:
464 EmitInsn_SparseSwitch(ARGS);
465 break;
466
467 case Instruction::CMPL_FLOAT:
468 EmitInsn_FPCompare(ARGS, kFloat, false);
469 break;
470
471 case Instruction::CMPG_FLOAT:
472 EmitInsn_FPCompare(ARGS, kFloat, true);
473 break;
474
475 case Instruction::CMPL_DOUBLE:
476 EmitInsn_FPCompare(ARGS, kDouble, false);
477 break;
478
479 case Instruction::CMPG_DOUBLE:
480 EmitInsn_FPCompare(ARGS, kDouble, true);
481 break;
482
483 case Instruction::CMP_LONG:
484 EmitInsn_LongCompare(ARGS);
485 break;
486
487 case Instruction::IF_EQ:
488 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_EQ);
489 break;
490
491 case Instruction::IF_NE:
492 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_NE);
493 break;
494
495 case Instruction::IF_LT:
496 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_LT);
497 break;
498
499 case Instruction::IF_GE:
500 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_GE);
501 break;
502
503 case Instruction::IF_GT:
504 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_GT);
505 break;
506
507 case Instruction::IF_LE:
508 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_LE);
509 break;
510
511 case Instruction::IF_EQZ:
512 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_EQ);
513 break;
514
515 case Instruction::IF_NEZ:
516 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_NE);
517 break;
518
519 case Instruction::IF_LTZ:
520 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_LT);
521 break;
522
523 case Instruction::IF_GEZ:
524 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_GE);
525 break;
526
527 case Instruction::IF_GTZ:
528 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_GT);
529 break;
530
531 case Instruction::IF_LEZ:
532 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_LE);
533 break;
534
535 case Instruction::AGET:
536 EmitInsn_AGet(ARGS, kInt);
537 break;
538
539 case Instruction::AGET_WIDE:
540 EmitInsn_AGet(ARGS, kLong);
541 break;
542
543 case Instruction::AGET_OBJECT:
544 EmitInsn_AGet(ARGS, kObject);
545 break;
546
547 case Instruction::AGET_BOOLEAN:
548 EmitInsn_AGet(ARGS, kBoolean);
549 break;
550
551 case Instruction::AGET_BYTE:
552 EmitInsn_AGet(ARGS, kByte);
553 break;
554
555 case Instruction::AGET_CHAR:
556 EmitInsn_AGet(ARGS, kChar);
557 break;
558
559 case Instruction::AGET_SHORT:
560 EmitInsn_AGet(ARGS, kShort);
561 break;
562
563 case Instruction::APUT:
564 EmitInsn_APut(ARGS, kInt);
565 break;
566
567 case Instruction::APUT_WIDE:
568 EmitInsn_APut(ARGS, kLong);
569 break;
570
571 case Instruction::APUT_OBJECT:
572 EmitInsn_APut(ARGS, kObject);
573 break;
574
575 case Instruction::APUT_BOOLEAN:
576 EmitInsn_APut(ARGS, kBoolean);
577 break;
578
579 case Instruction::APUT_BYTE:
580 EmitInsn_APut(ARGS, kByte);
581 break;
582
583 case Instruction::APUT_CHAR:
584 EmitInsn_APut(ARGS, kChar);
585 break;
586
587 case Instruction::APUT_SHORT:
588 EmitInsn_APut(ARGS, kShort);
589 break;
590
591 case Instruction::IGET:
592 EmitInsn_IGet(ARGS, kInt);
593 break;
594
595 case Instruction::IGET_WIDE:
596 EmitInsn_IGet(ARGS, kLong);
597 break;
598
599 case Instruction::IGET_OBJECT:
600 EmitInsn_IGet(ARGS, kObject);
601 break;
602
603 case Instruction::IGET_BOOLEAN:
604 EmitInsn_IGet(ARGS, kBoolean);
605 break;
606
607 case Instruction::IGET_BYTE:
608 EmitInsn_IGet(ARGS, kByte);
609 break;
610
611 case Instruction::IGET_CHAR:
612 EmitInsn_IGet(ARGS, kChar);
613 break;
614
615 case Instruction::IGET_SHORT:
616 EmitInsn_IGet(ARGS, kShort);
617 break;
618
619 case Instruction::IPUT:
620 EmitInsn_IPut(ARGS, kInt);
621 break;
622
623 case Instruction::IPUT_WIDE:
624 EmitInsn_IPut(ARGS, kLong);
625 break;
626
627 case Instruction::IPUT_OBJECT:
628 EmitInsn_IPut(ARGS, kObject);
629 break;
630
631 case Instruction::IPUT_BOOLEAN:
632 EmitInsn_IPut(ARGS, kBoolean);
633 break;
634
635 case Instruction::IPUT_BYTE:
636 EmitInsn_IPut(ARGS, kByte);
637 break;
638
639 case Instruction::IPUT_CHAR:
640 EmitInsn_IPut(ARGS, kChar);
641 break;
642
643 case Instruction::IPUT_SHORT:
644 EmitInsn_IPut(ARGS, kShort);
645 break;
646
647 case Instruction::SGET:
648 EmitInsn_SGet(ARGS, kInt);
649 break;
650
651 case Instruction::SGET_WIDE:
652 EmitInsn_SGet(ARGS, kLong);
653 break;
654
655 case Instruction::SGET_OBJECT:
656 EmitInsn_SGet(ARGS, kObject);
657 break;
658
659 case Instruction::SGET_BOOLEAN:
660 EmitInsn_SGet(ARGS, kBoolean);
661 break;
662
663 case Instruction::SGET_BYTE:
664 EmitInsn_SGet(ARGS, kByte);
665 break;
666
667 case Instruction::SGET_CHAR:
668 EmitInsn_SGet(ARGS, kChar);
669 break;
670
671 case Instruction::SGET_SHORT:
672 EmitInsn_SGet(ARGS, kShort);
673 break;
674
675 case Instruction::SPUT:
676 EmitInsn_SPut(ARGS, kInt);
677 break;
678
679 case Instruction::SPUT_WIDE:
680 EmitInsn_SPut(ARGS, kLong);
681 break;
682
683 case Instruction::SPUT_OBJECT:
684 EmitInsn_SPut(ARGS, kObject);
685 break;
686
687 case Instruction::SPUT_BOOLEAN:
688 EmitInsn_SPut(ARGS, kBoolean);
689 break;
690
691 case Instruction::SPUT_BYTE:
692 EmitInsn_SPut(ARGS, kByte);
693 break;
694
695 case Instruction::SPUT_CHAR:
696 EmitInsn_SPut(ARGS, kChar);
697 break;
698
699 case Instruction::SPUT_SHORT:
700 EmitInsn_SPut(ARGS, kShort);
701 break;
702
703
704 case Instruction::INVOKE_VIRTUAL:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800705 EmitInsn_Invoke(ARGS, kVirtual, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800706 break;
707
708 case Instruction::INVOKE_SUPER:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800709 EmitInsn_Invoke(ARGS, kSuper, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800710 break;
711
712 case Instruction::INVOKE_DIRECT:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800713 EmitInsn_Invoke(ARGS, kDirect, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800714 break;
715
716 case Instruction::INVOKE_STATIC:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800717 EmitInsn_Invoke(ARGS, kStatic, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800718 break;
719
720 case Instruction::INVOKE_INTERFACE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800721 EmitInsn_Invoke(ARGS, kInterface, kArgReg);
Logan Chien70f94b42011-12-27 17:49:11 +0800722 break;
723
724 case Instruction::INVOKE_VIRTUAL_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800725 EmitInsn_Invoke(ARGS, kVirtual, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800726 break;
727
728 case Instruction::INVOKE_SUPER_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800729 EmitInsn_Invoke(ARGS, kSuper, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800730 break;
731
732 case Instruction::INVOKE_DIRECT_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800733 EmitInsn_Invoke(ARGS, kDirect, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800734 break;
735
736 case Instruction::INVOKE_STATIC_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800737 EmitInsn_Invoke(ARGS, kStatic, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800738 break;
739
740 case Instruction::INVOKE_INTERFACE_RANGE:
Logan Chien7e7fabc2012-04-10 18:59:11 +0800741 EmitInsn_Invoke(ARGS, kInterface, kArgRange);
Logan Chien70f94b42011-12-27 17:49:11 +0800742 break;
743
744 case Instruction::NEG_INT:
745 EmitInsn_Neg(ARGS, kInt);
746 break;
747
748 case Instruction::NOT_INT:
749 EmitInsn_Not(ARGS, kInt);
750 break;
751
752 case Instruction::NEG_LONG:
753 EmitInsn_Neg(ARGS, kLong);
754 break;
755
756 case Instruction::NOT_LONG:
757 EmitInsn_Not(ARGS, kLong);
758 break;
759
760 case Instruction::NEG_FLOAT:
761 EmitInsn_FNeg(ARGS, kFloat);
762 break;
763
764 case Instruction::NEG_DOUBLE:
765 EmitInsn_FNeg(ARGS, kDouble);
766 break;
767
768 case Instruction::INT_TO_LONG:
769 EmitInsn_SExt(ARGS);
770 break;
771
772 case Instruction::INT_TO_FLOAT:
773 EmitInsn_IntToFP(ARGS, kInt, kFloat);
774 break;
775
776 case Instruction::INT_TO_DOUBLE:
777 EmitInsn_IntToFP(ARGS, kInt, kDouble);
778 break;
779
780 case Instruction::LONG_TO_INT:
781 EmitInsn_Trunc(ARGS);
782 break;
783
784 case Instruction::LONG_TO_FLOAT:
785 EmitInsn_IntToFP(ARGS, kLong, kFloat);
786 break;
787
788 case Instruction::LONG_TO_DOUBLE:
789 EmitInsn_IntToFP(ARGS, kLong, kDouble);
790 break;
791
792 case Instruction::FLOAT_TO_INT:
TDYa127a4746872012-04-11 23:48:55 -0700793 EmitInsn_FPToInt(ARGS, kFloat, kInt, F2I);
Logan Chien70f94b42011-12-27 17:49:11 +0800794 break;
795
796 case Instruction::FLOAT_TO_LONG:
TDYa127a4746872012-04-11 23:48:55 -0700797 EmitInsn_FPToInt(ARGS, kFloat, kLong, F2L);
Logan Chien70f94b42011-12-27 17:49:11 +0800798 break;
799
800 case Instruction::FLOAT_TO_DOUBLE:
801 EmitInsn_FExt(ARGS);
802 break;
803
804 case Instruction::DOUBLE_TO_INT:
TDYa127a4746872012-04-11 23:48:55 -0700805 EmitInsn_FPToInt(ARGS, kDouble, kInt, D2I);
Logan Chien70f94b42011-12-27 17:49:11 +0800806 break;
807
808 case Instruction::DOUBLE_TO_LONG:
TDYa127a4746872012-04-11 23:48:55 -0700809 EmitInsn_FPToInt(ARGS, kDouble, kLong, D2L);
Logan Chien70f94b42011-12-27 17:49:11 +0800810 break;
811
812 case Instruction::DOUBLE_TO_FLOAT:
813 EmitInsn_FTrunc(ARGS);
814 break;
815
816 case Instruction::INT_TO_BYTE:
817 EmitInsn_TruncAndSExt(ARGS, 8);
818 break;
819
820 case Instruction::INT_TO_CHAR:
821 EmitInsn_TruncAndZExt(ARGS, 16);
822 break;
823
824 case Instruction::INT_TO_SHORT:
825 EmitInsn_TruncAndSExt(ARGS, 16);
826 break;
827
828 case Instruction::ADD_INT:
829 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kInt, false);
830 break;
831
832 case Instruction::SUB_INT:
833 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kInt, false);
834 break;
835
836 case Instruction::MUL_INT:
837 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kInt, false);
838 break;
839
840 case Instruction::DIV_INT:
841 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kInt, false);
842 break;
843
844 case Instruction::REM_INT:
845 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kInt, false);
846 break;
847
848 case Instruction::AND_INT:
849 EmitInsn_IntArithm(ARGS, kIntArithm_And, kInt, false);
850 break;
851
852 case Instruction::OR_INT:
853 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kInt, false);
854 break;
855
856 case Instruction::XOR_INT:
857 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kInt, false);
858 break;
859
860 case Instruction::SHL_INT:
Logan Chien5539ad02012-04-02 14:36:55 +0800861 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kInt, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800862 break;
863
864 case Instruction::SHR_INT:
Logan Chien5539ad02012-04-02 14:36:55 +0800865 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kInt, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800866 break;
867
868 case Instruction::USHR_INT:
Logan Chien5539ad02012-04-02 14:36:55 +0800869 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kInt, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800870 break;
871
872 case Instruction::ADD_LONG:
873 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kLong, false);
874 break;
875
876 case Instruction::SUB_LONG:
877 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kLong, false);
878 break;
879
880 case Instruction::MUL_LONG:
881 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kLong, false);
882 break;
883
884 case Instruction::DIV_LONG:
885 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kLong, false);
886 break;
887
888 case Instruction::REM_LONG:
889 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kLong, false);
890 break;
891
892 case Instruction::AND_LONG:
893 EmitInsn_IntArithm(ARGS, kIntArithm_And, kLong, false);
894 break;
895
896 case Instruction::OR_LONG:
897 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kLong, false);
898 break;
899
900 case Instruction::XOR_LONG:
901 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kLong, false);
902 break;
903
904 case Instruction::SHL_LONG:
Logan Chien5539ad02012-04-02 14:36:55 +0800905 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kLong, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800906 break;
907
908 case Instruction::SHR_LONG:
Logan Chien5539ad02012-04-02 14:36:55 +0800909 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kLong, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800910 break;
911
912 case Instruction::USHR_LONG:
Logan Chien5539ad02012-04-02 14:36:55 +0800913 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kLong, false);
Logan Chien70f94b42011-12-27 17:49:11 +0800914 break;
915
916 case Instruction::ADD_FLOAT:
917 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kFloat, false);
918 break;
919
920 case Instruction::SUB_FLOAT:
921 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kFloat, false);
922 break;
923
924 case Instruction::MUL_FLOAT:
925 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kFloat, false);
926 break;
927
928 case Instruction::DIV_FLOAT:
929 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kFloat, false);
930 break;
931
932 case Instruction::REM_FLOAT:
933 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kFloat, false);
934 break;
935
936 case Instruction::ADD_DOUBLE:
937 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kDouble, false);
938 break;
939
940 case Instruction::SUB_DOUBLE:
941 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kDouble, false);
942 break;
943
944 case Instruction::MUL_DOUBLE:
945 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kDouble, false);
946 break;
947
948 case Instruction::DIV_DOUBLE:
949 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kDouble, false);
950 break;
951
952 case Instruction::REM_DOUBLE:
953 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kDouble, false);
954 break;
955
956 case Instruction::ADD_INT_2ADDR:
957 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kInt, true);
958 break;
959
960 case Instruction::SUB_INT_2ADDR:
961 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kInt, true);
962 break;
963
964 case Instruction::MUL_INT_2ADDR:
965 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kInt, true);
966 break;
967
968 case Instruction::DIV_INT_2ADDR:
969 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kInt, true);
970 break;
971
972 case Instruction::REM_INT_2ADDR:
973 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kInt, true);
974 break;
975
976 case Instruction::AND_INT_2ADDR:
977 EmitInsn_IntArithm(ARGS, kIntArithm_And, kInt, true);
978 break;
979
980 case Instruction::OR_INT_2ADDR:
981 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kInt, true);
982 break;
983
984 case Instruction::XOR_INT_2ADDR:
985 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kInt, true);
986 break;
987
988 case Instruction::SHL_INT_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +0800989 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kInt, true);
Logan Chien70f94b42011-12-27 17:49:11 +0800990 break;
991
992 case Instruction::SHR_INT_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +0800993 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kInt, true);
Logan Chien70f94b42011-12-27 17:49:11 +0800994 break;
995
996 case Instruction::USHR_INT_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +0800997 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kInt, true);
Logan Chien70f94b42011-12-27 17:49:11 +0800998 break;
999
1000 case Instruction::ADD_LONG_2ADDR:
1001 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kLong, true);
1002 break;
1003
1004 case Instruction::SUB_LONG_2ADDR:
1005 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kLong, true);
1006 break;
1007
1008 case Instruction::MUL_LONG_2ADDR:
1009 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kLong, true);
1010 break;
1011
1012 case Instruction::DIV_LONG_2ADDR:
1013 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kLong, true);
1014 break;
1015
1016 case Instruction::REM_LONG_2ADDR:
1017 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kLong, true);
1018 break;
1019
1020 case Instruction::AND_LONG_2ADDR:
1021 EmitInsn_IntArithm(ARGS, kIntArithm_And, kLong, true);
1022 break;
1023
1024 case Instruction::OR_LONG_2ADDR:
1025 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kLong, true);
1026 break;
1027
1028 case Instruction::XOR_LONG_2ADDR:
1029 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kLong, true);
1030 break;
1031
1032 case Instruction::SHL_LONG_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001033 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shl, kLong, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001034 break;
1035
1036 case Instruction::SHR_LONG_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001037 EmitInsn_IntShiftArithm(ARGS, kIntArithm_Shr, kLong, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001038 break;
1039
1040 case Instruction::USHR_LONG_2ADDR:
Logan Chien5539ad02012-04-02 14:36:55 +08001041 EmitInsn_IntShiftArithm(ARGS, kIntArithm_UShr, kLong, true);
Logan Chien70f94b42011-12-27 17:49:11 +08001042 break;
1043
1044 case Instruction::ADD_FLOAT_2ADDR:
1045 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kFloat, true);
1046 break;
1047
1048 case Instruction::SUB_FLOAT_2ADDR:
1049 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kFloat, true);
1050 break;
1051
1052 case Instruction::MUL_FLOAT_2ADDR:
1053 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kFloat, true);
1054 break;
1055
1056 case Instruction::DIV_FLOAT_2ADDR:
1057 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kFloat, true);
1058 break;
1059
1060 case Instruction::REM_FLOAT_2ADDR:
1061 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kFloat, true);
1062 break;
1063
1064 case Instruction::ADD_DOUBLE_2ADDR:
1065 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kDouble, true);
1066 break;
1067
1068 case Instruction::SUB_DOUBLE_2ADDR:
1069 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kDouble, true);
1070 break;
1071
1072 case Instruction::MUL_DOUBLE_2ADDR:
1073 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kDouble, true);
1074 break;
1075
1076 case Instruction::DIV_DOUBLE_2ADDR:
1077 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kDouble, true);
1078 break;
1079
1080 case Instruction::REM_DOUBLE_2ADDR:
1081 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kDouble, true);
1082 break;
1083
1084 case Instruction::ADD_INT_LIT16:
1085 case Instruction::ADD_INT_LIT8:
1086 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Add);
1087 break;
1088
1089 case Instruction::RSUB_INT:
1090 case Instruction::RSUB_INT_LIT8:
1091 EmitInsn_RSubImmediate(ARGS);
1092 break;
1093
1094 case Instruction::MUL_INT_LIT16:
1095 case Instruction::MUL_INT_LIT8:
1096 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Mul);
1097 break;
1098
1099 case Instruction::DIV_INT_LIT16:
1100 case Instruction::DIV_INT_LIT8:
1101 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Div);
1102 break;
1103
1104 case Instruction::REM_INT_LIT16:
1105 case Instruction::REM_INT_LIT8:
1106 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Rem);
1107 break;
1108
1109 case Instruction::AND_INT_LIT16:
1110 case Instruction::AND_INT_LIT8:
1111 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_And);
1112 break;
1113
1114 case Instruction::OR_INT_LIT16:
1115 case Instruction::OR_INT_LIT8:
1116 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Or);
1117 break;
1118
1119 case Instruction::XOR_INT_LIT16:
1120 case Instruction::XOR_INT_LIT8:
1121 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Xor);
1122 break;
1123
1124 case Instruction::SHL_INT_LIT8:
Logan Chien5539ad02012-04-02 14:36:55 +08001125 EmitInsn_IntShiftArithmImmediate(ARGS, kIntArithm_Shl);
Logan Chien70f94b42011-12-27 17:49:11 +08001126 break;
1127
1128 case Instruction::SHR_INT_LIT8:
Logan Chien5539ad02012-04-02 14:36:55 +08001129 EmitInsn_IntShiftArithmImmediate(ARGS, kIntArithm_Shr);
Logan Chien70f94b42011-12-27 17:49:11 +08001130 break;
1131
1132 case Instruction::USHR_INT_LIT8:
Logan Chien5539ad02012-04-02 14:36:55 +08001133 EmitInsn_IntShiftArithmImmediate(ARGS, kIntArithm_UShr);
Logan Chien70f94b42011-12-27 17:49:11 +08001134 break;
1135
Logan Chien9e5f5c12012-04-10 13:51:45 +08001136 case Instruction::THROW_VERIFICATION_ERROR:
1137 EmitInsn_ThrowVerificationError(ARGS);
1138 break;
1139
Logan Chien70f94b42011-12-27 17:49:11 +08001140 case Instruction::UNUSED_3E:
1141 case Instruction::UNUSED_3F:
1142 case Instruction::UNUSED_40:
1143 case Instruction::UNUSED_41:
1144 case Instruction::UNUSED_42:
1145 case Instruction::UNUSED_43:
1146 case Instruction::UNUSED_73:
1147 case Instruction::UNUSED_79:
1148 case Instruction::UNUSED_7A:
1149 case Instruction::UNUSED_E3:
1150 case Instruction::UNUSED_E4:
1151 case Instruction::UNUSED_E5:
1152 case Instruction::UNUSED_E6:
1153 case Instruction::UNUSED_E7:
1154 case Instruction::UNUSED_E8:
1155 case Instruction::UNUSED_E9:
1156 case Instruction::UNUSED_EA:
1157 case Instruction::UNUSED_EB:
1158 case Instruction::UNUSED_EC:
Logan Chien70f94b42011-12-27 17:49:11 +08001159 case Instruction::UNUSED_EE:
1160 case Instruction::UNUSED_EF:
1161 case Instruction::UNUSED_F0:
1162 case Instruction::UNUSED_F1:
1163 case Instruction::UNUSED_F2:
1164 case Instruction::UNUSED_F3:
1165 case Instruction::UNUSED_F4:
1166 case Instruction::UNUSED_F5:
1167 case Instruction::UNUSED_F6:
1168 case Instruction::UNUSED_F7:
1169 case Instruction::UNUSED_F8:
1170 case Instruction::UNUSED_F9:
1171 case Instruction::UNUSED_FA:
1172 case Instruction::UNUSED_FB:
1173 case Instruction::UNUSED_FC:
1174 case Instruction::UNUSED_FD:
1175 case Instruction::UNUSED_FE:
1176 case Instruction::UNUSED_FF:
1177 LOG(FATAL) << "Dex file contains UNUSED bytecode: " << insn->Opcode();
1178 break;
1179 }
1180
1181#undef ARGS
1182}
1183
1184
1185void MethodCompiler::EmitInsn_Nop(uint32_t dex_pc,
1186 Instruction const* insn) {
Logan Chiene09a6b72011-12-27 17:50:21 +08001187
1188 uint16_t insn_signature = code_item_->insns_[dex_pc];
1189
1190 if (insn_signature == Instruction::kPackedSwitchSignature ||
1191 insn_signature == Instruction::kSparseSwitchSignature ||
1192 insn_signature == Instruction::kArrayDataSignature) {
1193 irb_.CreateUnreachable();
Elliott Hughesb25c3f62012-03-26 16:35:06 -07001194 } else {
Logan Chiene09a6b72011-12-27 17:50:21 +08001195 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1196 }
Shih-wei Liaod1fec812012-02-13 09:51:10 -08001197}
1198
1199
Logan Chien70f94b42011-12-27 17:49:11 +08001200void MethodCompiler::EmitInsn_Move(uint32_t dex_pc,
1201 Instruction const* insn,
1202 JType jty) {
Logan Chien48173132011-12-27 17:51:13 +08001203
Elliott Hughesadb8c672012-03-06 16:49:32 -08001204 DecodedInstruction dec_insn(insn);
Logan Chien48173132011-12-27 17:51:13 +08001205
Elliott Hughesadb8c672012-03-06 16:49:32 -08001206 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, jty, kReg);
1207 EmitStoreDalvikReg(dec_insn.vA, jty, kReg, src_value);
Logan Chien48173132011-12-27 17:51:13 +08001208
Logan Chien70f94b42011-12-27 17:49:11 +08001209 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1210}
1211
1212
1213void MethodCompiler::EmitInsn_MoveResult(uint32_t dex_pc,
1214 Instruction const* insn,
1215 JType jty) {
Logan Chien48173132011-12-27 17:51:13 +08001216
Elliott Hughesadb8c672012-03-06 16:49:32 -08001217 DecodedInstruction dec_insn(insn);
Logan Chien48173132011-12-27 17:51:13 +08001218
1219 llvm::Value* src_value = EmitLoadDalvikRetValReg(jty, kReg);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001220 EmitStoreDalvikReg(dec_insn.vA, jty, kReg, src_value);
Logan Chien48173132011-12-27 17:51:13 +08001221
Logan Chien70f94b42011-12-27 17:49:11 +08001222 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1223}
1224
1225
1226void MethodCompiler::EmitInsn_MoveException(uint32_t dex_pc,
1227 Instruction const* insn) {
Logan Chien3354cec2012-01-13 14:29:03 +08001228
Elliott Hughesadb8c672012-03-06 16:49:32 -08001229 DecodedInstruction dec_insn(insn);
Logan Chien3354cec2012-01-13 14:29:03 +08001230
TDYa127ee1f59b2012-04-25 00:56:40 -07001231 // Get thread
Logan Chien3354cec2012-01-13 14:29:03 +08001232 llvm::Value* thread_object_addr =
1233 irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1234
TDYa127ee1f59b2012-04-25 00:56:40 -07001235 // Get thread-local exception field address
1236 llvm::Value* exception_object_addr =
1237 irb_.LoadFromObjectOffset(thread_object_addr,
1238 Thread::ExceptionOffset().Int32Value(),
1239 irb_.getJObjectTy());
Logan Chien3354cec2012-01-13 14:29:03 +08001240
1241 // Set thread-local exception field address to NULL
TDYa127ee1f59b2012-04-25 00:56:40 -07001242 irb_.StoreToObjectOffset(thread_object_addr,
1243 Thread::ExceptionOffset().Int32Value(),
1244 irb_.getJNull());
Logan Chien3354cec2012-01-13 14:29:03 +08001245
1246 // Keep the exception object in the Dalvik register
Elliott Hughesadb8c672012-03-06 16:49:32 -08001247 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, exception_object_addr);
Logan Chien3354cec2012-01-13 14:29:03 +08001248
Logan Chien70f94b42011-12-27 17:49:11 +08001249 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1250}
1251
1252
1253void MethodCompiler::EmitInsn_ThrowException(uint32_t dex_pc,
1254 Instruction const* insn) {
Logan Chien6c6f12d2012-01-13 19:26:27 +08001255
Elliott Hughesadb8c672012-03-06 16:49:32 -08001256 DecodedInstruction dec_insn(insn);
Logan Chien6c6f12d2012-01-13 19:26:27 +08001257
1258 llvm::Value* exception_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001259 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chien6c6f12d2012-01-13 19:26:27 +08001260
TDYa127c8dc1012012-04-19 07:03:33 -07001261 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001262
Logan Chien6c6f12d2012-01-13 19:26:27 +08001263 irb_.CreateCall(irb_.GetRuntime(ThrowException), exception_addr);
1264
1265 EmitBranchExceptionLandingPad(dex_pc);
Logan Chien70f94b42011-12-27 17:49:11 +08001266}
1267
1268
Logan Chien9e5f5c12012-04-10 13:51:45 +08001269void MethodCompiler::EmitInsn_ThrowVerificationError(uint32_t dex_pc,
1270 Instruction const* insn) {
1271
1272 DecodedInstruction dec_insn(insn);
1273
TDYa127c8dc1012012-04-19 07:03:33 -07001274 EmitUpdateDexPC(dex_pc);
Logan Chien9e5f5c12012-04-10 13:51:45 +08001275
1276 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1277 llvm::Value* kind_value = irb_.getInt32(dec_insn.vA);
1278 llvm::Value* ref_value = irb_.getInt32(dec_insn.vB);
1279
1280 irb_.CreateCall3(irb_.GetRuntime(ThrowVerificationError),
1281 method_object_addr, kind_value, ref_value);
1282
1283 EmitBranchExceptionLandingPad(dex_pc);
1284}
1285
1286
Logan Chien70f94b42011-12-27 17:49:11 +08001287void MethodCompiler::EmitInsn_ReturnVoid(uint32_t dex_pc,
1288 Instruction const* insn) {
Logan Chien8898a272011-12-27 17:51:56 +08001289 // Garbage collection safe-point
1290 EmitGuard_GarbageCollectionSuspend(dex_pc);
1291
Logan Chien8dfcbea2012-02-17 18:50:32 +08001292 // Pop the shadow frame
1293 EmitPopShadowFrame();
1294
Logan Chien8898a272011-12-27 17:51:56 +08001295 // Return!
1296 irb_.CreateRetVoid();
Logan Chien70f94b42011-12-27 17:49:11 +08001297}
1298
1299
1300void MethodCompiler::EmitInsn_Return(uint32_t dex_pc,
1301 Instruction const* insn) {
Logan Chien8898a272011-12-27 17:51:56 +08001302
Elliott Hughesadb8c672012-03-06 16:49:32 -08001303 DecodedInstruction dec_insn(insn);
Logan Chien8898a272011-12-27 17:51:56 +08001304
1305 // Garbage collection safe-point
1306 EmitGuard_GarbageCollectionSuspend(dex_pc);
1307
Logan Chien8dfcbea2012-02-17 18:50:32 +08001308 // Pop the shadow frame
1309 EmitPopShadowFrame();
1310 // NOTE: It is important to keep this AFTER the GC safe-point. Otherwise,
1311 // the return value might be collected since the shadow stack is popped.
1312
Logan Chien8898a272011-12-27 17:51:56 +08001313 // Return!
Logan Chiendd361c92012-04-10 23:40:37 +08001314 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
Elliott Hughesadb8c672012-03-06 16:49:32 -08001315 llvm::Value* retval = EmitLoadDalvikReg(dec_insn.vA, ret_shorty, kAccurate);
Logan Chien8898a272011-12-27 17:51:56 +08001316
1317 irb_.CreateRet(retval);
Logan Chien70f94b42011-12-27 17:49:11 +08001318}
1319
1320
1321void MethodCompiler::EmitInsn_LoadConstant(uint32_t dex_pc,
1322 Instruction const* insn,
1323 JType imm_jty) {
Shih-wei Liao798366e2012-02-16 09:25:33 -08001324
Elliott Hughesadb8c672012-03-06 16:49:32 -08001325 DecodedInstruction dec_insn(insn);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001326
1327 DCHECK(imm_jty == kInt || imm_jty == kLong) << imm_jty;
1328
1329 int64_t imm = 0;
1330
1331 switch (insn->Opcode()) {
1332 // 32-bit Immediate
1333 case Instruction::CONST_4:
1334 case Instruction::CONST_16:
1335 case Instruction::CONST:
1336 case Instruction::CONST_WIDE_16:
1337 case Instruction::CONST_WIDE_32:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001338 imm = static_cast<int64_t>(static_cast<int32_t>(dec_insn.vB));
Shih-wei Liao798366e2012-02-16 09:25:33 -08001339 break;
1340
1341 case Instruction::CONST_HIGH16:
1342 imm = static_cast<int64_t>(static_cast<int32_t>(
Elliott Hughesadb8c672012-03-06 16:49:32 -08001343 static_cast<uint32_t>(static_cast<uint16_t>(dec_insn.vB)) << 16));
Shih-wei Liao798366e2012-02-16 09:25:33 -08001344 break;
1345
1346 // 64-bit Immediate
1347 case Instruction::CONST_WIDE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001348 imm = static_cast<int64_t>(dec_insn.vB_wide);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001349 break;
1350
1351 case Instruction::CONST_WIDE_HIGH16:
1352 imm = static_cast<int64_t>(
Elliott Hughesadb8c672012-03-06 16:49:32 -08001353 static_cast<uint64_t>(static_cast<uint16_t>(dec_insn.vB)) << 48);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001354 break;
1355
1356 // Unknown opcode for load constant (unreachable)
1357 default:
1358 LOG(FATAL) << "Unknown opcode for load constant: " << insn->Opcode();
1359 break;
1360 }
1361
1362 // Store the non-object register
1363 llvm::Type* imm_type = irb_.getJType(imm_jty, kAccurate);
1364 llvm::Constant* imm_value = llvm::ConstantInt::getSigned(imm_type, imm);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001365 EmitStoreDalvikReg(dec_insn.vA, imm_jty, kAccurate, imm_value);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001366
1367 // Store the object register if it is possible to be null.
1368 if (imm_jty == kInt && imm == 0) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001369 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, irb_.getJNull());
Shih-wei Liao798366e2012-02-16 09:25:33 -08001370 }
1371
Logan Chien70f94b42011-12-27 17:49:11 +08001372 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1373}
1374
1375
1376void MethodCompiler::EmitInsn_LoadConstantString(uint32_t dex_pc,
1377 Instruction const* insn) {
Logan Chienc3b4ba12012-01-16 19:52:53 +08001378
Elliott Hughesadb8c672012-03-06 16:49:32 -08001379 DecodedInstruction dec_insn(insn);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001380
Elliott Hughesadb8c672012-03-06 16:49:32 -08001381 uint32_t string_idx = dec_insn.vB;
Logan Chienc3b4ba12012-01-16 19:52:53 +08001382
1383 llvm::Value* string_field_addr = EmitLoadDexCacheStringFieldAddr(string_idx);
1384
1385 llvm::Value* string_addr = irb_.CreateLoad(string_field_addr);
1386
1387 if (!compiler_->CanAssumeStringIsPresentInDexCache(dex_cache_, string_idx)) {
1388 llvm::BasicBlock* block_str_exist =
1389 CreateBasicBlockWithDexPC(dex_pc, "str_exist");
1390
1391 llvm::BasicBlock* block_str_resolve =
1392 CreateBasicBlockWithDexPC(dex_pc, "str_resolve");
1393
1394 // Test: Is the string resolved and in the dex cache?
1395 llvm::Value* equal_null = irb_.CreateICmpEQ(string_addr, irb_.getJNull());
1396
TDYa127a849cb62012-04-01 05:59:34 -07001397 irb_.CreateCondBr(equal_null, block_str_resolve, block_str_exist);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001398
1399 // String is resolved, go to next basic block.
1400 irb_.SetInsertPoint(block_str_exist);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001401 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, string_addr);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001402 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1403
1404 // String is not resolved yet, resolve it now.
1405 irb_.SetInsertPoint(block_str_resolve);
1406
1407 llvm::Function* runtime_func = irb_.GetRuntime(ResolveString);
1408
1409 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1410
1411 llvm::Value* string_idx_value = irb_.getInt32(string_idx);
1412
TDYa127c8dc1012012-04-19 07:03:33 -07001413 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001414
1415 string_addr = irb_.CreateCall2(runtime_func, method_object_addr,
1416 string_idx_value);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001417
1418 EmitGuard_ExceptionLandingPad(dex_pc);
1419 }
1420
1421 // Store the string object to the Dalvik register
Elliott Hughesadb8c672012-03-06 16:49:32 -08001422 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, string_addr);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001423
Logan Chien70f94b42011-12-27 17:49:11 +08001424 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1425}
1426
1427
Logan Chien27b30252012-01-14 03:43:35 +08001428llvm::Value* MethodCompiler::EmitLoadConstantClass(uint32_t dex_pc,
1429 uint32_t type_idx) {
1430 if (!compiler_->CanAccessTypeWithoutChecks(method_idx_, dex_cache_,
1431 *dex_file_, type_idx)) {
1432 llvm::Value* type_idx_value = irb_.getInt32(type_idx);
1433
1434 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1435
TDYa127706e9b62012-04-19 12:24:26 -07001436 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1437
Logan Chien27b30252012-01-14 03:43:35 +08001438 llvm::Function* runtime_func =
1439 irb_.GetRuntime(InitializeTypeAndVerifyAccess);
1440
TDYa127c8dc1012012-04-19 07:03:33 -07001441 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001442
Logan Chien27b30252012-01-14 03:43:35 +08001443 llvm::Value* type_object_addr =
TDYa127706e9b62012-04-19 12:24:26 -07001444 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
Logan Chien27b30252012-01-14 03:43:35 +08001445
1446 EmitGuard_ExceptionLandingPad(dex_pc);
1447
1448 return type_object_addr;
1449
1450 } else {
1451 // Try to load the class (type) object from the test cache.
1452 llvm::Value* type_field_addr =
1453 EmitLoadDexCacheResolvedTypeFieldAddr(type_idx);
1454
1455 llvm::Value* type_object_addr = irb_.CreateLoad(type_field_addr);
1456
1457 if (compiler_->CanAssumeTypeIsPresentInDexCache(dex_cache_, type_idx)) {
1458 return type_object_addr;
1459 }
1460
1461 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
1462
1463 // Test whether class (type) object is in the dex cache or not
1464 llvm::Value* equal_null =
1465 irb_.CreateICmpEQ(type_object_addr, irb_.getJNull());
1466
1467 llvm::BasicBlock* block_cont =
1468 CreateBasicBlockWithDexPC(dex_pc, "cont");
1469
1470 llvm::BasicBlock* block_load_class =
1471 CreateBasicBlockWithDexPC(dex_pc, "load_class");
1472
1473 irb_.CreateCondBr(equal_null, block_load_class, block_cont);
1474
1475 // Failback routine to load the class object
1476 irb_.SetInsertPoint(block_load_class);
1477
1478 llvm::Function* runtime_func = irb_.GetRuntime(InitializeType);
1479
1480 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
1481
1482 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1483
TDYa127706e9b62012-04-19 12:24:26 -07001484 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1485
TDYa127c8dc1012012-04-19 07:03:33 -07001486 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001487
Logan Chien27b30252012-01-14 03:43:35 +08001488 llvm::Value* loaded_type_object_addr =
TDYa127706e9b62012-04-19 12:24:26 -07001489 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
Logan Chien27b30252012-01-14 03:43:35 +08001490
1491 EmitGuard_ExceptionLandingPad(dex_pc);
1492
1493 llvm::BasicBlock* block_after_load_class = irb_.GetInsertBlock();
1494
1495 irb_.CreateBr(block_cont);
1496
1497 // Now the class object must be loaded
1498 irb_.SetInsertPoint(block_cont);
1499
1500 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1501
1502 phi->addIncoming(type_object_addr, block_original);
1503 phi->addIncoming(loaded_type_object_addr, block_after_load_class);
1504
1505 return phi;
1506 }
1507}
1508
1509
Logan Chien70f94b42011-12-27 17:49:11 +08001510void MethodCompiler::EmitInsn_LoadConstantClass(uint32_t dex_pc,
1511 Instruction const* insn) {
Logan Chien27b30252012-01-14 03:43:35 +08001512
Elliott Hughesadb8c672012-03-06 16:49:32 -08001513 DecodedInstruction dec_insn(insn);
Logan Chien27b30252012-01-14 03:43:35 +08001514
Elliott Hughesadb8c672012-03-06 16:49:32 -08001515 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vB);
1516 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, type_object_addr);
Logan Chien27b30252012-01-14 03:43:35 +08001517
Logan Chien70f94b42011-12-27 17:49:11 +08001518 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1519}
1520
1521
1522void MethodCompiler::EmitInsn_MonitorEnter(uint32_t dex_pc,
1523 Instruction const* insn) {
Logan Chien9e0dbe42012-01-13 12:11:37 +08001524
Elliott Hughesadb8c672012-03-06 16:49:32 -08001525 DecodedInstruction dec_insn(insn);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001526
1527 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001528 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001529
1530 // TODO: Slow path always. May not need NullPointerException check.
1531 EmitGuard_NullPointerException(dex_pc, object_addr);
1532
TDYa127c8dc1012012-04-19 07:03:33 -07001533 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001534
TDYa127706e9b62012-04-19 12:24:26 -07001535 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1536
1537 irb_.CreateCall2(irb_.GetRuntime(LockObject), object_addr, thread_object_addr);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001538 EmitGuard_ExceptionLandingPad(dex_pc);
1539
Logan Chien70f94b42011-12-27 17:49:11 +08001540 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1541}
1542
1543
1544void MethodCompiler::EmitInsn_MonitorExit(uint32_t dex_pc,
1545 Instruction const* insn) {
Logan Chien9e0dbe42012-01-13 12:11:37 +08001546
Elliott Hughesadb8c672012-03-06 16:49:32 -08001547 DecodedInstruction dec_insn(insn);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001548
1549 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001550 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001551
1552 EmitGuard_NullPointerException(dex_pc, object_addr);
1553
TDYa127c8dc1012012-04-19 07:03:33 -07001554 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001555
TDYa127706e9b62012-04-19 12:24:26 -07001556 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1557
1558 irb_.CreateCall2(irb_.GetRuntime(UnlockObject), object_addr, thread_object_addr);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001559 EmitGuard_ExceptionLandingPad(dex_pc);
1560
Logan Chien70f94b42011-12-27 17:49:11 +08001561 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1562}
1563
1564
1565void MethodCompiler::EmitInsn_CheckCast(uint32_t dex_pc,
1566 Instruction const* insn) {
Logan Chienfc880952012-01-15 23:53:10 +08001567
Elliott Hughesadb8c672012-03-06 16:49:32 -08001568 DecodedInstruction dec_insn(insn);
Logan Chienfc880952012-01-15 23:53:10 +08001569
1570 llvm::BasicBlock* block_test_class =
1571 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1572
1573 llvm::BasicBlock* block_test_sub_class =
1574 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
1575
1576 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001577 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chienfc880952012-01-15 23:53:10 +08001578
1579 // Test: Is the reference equal to null? Act as no-op when it is null.
1580 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
1581
1582 irb_.CreateCondBr(equal_null,
1583 GetNextBasicBlock(dex_pc),
1584 block_test_class);
1585
1586 // Test: Is the object instantiated from the given class?
1587 irb_.SetInsertPoint(block_test_class);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001588 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vB);
Logan Chienfc880952012-01-15 23:53:10 +08001589 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1590
1591 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
1592
1593 llvm::Value* object_type_field_addr =
1594 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
1595
1596 llvm::Value* object_type_object_addr =
1597 irb_.CreateLoad(object_type_field_addr);
1598
1599 llvm::Value* equal_class =
1600 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
1601
1602 irb_.CreateCondBr(equal_class,
1603 GetNextBasicBlock(dex_pc),
1604 block_test_sub_class);
1605
1606 // Test: Is the object instantiated from the subclass of the given class?
1607 irb_.SetInsertPoint(block_test_sub_class);
1608
TDYa127c8dc1012012-04-19 07:03:33 -07001609 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001610
Logan Chienfc880952012-01-15 23:53:10 +08001611 irb_.CreateCall2(irb_.GetRuntime(CheckCast),
1612 type_object_addr, object_type_object_addr);
1613
1614 EmitGuard_ExceptionLandingPad(dex_pc);
1615
Logan Chien70f94b42011-12-27 17:49:11 +08001616 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1617}
1618
1619
1620void MethodCompiler::EmitInsn_InstanceOf(uint32_t dex_pc,
1621 Instruction const* insn) {
Logan Chien68725e22012-01-15 22:25:34 +08001622
Elliott Hughesadb8c672012-03-06 16:49:32 -08001623 DecodedInstruction dec_insn(insn);
Logan Chien68725e22012-01-15 22:25:34 +08001624
1625 llvm::Constant* zero = irb_.getJInt(0);
1626 llvm::Constant* one = irb_.getJInt(1);
1627
1628 llvm::BasicBlock* block_nullp = CreateBasicBlockWithDexPC(dex_pc, "nullp");
1629
1630 llvm::BasicBlock* block_test_class =
1631 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1632
1633 llvm::BasicBlock* block_class_equals =
1634 CreateBasicBlockWithDexPC(dex_pc, "class_eq");
1635
1636 llvm::BasicBlock* block_test_sub_class =
1637 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
1638
1639 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001640 EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
Logan Chien68725e22012-01-15 22:25:34 +08001641
1642 // Overview of the following code :
1643 // We check for null, if so, then false, otherwise check for class == . If so
1644 // then true, otherwise do callout slowpath.
1645 //
1646 // Test: Is the reference equal to null? Set 0 when it is null.
1647 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
1648
1649 irb_.CreateCondBr(equal_null, block_nullp, block_test_class);
1650
1651 irb_.SetInsertPoint(block_nullp);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001652 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, zero);
Logan Chien68725e22012-01-15 22:25:34 +08001653 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1654
1655 // Test: Is the object instantiated from the given class?
1656 irb_.SetInsertPoint(block_test_class);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001657 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vC);
Logan Chien68725e22012-01-15 22:25:34 +08001658 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1659
1660 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
1661
1662 llvm::Value* object_type_field_addr =
1663 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
1664
1665 llvm::Value* object_type_object_addr =
1666 irb_.CreateLoad(object_type_field_addr);
1667
1668 llvm::Value* equal_class =
1669 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
1670
1671 irb_.CreateCondBr(equal_class, block_class_equals, block_test_sub_class);
1672
1673 irb_.SetInsertPoint(block_class_equals);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001674 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, one);
Logan Chien68725e22012-01-15 22:25:34 +08001675 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1676
1677 // Test: Is the object instantiated from the subclass of the given class?
1678 irb_.SetInsertPoint(block_test_sub_class);
1679
1680 llvm::Value* result =
1681 irb_.CreateCall2(irb_.GetRuntime(IsAssignable),
1682 type_object_addr, object_type_object_addr);
1683
Elliott Hughesadb8c672012-03-06 16:49:32 -08001684 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result);
Logan Chien68725e22012-01-15 22:25:34 +08001685
Logan Chien70f94b42011-12-27 17:49:11 +08001686 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1687}
1688
1689
Logan Chien61bb6142012-02-03 15:34:53 +08001690llvm::Value* MethodCompiler::EmitLoadArrayLength(llvm::Value* array) {
Logan Chien61bb6142012-02-03 15:34:53 +08001691 // Load array length
TDYa127ee1f59b2012-04-25 00:56:40 -07001692 return irb_.LoadFromObjectOffset(array,
1693 Array::LengthOffset().Int32Value(),
1694 irb_.getJIntTy());
Logan Chien61bb6142012-02-03 15:34:53 +08001695}
1696
1697
Logan Chien70f94b42011-12-27 17:49:11 +08001698void MethodCompiler::EmitInsn_ArrayLength(uint32_t dex_pc,
1699 Instruction const* insn) {
Logan Chien61bb6142012-02-03 15:34:53 +08001700
Elliott Hughesadb8c672012-03-06 16:49:32 -08001701 DecodedInstruction dec_insn(insn);
Logan Chien61bb6142012-02-03 15:34:53 +08001702
1703 // Get the array object address
Elliott Hughesadb8c672012-03-06 16:49:32 -08001704 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
Logan Chien61bb6142012-02-03 15:34:53 +08001705 EmitGuard_NullPointerException(dex_pc, array_addr);
1706
1707 // Get the array length and store it to the register
1708 llvm::Value* array_len = EmitLoadArrayLength(array_addr);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001709 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, array_len);
Logan Chien61bb6142012-02-03 15:34:53 +08001710
Logan Chien70f94b42011-12-27 17:49:11 +08001711 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1712}
1713
1714
1715void MethodCompiler::EmitInsn_NewInstance(uint32_t dex_pc,
1716 Instruction const* insn) {
Logan Chien032bdad2012-01-16 09:59:23 +08001717
Elliott Hughesadb8c672012-03-06 16:49:32 -08001718 DecodedInstruction dec_insn(insn);
Logan Chien032bdad2012-01-16 09:59:23 +08001719
1720 llvm::Function* runtime_func;
Logan Chien1a032b12012-04-11 11:43:04 +08001721 if (compiler_->CanAccessInstantiableTypeWithoutChecks(
1722 method_idx_, dex_cache_, *dex_file_, dec_insn.vB)) {
Logan Chien032bdad2012-01-16 09:59:23 +08001723 runtime_func = irb_.GetRuntime(AllocObject);
1724 } else {
1725 runtime_func = irb_.GetRuntime(AllocObjectWithAccessCheck);
1726 }
1727
Elliott Hughesadb8c672012-03-06 16:49:32 -08001728 llvm::Constant* type_index_value = irb_.getInt32(dec_insn.vB);
Logan Chien032bdad2012-01-16 09:59:23 +08001729
1730 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1731
TDYa127da83d972012-04-18 00:21:49 -07001732 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1733
TDYa127c8dc1012012-04-19 07:03:33 -07001734 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001735
Logan Chien032bdad2012-01-16 09:59:23 +08001736 llvm::Value* object_addr =
TDYa127da83d972012-04-18 00:21:49 -07001737 irb_.CreateCall3(runtime_func, type_index_value, method_object_addr, thread_object_addr);
Logan Chien032bdad2012-01-16 09:59:23 +08001738
1739 EmitGuard_ExceptionLandingPad(dex_pc);
1740
Elliott Hughesadb8c672012-03-06 16:49:32 -08001741 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, object_addr);
Logan Chien032bdad2012-01-16 09:59:23 +08001742
Logan Chien70f94b42011-12-27 17:49:11 +08001743 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1744}
1745
1746
Logan Chiena2cc6a32012-01-16 10:38:41 +08001747llvm::Value* MethodCompiler::EmitAllocNewArray(uint32_t dex_pc,
1748 int32_t length,
1749 uint32_t type_idx,
1750 bool is_filled_new_array) {
1751 llvm::Function* runtime_func;
1752
1753 bool skip_access_check =
1754 compiler_->CanAccessTypeWithoutChecks(method_idx_, dex_cache_,
1755 *dex_file_, type_idx);
1756
TDYa127a849cb62012-04-01 05:59:34 -07001757 llvm::Value* array_length_value;
1758
Logan Chiena2cc6a32012-01-16 10:38:41 +08001759 if (is_filled_new_array) {
1760 runtime_func = skip_access_check ?
1761 irb_.GetRuntime(CheckAndAllocArray) :
1762 irb_.GetRuntime(CheckAndAllocArrayWithAccessCheck);
TDYa127a849cb62012-04-01 05:59:34 -07001763 array_length_value = irb_.getInt32(length);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001764 } else {
1765 runtime_func = skip_access_check ?
1766 irb_.GetRuntime(AllocArray) :
1767 irb_.GetRuntime(AllocArrayWithAccessCheck);
TDYa127a849cb62012-04-01 05:59:34 -07001768 array_length_value = EmitLoadDalvikReg(length, kInt, kAccurate);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001769 }
1770
1771 llvm::Constant* type_index_value = irb_.getInt32(type_idx);
1772
1773 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1774
TDYa127da83d972012-04-18 00:21:49 -07001775 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1776
TDYa127c8dc1012012-04-19 07:03:33 -07001777 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001778
Logan Chiena2cc6a32012-01-16 10:38:41 +08001779 llvm::Value* object_addr =
TDYa127da83d972012-04-18 00:21:49 -07001780 irb_.CreateCall4(runtime_func, type_index_value, method_object_addr,
1781 array_length_value, thread_object_addr);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001782
1783 EmitGuard_ExceptionLandingPad(dex_pc);
1784
1785 return object_addr;
1786}
1787
1788
Logan Chien70f94b42011-12-27 17:49:11 +08001789void MethodCompiler::EmitInsn_NewArray(uint32_t dex_pc,
1790 Instruction const* insn) {
Logan Chiena2cc6a32012-01-16 10:38:41 +08001791
Elliott Hughesadb8c672012-03-06 16:49:32 -08001792 DecodedInstruction dec_insn(insn);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001793
1794 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001795 EmitAllocNewArray(dex_pc, dec_insn.vB, dec_insn.vC, false);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001796
Elliott Hughesadb8c672012-03-06 16:49:32 -08001797 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, object_addr);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001798
Logan Chien70f94b42011-12-27 17:49:11 +08001799 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1800}
1801
1802
1803void MethodCompiler::EmitInsn_FilledNewArray(uint32_t dex_pc,
1804 Instruction const* insn,
1805 bool is_range) {
Logan Chiena85fb2f2012-01-16 12:52:56 +08001806
Elliott Hughesadb8c672012-03-06 16:49:32 -08001807 DecodedInstruction dec_insn(insn);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001808
1809 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001810 EmitAllocNewArray(dex_pc, dec_insn.vA, dec_insn.vB, true);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001811
Elliott Hughesadb8c672012-03-06 16:49:32 -08001812 if (dec_insn.vA > 0) {
Logan Chiendd361c92012-04-10 23:40:37 +08001813 // Resolve the element type
TDYa127183cf262012-04-11 07:53:21 -07001814 Class* klass = dex_cache_->GetResolvedType(dec_insn.vB)->GetComponentType();
Logan Chiendd361c92012-04-10 23:40:37 +08001815 // TODO: Avoid the usage of the dex_cache_. Try to figure out a better
1816 // way to distinguish [I and [L.
Logan Chiena85fb2f2012-01-16 12:52:56 +08001817 CHECK_NE(klass, static_cast<Class*>(NULL));
Logan Chiena85fb2f2012-01-16 12:52:56 +08001818
Logan Chiendd361c92012-04-10 23:40:37 +08001819 uint32_t alignment;
1820 llvm::Constant* elem_size;
1821 llvm::PointerType* field_type;
Logan Chiena85fb2f2012-01-16 12:52:56 +08001822
Logan Chiendd361c92012-04-10 23:40:37 +08001823 // NOTE: Currently filled-new-array only supports 'L', '[', and 'I'
1824 // as the element, thus we are only checking 2 cases: primitive int and
1825 // non-primitive type.
Logan Chiena85fb2f2012-01-16 12:52:56 +08001826 if (klass->IsPrimitiveInt()) {
Logan Chiendd361c92012-04-10 23:40:37 +08001827 alignment = sizeof(int32_t);
1828 elem_size = irb_.getPtrEquivInt(sizeof(int32_t));
Logan Chiena85fb2f2012-01-16 12:52:56 +08001829 field_type = irb_.getJIntTy()->getPointerTo();
1830 } else {
1831 CHECK(!klass->IsPrimitive());
Logan Chiendd361c92012-04-10 23:40:37 +08001832 alignment = irb_.getSizeOfPtrEquivInt();
1833 elem_size = irb_.getSizeOfPtrEquivIntValue();
Logan Chiena85fb2f2012-01-16 12:52:56 +08001834 field_type = irb_.getJObjectTy()->getPointerTo();
1835 }
1836
Logan Chiendd361c92012-04-10 23:40:37 +08001837 llvm::Value* data_field_offset =
1838 irb_.getPtrEquivInt(Array::DataOffset(alignment).Int32Value());
1839
1840 llvm::Value* data_field_addr =
1841 irb_.CreatePtrDisp(object_addr, data_field_offset, field_type);
1842
Logan Chiena85fb2f2012-01-16 12:52:56 +08001843 // TODO: Tune this code. Currently we are generating one instruction for
1844 // one element which may be very space consuming. Maybe changing to use
1845 // memcpy may help; however, since we can't guarantee that the alloca of
1846 // dalvik register are continuous, we can't perform such optimization yet.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001847 for (uint32_t i = 0; i < dec_insn.vA; ++i) {
Logan Chiena85fb2f2012-01-16 12:52:56 +08001848 int reg_index;
1849 if (is_range) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001850 reg_index = dec_insn.vC + i;
Logan Chiena85fb2f2012-01-16 12:52:56 +08001851 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001852 reg_index = dec_insn.arg[i];
Logan Chiena85fb2f2012-01-16 12:52:56 +08001853 }
1854
1855 llvm::Value* reg_value;
1856 if (klass->IsPrimitiveInt()) {
1857 reg_value = EmitLoadDalvikReg(reg_index, kInt, kAccurate);
1858 } else {
1859 reg_value = EmitLoadDalvikReg(reg_index, kObject, kAccurate);
1860 }
1861
1862 irb_.CreateStore(reg_value, data_field_addr);
1863
Logan Chiendd361c92012-04-10 23:40:37 +08001864 data_field_addr =
1865 irb_.CreatePtrDisp(data_field_addr, elem_size, field_type);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001866 }
1867 }
1868
1869 EmitStoreDalvikRetValReg(kObject, kAccurate, object_addr);
1870
Logan Chien70f94b42011-12-27 17:49:11 +08001871 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1872}
1873
1874
1875void MethodCompiler::EmitInsn_FillArrayData(uint32_t dex_pc,
1876 Instruction const* insn) {
Logan Chiene58b6582012-01-16 17:13:13 +08001877
Elliott Hughesadb8c672012-03-06 16:49:32 -08001878 DecodedInstruction dec_insn(insn);
Logan Chiene58b6582012-01-16 17:13:13 +08001879
1880 // Read the payload
Logan Chiene58b6582012-01-16 17:13:13 +08001881 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
Elliott Hughesadb8c672012-03-06 16:49:32 -08001882 static_cast<int32_t>(dec_insn.vB);
Logan Chiene58b6582012-01-16 17:13:13 +08001883
Logan Chien19c350a2012-05-01 19:21:32 +08001884 const Instruction::ArrayDataPayload* payload =
1885 reinterpret_cast<const Instruction::ArrayDataPayload*>(
1886 code_item_->insns_ + payload_offset);
Logan Chiene58b6582012-01-16 17:13:13 +08001887
Logan Chien86f50672012-04-24 13:08:45 +08001888 // Load array object
Elliott Hughesadb8c672012-03-06 16:49:32 -08001889 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiene58b6582012-01-16 17:13:13 +08001890
Logan Chien86f50672012-04-24 13:08:45 +08001891 if (payload->element_count == 0) {
1892 // When the number of the elements in the payload is zero, we don't have
1893 // to copy any numbers. However, we should check whether the array object
1894 // address is equal to null or not.
1895 EmitGuard_NullPointerException(dex_pc, array_addr);
1896 } else {
1897 // To save the code size, we are going to call the runtime function to
1898 // copy the content from DexFile.
Logan Chiene58b6582012-01-16 17:13:13 +08001899
Logan Chien86f50672012-04-24 13:08:45 +08001900 // NOTE: We will check for the NullPointerException in the runtime.
Logan Chiene58b6582012-01-16 17:13:13 +08001901
Logan Chien86f50672012-04-24 13:08:45 +08001902 llvm::Function* runtime_func = irb_.GetRuntime(FillArrayData);
Logan Chiene58b6582012-01-16 17:13:13 +08001903
Logan Chien86f50672012-04-24 13:08:45 +08001904 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
Logan Chiene58b6582012-01-16 17:13:13 +08001905
Logan Chien86f50672012-04-24 13:08:45 +08001906 EmitUpdateDexPC(dex_pc);
Logan Chiene58b6582012-01-16 17:13:13 +08001907
Logan Chien86f50672012-04-24 13:08:45 +08001908 irb_.CreateCall4(runtime_func,
1909 method_object_addr, irb_.getInt32(dex_pc),
1910 array_addr, irb_.getInt32(payload_offset));
Logan Chiene58b6582012-01-16 17:13:13 +08001911
Logan Chien86f50672012-04-24 13:08:45 +08001912 EmitGuard_ExceptionLandingPad(dex_pc);
Logan Chiene58b6582012-01-16 17:13:13 +08001913 }
1914
Logan Chien70f94b42011-12-27 17:49:11 +08001915 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1916}
1917
1918
1919void MethodCompiler::EmitInsn_UnconditionalBranch(uint32_t dex_pc,
1920 Instruction const* insn) {
Logan Chiena466c162011-12-27 17:55:46 +08001921
Elliott Hughesadb8c672012-03-06 16:49:32 -08001922 DecodedInstruction dec_insn(insn);
Logan Chiena466c162011-12-27 17:55:46 +08001923
Elliott Hughesadb8c672012-03-06 16:49:32 -08001924 int32_t branch_offset = dec_insn.vA;
Logan Chiena466c162011-12-27 17:55:46 +08001925
1926 if (branch_offset <= 0) {
1927 // Garbage collection safe-point on backward branch
1928 EmitGuard_GarbageCollectionSuspend(dex_pc);
1929 }
1930
1931 irb_.CreateBr(GetBasicBlock(dex_pc + branch_offset));
Logan Chien70f94b42011-12-27 17:49:11 +08001932}
1933
1934
1935void MethodCompiler::EmitInsn_PackedSwitch(uint32_t dex_pc,
1936 Instruction const* insn) {
Logan Chien7a89b6d2011-12-27 17:56:56 +08001937
Elliott Hughesadb8c672012-03-06 16:49:32 -08001938 DecodedInstruction dec_insn(insn);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001939
Logan Chien7a89b6d2011-12-27 17:56:56 +08001940 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
Elliott Hughesadb8c672012-03-06 16:49:32 -08001941 static_cast<int32_t>(dec_insn.vB);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001942
Logan Chien19c350a2012-05-01 19:21:32 +08001943 const Instruction::PackedSwitchPayload* payload =
1944 reinterpret_cast<const Instruction::PackedSwitchPayload*>(
1945 code_item_->insns_ + payload_offset);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001946
Elliott Hughesadb8c672012-03-06 16:49:32 -08001947 llvm::Value* value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001948
1949 llvm::SwitchInst* sw =
Logan Chien19c350a2012-05-01 19:21:32 +08001950 irb_.CreateSwitch(value, GetNextBasicBlock(dex_pc), payload->case_count);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001951
Logan Chien19c350a2012-05-01 19:21:32 +08001952 for (uint16_t i = 0; i < payload->case_count; ++i) {
1953 sw->addCase(irb_.getInt32(payload->first_key + i),
1954 GetBasicBlock(dex_pc + payload->targets[i]));
Logan Chien7a89b6d2011-12-27 17:56:56 +08001955 }
Logan Chien70f94b42011-12-27 17:49:11 +08001956}
1957
1958
1959void MethodCompiler::EmitInsn_SparseSwitch(uint32_t dex_pc,
1960 Instruction const* insn) {
Logan Chien7a89b6d2011-12-27 17:56:56 +08001961
Elliott Hughesadb8c672012-03-06 16:49:32 -08001962 DecodedInstruction dec_insn(insn);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001963
Logan Chien7a89b6d2011-12-27 17:56:56 +08001964 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
Elliott Hughesadb8c672012-03-06 16:49:32 -08001965 static_cast<int32_t>(dec_insn.vB);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001966
Logan Chien19c350a2012-05-01 19:21:32 +08001967 const Instruction::SparseSwitchPayload* payload =
1968 reinterpret_cast<const Instruction::SparseSwitchPayload*>(
1969 code_item_->insns_ + payload_offset);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001970
Logan Chien19c350a2012-05-01 19:21:32 +08001971 const int32_t* keys = payload->GetKeys();
1972 const int32_t* targets = payload->GetTargets();
Logan Chien7a89b6d2011-12-27 17:56:56 +08001973
Elliott Hughesadb8c672012-03-06 16:49:32 -08001974 llvm::Value* value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001975
1976 llvm::SwitchInst* sw =
Logan Chien19c350a2012-05-01 19:21:32 +08001977 irb_.CreateSwitch(value, GetNextBasicBlock(dex_pc), payload->case_count);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001978
Logan Chien19c350a2012-05-01 19:21:32 +08001979 for (size_t i = 0; i < payload->case_count; ++i) {
Logan Chien7a89b6d2011-12-27 17:56:56 +08001980 sw->addCase(irb_.getInt32(keys[i]), GetBasicBlock(dex_pc + targets[i]));
1981 }
Logan Chien70f94b42011-12-27 17:49:11 +08001982}
1983
1984
1985void MethodCompiler::EmitInsn_FPCompare(uint32_t dex_pc,
1986 Instruction const* insn,
1987 JType fp_jty,
1988 bool gt_bias) {
Logan Chien2c37e8e2011-12-27 17:58:46 +08001989
Elliott Hughesadb8c672012-03-06 16:49:32 -08001990 DecodedInstruction dec_insn(insn);
Logan Chien2c37e8e2011-12-27 17:58:46 +08001991
1992 DCHECK(fp_jty == kFloat || fp_jty == kDouble) << "JType: " << fp_jty;
1993
Elliott Hughesadb8c672012-03-06 16:49:32 -08001994 llvm::Value* src1_value = EmitLoadDalvikReg(dec_insn.vB, fp_jty, kAccurate);
1995 llvm::Value* src2_value = EmitLoadDalvikReg(dec_insn.vC, fp_jty, kAccurate);
Logan Chien2c37e8e2011-12-27 17:58:46 +08001996
1997 llvm::Value* cmp_eq = irb_.CreateFCmpOEQ(src1_value, src2_value);
1998 llvm::Value* cmp_lt;
1999
2000 if (gt_bias) {
2001 cmp_lt = irb_.CreateFCmpOLT(src1_value, src2_value);
2002 } else {
2003 cmp_lt = irb_.CreateFCmpULT(src1_value, src2_value);
2004 }
2005
2006 llvm::Value* result = EmitCompareResultSelection(cmp_eq, cmp_lt);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002007 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002008
Logan Chien70f94b42011-12-27 17:49:11 +08002009 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2010}
2011
2012
2013void MethodCompiler::EmitInsn_LongCompare(uint32_t dex_pc,
2014 Instruction const* insn) {
Logan Chien2c37e8e2011-12-27 17:58:46 +08002015
Elliott Hughesadb8c672012-03-06 16:49:32 -08002016 DecodedInstruction dec_insn(insn);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002017
Elliott Hughesadb8c672012-03-06 16:49:32 -08002018 llvm::Value* src1_value = EmitLoadDalvikReg(dec_insn.vB, kLong, kAccurate);
2019 llvm::Value* src2_value = EmitLoadDalvikReg(dec_insn.vC, kLong, kAccurate);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002020
2021 llvm::Value* cmp_eq = irb_.CreateICmpEQ(src1_value, src2_value);
2022 llvm::Value* cmp_lt = irb_.CreateICmpSLT(src1_value, src2_value);
2023
2024 llvm::Value* result = EmitCompareResultSelection(cmp_eq, cmp_lt);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002025 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002026
Logan Chien70f94b42011-12-27 17:49:11 +08002027 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2028}
2029
2030
Logan Chien2c37e8e2011-12-27 17:58:46 +08002031llvm::Value* MethodCompiler::EmitCompareResultSelection(llvm::Value* cmp_eq,
2032 llvm::Value* cmp_lt) {
2033
2034 llvm::Constant* zero = irb_.getJInt(0);
2035 llvm::Constant* pos1 = irb_.getJInt(1);
2036 llvm::Constant* neg1 = irb_.getJInt(-1);
2037
2038 llvm::Value* result_lt = irb_.CreateSelect(cmp_lt, neg1, pos1);
2039 llvm::Value* result_eq = irb_.CreateSelect(cmp_eq, zero, result_lt);
2040
2041 return result_eq;
2042}
2043
2044
Logan Chien70f94b42011-12-27 17:49:11 +08002045void MethodCompiler::EmitInsn_BinaryConditionalBranch(uint32_t dex_pc,
2046 Instruction const* insn,
2047 CondBranchKind cond) {
Logan Chiena78e3c82011-12-27 17:59:35 +08002048
Elliott Hughesadb8c672012-03-06 16:49:32 -08002049 DecodedInstruction dec_insn(insn);
Logan Chiena78e3c82011-12-27 17:59:35 +08002050
Elliott Hughesadb8c672012-03-06 16:49:32 -08002051 int8_t src1_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vA);
2052 int8_t src2_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vB);
Logan Chiena78e3c82011-12-27 17:59:35 +08002053
2054 DCHECK_NE(kRegUnknown, src1_reg_cat);
2055 DCHECK_NE(kRegUnknown, src2_reg_cat);
2056 DCHECK_NE(kRegCat2, src1_reg_cat);
2057 DCHECK_NE(kRegCat2, src2_reg_cat);
2058
Elliott Hughesadb8c672012-03-06 16:49:32 -08002059 int32_t branch_offset = dec_insn.vC;
Logan Chiena78e3c82011-12-27 17:59:35 +08002060
2061 if (branch_offset <= 0) {
2062 // Garbage collection safe-point on backward branch
2063 EmitGuard_GarbageCollectionSuspend(dex_pc);
2064 }
2065
Logan Chiena78e3c82011-12-27 17:59:35 +08002066 llvm::Value* src1_value;
2067 llvm::Value* src2_value;
2068
TDYa1278e9b4492012-04-24 15:50:27 -07002069 if (src1_reg_cat == kRegZero && src2_reg_cat == kRegZero) {
2070 src1_value = irb_.getInt32(0);
2071 src2_value = irb_.getInt32(0);
2072 } else if (src1_reg_cat != kRegZero && src2_reg_cat != kRegZero) {
Logan Chiena78e3c82011-12-27 17:59:35 +08002073 CHECK_EQ(src1_reg_cat, src2_reg_cat);
2074
2075 if (src1_reg_cat == kRegCat1nr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002076 src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
2077 src2_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002078 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002079 src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
2080 src2_value = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002081 }
2082 } else {
2083 DCHECK(src1_reg_cat == kRegZero ||
2084 src2_reg_cat == kRegZero);
2085
2086 if (src1_reg_cat == kRegZero) {
2087 if (src2_reg_cat == kRegCat1nr) {
2088 src1_value = irb_.getJInt(0);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002089 src2_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002090 } else {
2091 src1_value = irb_.getJNull();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002092 src2_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002093 }
2094 } else { // src2_reg_cat == kRegZero
2095 if (src2_reg_cat == kRegCat1nr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002096 src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002097 src2_value = irb_.getJInt(0);
2098 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002099 src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002100 src2_value = irb_.getJNull();
2101 }
2102 }
2103 }
2104
2105 llvm::Value* cond_value =
2106 EmitConditionResult(src1_value, src2_value, cond);
2107
2108 irb_.CreateCondBr(cond_value,
2109 GetBasicBlock(dex_pc + branch_offset),
2110 GetNextBasicBlock(dex_pc));
Logan Chien70f94b42011-12-27 17:49:11 +08002111}
2112
2113
2114void MethodCompiler::EmitInsn_UnaryConditionalBranch(uint32_t dex_pc,
2115 Instruction const* insn,
2116 CondBranchKind cond) {
Logan Chiena78e3c82011-12-27 17:59:35 +08002117
Elliott Hughesadb8c672012-03-06 16:49:32 -08002118 DecodedInstruction dec_insn(insn);
Logan Chiena78e3c82011-12-27 17:59:35 +08002119
Elliott Hughesadb8c672012-03-06 16:49:32 -08002120 int8_t src_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vA);
Logan Chiena78e3c82011-12-27 17:59:35 +08002121
2122 DCHECK_NE(kRegUnknown, src_reg_cat);
2123 DCHECK_NE(kRegCat2, src_reg_cat);
2124
Elliott Hughesadb8c672012-03-06 16:49:32 -08002125 int32_t branch_offset = dec_insn.vB;
Logan Chiena78e3c82011-12-27 17:59:35 +08002126
2127 if (branch_offset <= 0) {
2128 // Garbage collection safe-point on backward branch
2129 EmitGuard_GarbageCollectionSuspend(dex_pc);
2130 }
2131
Logan Chiena78e3c82011-12-27 17:59:35 +08002132 llvm::Value* src1_value;
2133 llvm::Value* src2_value;
2134
TDYa1278e9b4492012-04-24 15:50:27 -07002135 if (src_reg_cat == kRegZero) {
2136 src1_value = irb_.getInt32(0);
2137 src2_value = irb_.getInt32(0);
2138 } else if (src_reg_cat == kRegCat1nr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002139 src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002140 src2_value = irb_.getInt32(0);
2141 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002142 src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002143 src2_value = irb_.getJNull();
2144 }
2145
2146 llvm::Value* cond_value =
2147 EmitConditionResult(src1_value, src2_value, cond);
2148
2149 irb_.CreateCondBr(cond_value,
2150 GetBasicBlock(dex_pc + branch_offset),
2151 GetNextBasicBlock(dex_pc));
2152}
2153
2154
2155RegCategory MethodCompiler::GetInferredRegCategory(uint32_t dex_pc,
2156 uint16_t reg_idx) {
Logan Chiendd361c92012-04-10 23:40:37 +08002157
2158 Compiler::MethodReference mref(dex_file_, method_idx_);
2159
2160 InferredRegCategoryMap const* map =
Ian Rogers776ac1f2012-04-13 23:36:36 -07002161 verifier::MethodVerifier::GetInferredRegCategoryMap(mref);
Logan Chiendd361c92012-04-10 23:40:37 +08002162
Logan Chiena78e3c82011-12-27 17:59:35 +08002163 CHECK_NE(map, static_cast<InferredRegCategoryMap*>(NULL));
2164
2165 return map->GetRegCategory(dex_pc, reg_idx);
2166}
2167
2168
2169llvm::Value* MethodCompiler::EmitConditionResult(llvm::Value* lhs,
2170 llvm::Value* rhs,
2171 CondBranchKind cond) {
2172 switch (cond) {
2173 case kCondBranch_EQ:
2174 return irb_.CreateICmpEQ(lhs, rhs);
2175
2176 case kCondBranch_NE:
2177 return irb_.CreateICmpNE(lhs, rhs);
2178
2179 case kCondBranch_LT:
2180 return irb_.CreateICmpSLT(lhs, rhs);
2181
2182 case kCondBranch_GE:
2183 return irb_.CreateICmpSGE(lhs, rhs);
2184
2185 case kCondBranch_GT:
2186 return irb_.CreateICmpSGT(lhs, rhs);
2187
2188 case kCondBranch_LE:
2189 return irb_.CreateICmpSLE(lhs, rhs);
2190
2191 default: // Unreachable
2192 LOG(FATAL) << "Unknown conditional branch kind: " << cond;
2193 return NULL;
2194 }
Logan Chien70f94b42011-12-27 17:49:11 +08002195}
2196
TDYa12783bb6622012-04-17 02:20:34 -07002197void MethodCompiler::EmitMarkGCCard(llvm::Value* value, llvm::Value* target_addr) {
2198 // Using runtime support, let the target can override by InlineAssembly.
2199 llvm::Function* runtime_func = irb_.GetRuntime(MarkGCCard);
2200
2201 irb_.CreateCall2(runtime_func, value, target_addr);
2202}
Logan Chien70f94b42011-12-27 17:49:11 +08002203
Logan Chiene27fdbb2012-01-02 23:27:26 +08002204void
2205MethodCompiler::EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc,
2206 llvm::Value* array,
2207 llvm::Value* index) {
2208 llvm::Value* array_len = EmitLoadArrayLength(array);
2209
2210 llvm::Value* cmp = irb_.CreateICmpUGE(index, array_len);
2211
2212 llvm::BasicBlock* block_exception =
2213 CreateBasicBlockWithDexPC(dex_pc, "overflow");
2214
2215 llvm::BasicBlock* block_continue =
2216 CreateBasicBlockWithDexPC(dex_pc, "cont");
2217
2218 irb_.CreateCondBr(cmp, block_exception, block_continue);
2219
2220 irb_.SetInsertPoint(block_exception);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002221
TDYa127c8dc1012012-04-19 07:03:33 -07002222 EmitUpdateDexPC(dex_pc);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002223 irb_.CreateCall2(irb_.GetRuntime(ThrowIndexOutOfBounds), index, array_len);
2224 EmitBranchExceptionLandingPad(dex_pc);
2225
2226 irb_.SetInsertPoint(block_continue);
2227}
2228
2229
2230void MethodCompiler::EmitGuard_ArrayException(uint32_t dex_pc,
2231 llvm::Value* array,
2232 llvm::Value* index) {
2233 EmitGuard_NullPointerException(dex_pc, array);
2234 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array, index);
2235}
2236
2237
2238// Emit Array GetElementPtr
2239llvm::Value* MethodCompiler::EmitArrayGEP(llvm::Value* array_addr,
2240 llvm::Value* index_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08002241 llvm::Type* elem_type,
2242 JType elem_jty) {
2243
2244 int data_offset;
2245 if (elem_jty == kLong || elem_jty == kDouble ||
2246 (elem_jty == kObject && sizeof(uint64_t) == sizeof(Object*))) {
2247 data_offset = Array::DataOffset(sizeof(int64_t)).Int32Value();
2248 } else {
2249 data_offset = Array::DataOffset(sizeof(int32_t)).Int32Value();
2250 }
Logan Chiene27fdbb2012-01-02 23:27:26 +08002251
2252 llvm::Constant* data_offset_value =
Ian Rogers04ec04e2012-02-28 16:15:33 -08002253 irb_.getPtrEquivInt(data_offset);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002254
2255 llvm::Value* array_data_addr =
2256 irb_.CreatePtrDisp(array_addr, data_offset_value,
2257 elem_type->getPointerTo());
2258
2259 return irb_.CreateGEP(array_data_addr, index_value);
2260}
2261
2262
Logan Chien70f94b42011-12-27 17:49:11 +08002263void MethodCompiler::EmitInsn_AGet(uint32_t dex_pc,
2264 Instruction const* insn,
2265 JType elem_jty) {
Logan Chiene27fdbb2012-01-02 23:27:26 +08002266
Elliott Hughesadb8c672012-03-06 16:49:32 -08002267 DecodedInstruction dec_insn(insn);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002268
Elliott Hughesadb8c672012-03-06 16:49:32 -08002269 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
2270 llvm::Value* index_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002271
2272 EmitGuard_ArrayException(dex_pc, array_addr, index_value);
2273
2274 llvm::Type* elem_type = irb_.getJType(elem_jty, kArray);
2275
2276 llvm::Value* array_elem_addr =
Ian Rogers04ec04e2012-02-28 16:15:33 -08002277 EmitArrayGEP(array_addr, index_value, elem_type, elem_jty);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002278
2279 llvm::Value* array_elem_value = irb_.CreateLoad(array_elem_addr);
2280
Elliott Hughesadb8c672012-03-06 16:49:32 -08002281 EmitStoreDalvikReg(dec_insn.vA, elem_jty, kArray, array_elem_value);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002282
Logan Chien70f94b42011-12-27 17:49:11 +08002283 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2284}
2285
2286
2287void MethodCompiler::EmitInsn_APut(uint32_t dex_pc,
2288 Instruction const* insn,
2289 JType elem_jty) {
Logan Chien8dabb432012-01-02 23:29:32 +08002290
Elliott Hughesadb8c672012-03-06 16:49:32 -08002291 DecodedInstruction dec_insn(insn);
Logan Chien8dabb432012-01-02 23:29:32 +08002292
Elliott Hughesadb8c672012-03-06 16:49:32 -08002293 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
2294 llvm::Value* index_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate);
Logan Chien8dabb432012-01-02 23:29:32 +08002295
2296 EmitGuard_ArrayException(dex_pc, array_addr, index_value);
2297
2298 llvm::Type* elem_type = irb_.getJType(elem_jty, kArray);
2299
2300 llvm::Value* array_elem_addr =
Ian Rogers04ec04e2012-02-28 16:15:33 -08002301 EmitArrayGEP(array_addr, index_value, elem_type, elem_jty);
Logan Chien8dabb432012-01-02 23:29:32 +08002302
Elliott Hughesadb8c672012-03-06 16:49:32 -08002303 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, elem_jty, kArray);
Logan Chien8dabb432012-01-02 23:29:32 +08002304
TDYa12783bb6622012-04-17 02:20:34 -07002305 if (elem_jty == kObject) { // If put an object, check the type, and mark GC card table.
TDYa1271b86d072012-04-05 17:38:56 -07002306 llvm::Function* runtime_func = irb_.GetRuntime(CheckPutArrayElement);
2307
2308 irb_.CreateCall2(runtime_func, new_value, array_addr);
2309
2310 EmitGuard_ExceptionLandingPad(dex_pc);
TDYa12783bb6622012-04-17 02:20:34 -07002311
2312 EmitMarkGCCard(new_value, array_addr);
TDYa1271b86d072012-04-05 17:38:56 -07002313 }
2314
Logan Chien8dabb432012-01-02 23:29:32 +08002315 irb_.CreateStore(new_value, array_elem_addr);
2316
Logan Chien70f94b42011-12-27 17:49:11 +08002317 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2318}
2319
2320
2321void MethodCompiler::EmitInsn_IGet(uint32_t dex_pc,
2322 Instruction const* insn,
2323 JType field_jty) {
Logan Chien48f1d2a2012-01-02 22:49:53 +08002324
Elliott Hughesadb8c672012-03-06 16:49:32 -08002325 DecodedInstruction dec_insn(insn);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002326
Elliott Hughesadb8c672012-03-06 16:49:32 -08002327 uint32_t reg_idx = dec_insn.vB;
2328 uint32_t field_idx = dec_insn.vC;
Logan Chien48f1d2a2012-01-02 22:49:53 +08002329
Logan Chien48f1d2a2012-01-02 22:49:53 +08002330 llvm::Value* object_addr = EmitLoadDalvikReg(reg_idx, kObject, kAccurate);
2331
2332 EmitGuard_NullPointerException(dex_pc, object_addr);
2333
2334 llvm::Value* field_value;
2335
Logan Chien933abf82012-04-11 12:24:31 +08002336 int field_offset;
2337 bool is_volatile;
2338 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
2339 field_idx, oat_compilation_unit_, field_offset, is_volatile, false);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002340
Logan Chien933abf82012-04-11 12:24:31 +08002341 if (!is_fast_path) {
Logan Chien48f1d2a2012-01-02 22:49:53 +08002342 llvm::Function* runtime_func;
2343
2344 if (field_jty == kObject) {
Logan Chien3b2b2e72012-03-06 16:11:45 +08002345 runtime_func = irb_.GetRuntime(GetObjectInstance);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002346 } else if (field_jty == kLong || field_jty == kDouble) {
Logan Chien3b2b2e72012-03-06 16:11:45 +08002347 runtime_func = irb_.GetRuntime(Get64Instance);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002348 } else {
Logan Chien3b2b2e72012-03-06 16:11:45 +08002349 runtime_func = irb_.GetRuntime(Get32Instance);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002350 }
2351
2352 llvm::ConstantInt* field_idx_value = irb_.getInt32(field_idx);
2353
2354 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2355
TDYa127c8dc1012012-04-19 07:03:33 -07002356 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002357
Logan Chien3b2b2e72012-03-06 16:11:45 +08002358 field_value = irb_.CreateCall3(runtime_func, field_idx_value,
2359 method_object_addr, object_addr);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002360
2361 EmitGuard_ExceptionLandingPad(dex_pc);
2362
2363 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002364 DCHECK_GE(field_offset, 0);
2365
Logan Chien48f1d2a2012-01-02 22:49:53 +08002366 llvm::PointerType* field_type =
2367 irb_.getJType(field_jty, kField)->getPointerTo();
2368
Logan Chien933abf82012-04-11 12:24:31 +08002369 llvm::ConstantInt* field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002370
2371 llvm::Value* field_addr =
Logan Chien933abf82012-04-11 12:24:31 +08002372 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002373
Logan Chien933abf82012-04-11 12:24:31 +08002374 // TODO: Check is_volatile. We need to generate atomic load instruction
2375 // when is_volatile is true.
Logan Chien48f1d2a2012-01-02 22:49:53 +08002376 field_value = irb_.CreateLoad(field_addr);
2377 }
2378
Elliott Hughesadb8c672012-03-06 16:49:32 -08002379 EmitStoreDalvikReg(dec_insn.vA, field_jty, kField, field_value);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002380
Logan Chien70f94b42011-12-27 17:49:11 +08002381 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2382}
2383
2384
2385void MethodCompiler::EmitInsn_IPut(uint32_t dex_pc,
2386 Instruction const* insn,
2387 JType field_jty) {
Logan Chiendd6aa872012-01-03 16:06:32 +08002388
Elliott Hughesadb8c672012-03-06 16:49:32 -08002389 DecodedInstruction dec_insn(insn);
Logan Chiendd6aa872012-01-03 16:06:32 +08002390
Elliott Hughesadb8c672012-03-06 16:49:32 -08002391 uint32_t reg_idx = dec_insn.vB;
2392 uint32_t field_idx = dec_insn.vC;
Logan Chiendd6aa872012-01-03 16:06:32 +08002393
Logan Chiendd6aa872012-01-03 16:06:32 +08002394 llvm::Value* object_addr = EmitLoadDalvikReg(reg_idx, kObject, kAccurate);
2395
2396 EmitGuard_NullPointerException(dex_pc, object_addr);
2397
Elliott Hughesadb8c672012-03-06 16:49:32 -08002398 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, field_jty, kField);
Logan Chiendd6aa872012-01-03 16:06:32 +08002399
Logan Chien933abf82012-04-11 12:24:31 +08002400 int field_offset;
2401 bool is_volatile;
2402 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
2403 field_idx, oat_compilation_unit_, field_offset, is_volatile, true);
Logan Chiendd6aa872012-01-03 16:06:32 +08002404
Logan Chien933abf82012-04-11 12:24:31 +08002405 if (!is_fast_path) {
Logan Chiendd6aa872012-01-03 16:06:32 +08002406 llvm::Function* runtime_func;
2407
2408 if (field_jty == kObject) {
2409 runtime_func = irb_.GetRuntime(SetObjectInstance);
2410 } else if (field_jty == kLong || field_jty == kDouble) {
2411 runtime_func = irb_.GetRuntime(Set64Instance);
2412 } else {
2413 runtime_func = irb_.GetRuntime(Set32Instance);
2414 }
2415
2416 llvm::Value* field_idx_value = irb_.getInt32(field_idx);
2417
2418 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2419
TDYa127c8dc1012012-04-19 07:03:33 -07002420 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002421
Logan Chien3b2b2e72012-03-06 16:11:45 +08002422 irb_.CreateCall4(runtime_func, field_idx_value,
2423 method_object_addr, object_addr, new_value);
Logan Chiendd6aa872012-01-03 16:06:32 +08002424
2425 EmitGuard_ExceptionLandingPad(dex_pc);
2426
2427 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002428 DCHECK_GE(field_offset, 0);
2429
Logan Chiendd6aa872012-01-03 16:06:32 +08002430 llvm::PointerType* field_type =
2431 irb_.getJType(field_jty, kField)->getPointerTo();
2432
Logan Chien933abf82012-04-11 12:24:31 +08002433 llvm::Value* field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chiendd6aa872012-01-03 16:06:32 +08002434
2435 llvm::Value* field_addr =
Logan Chien933abf82012-04-11 12:24:31 +08002436 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
Logan Chiendd6aa872012-01-03 16:06:32 +08002437
Logan Chien933abf82012-04-11 12:24:31 +08002438 // TODO: Check is_volatile. We need to generate atomic store instruction
2439 // when is_volatile is true.
Logan Chiendd6aa872012-01-03 16:06:32 +08002440 irb_.CreateStore(new_value, field_addr);
TDYa12783bb6622012-04-17 02:20:34 -07002441
2442 if (field_jty == kObject) { // If put an object, mark the GC card table.
2443 EmitMarkGCCard(new_value, object_addr);
2444 }
Logan Chiendd6aa872012-01-03 16:06:32 +08002445 }
2446
Logan Chien70f94b42011-12-27 17:49:11 +08002447 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2448}
2449
2450
Logan Chien438c4b62012-01-17 16:06:00 +08002451llvm::Value* MethodCompiler::EmitLoadStaticStorage(uint32_t dex_pc,
2452 uint32_t type_idx) {
2453 llvm::BasicBlock* block_load_static =
2454 CreateBasicBlockWithDexPC(dex_pc, "load_static");
2455
2456 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
2457
2458 // Load static storage from dex cache
2459 llvm::Value* storage_field_addr =
2460 EmitLoadDexCacheStaticStorageFieldAddr(type_idx);
2461
2462 llvm::Value* storage_object_addr = irb_.CreateLoad(storage_field_addr);
2463
2464 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
2465
2466 // Test: Is the static storage of this class initialized?
2467 llvm::Value* equal_null =
2468 irb_.CreateICmpEQ(storage_object_addr, irb_.getJNull());
2469
2470 irb_.CreateCondBr(equal_null, block_load_static, block_cont);
2471
2472 // Failback routine to load the class object
2473 irb_.SetInsertPoint(block_load_static);
2474
2475 llvm::Function* runtime_func =
2476 irb_.GetRuntime(InitializeStaticStorage);
2477
2478 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
2479
2480 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2481
TDYa127706e9b62012-04-19 12:24:26 -07002482 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
2483
TDYa127c8dc1012012-04-19 07:03:33 -07002484 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002485
Logan Chien438c4b62012-01-17 16:06:00 +08002486 llvm::Value* loaded_storage_object_addr =
TDYa127706e9b62012-04-19 12:24:26 -07002487 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
Logan Chien438c4b62012-01-17 16:06:00 +08002488
2489 EmitGuard_ExceptionLandingPad(dex_pc);
2490
2491 llvm::BasicBlock* block_after_load_static = irb_.GetInsertBlock();
2492
2493 irb_.CreateBr(block_cont);
2494
2495 // Now the class object must be loaded
2496 irb_.SetInsertPoint(block_cont);
2497
2498 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
2499
2500 phi->addIncoming(storage_object_addr, block_original);
2501 phi->addIncoming(loaded_storage_object_addr, block_after_load_static);
2502
2503 return phi;
2504}
2505
2506
Logan Chien70f94b42011-12-27 17:49:11 +08002507void MethodCompiler::EmitInsn_SGet(uint32_t dex_pc,
2508 Instruction const* insn,
2509 JType field_jty) {
Logan Chien438c4b62012-01-17 16:06:00 +08002510
Elliott Hughesadb8c672012-03-06 16:49:32 -08002511 DecodedInstruction dec_insn(insn);
Logan Chien438c4b62012-01-17 16:06:00 +08002512
Logan Chien933abf82012-04-11 12:24:31 +08002513 uint32_t field_idx = dec_insn.vB;
Logan Chien438c4b62012-01-17 16:06:00 +08002514
Logan Chien933abf82012-04-11 12:24:31 +08002515 int field_offset;
2516 int ssb_index;
2517 bool is_referrers_class;
2518 bool is_volatile;
2519
2520 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
2521 field_idx, oat_compilation_unit_, field_offset, ssb_index,
2522 is_referrers_class, is_volatile, false);
Logan Chien438c4b62012-01-17 16:06:00 +08002523
2524 llvm::Value* static_field_value;
2525
Logan Chien933abf82012-04-11 12:24:31 +08002526 if (!is_fast_path) {
Logan Chien438c4b62012-01-17 16:06:00 +08002527 llvm::Function* runtime_func;
2528
2529 if (field_jty == kObject) {
2530 runtime_func = irb_.GetRuntime(GetObjectStatic);
2531 } else if (field_jty == kLong || field_jty == kDouble) {
2532 runtime_func = irb_.GetRuntime(Get64Static);
2533 } else {
2534 runtime_func = irb_.GetRuntime(Get32Static);
2535 }
2536
Elliott Hughesadb8c672012-03-06 16:49:32 -08002537 llvm::Constant* field_idx_value = irb_.getInt32(dec_insn.vB);
Logan Chien438c4b62012-01-17 16:06:00 +08002538
2539 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2540
TDYa127c8dc1012012-04-19 07:03:33 -07002541 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002542
Logan Chien438c4b62012-01-17 16:06:00 +08002543 static_field_value =
2544 irb_.CreateCall2(runtime_func, field_idx_value, method_object_addr);
2545
2546 EmitGuard_ExceptionLandingPad(dex_pc);
2547
2548 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002549 DCHECK_GE(field_offset, 0);
Logan Chien438c4b62012-01-17 16:06:00 +08002550
Logan Chien933abf82012-04-11 12:24:31 +08002551 llvm::Value* static_storage_addr = NULL;
2552
2553 if (is_referrers_class) {
2554 // Fast path, static storage base is this method's class
2555 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2556
TDYa127ee1f59b2012-04-25 00:56:40 -07002557 static_storage_addr =
2558 irb_.LoadFromObjectOffset(method_object_addr,
2559 Method::DeclaringClassOffset().Int32Value(),
2560 irb_.getJObjectTy());
Logan Chien933abf82012-04-11 12:24:31 +08002561 } else {
2562 // Medium path, static storage base in a different class which
2563 // requires checks that the other class is initialized
2564 DCHECK_GE(ssb_index, 0);
2565 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
2566 }
2567
2568 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chien438c4b62012-01-17 16:06:00 +08002569
2570 llvm::Value* static_field_addr =
2571 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
2572 irb_.getJType(field_jty, kField)->getPointerTo());
2573
Logan Chien933abf82012-04-11 12:24:31 +08002574 // TODO: Check is_volatile. We need to generate atomic load instruction
2575 // when is_volatile is true.
Logan Chien438c4b62012-01-17 16:06:00 +08002576 static_field_value = irb_.CreateLoad(static_field_addr);
2577 }
2578
Elliott Hughesadb8c672012-03-06 16:49:32 -08002579 EmitStoreDalvikReg(dec_insn.vA, field_jty, kField, static_field_value);
Logan Chien438c4b62012-01-17 16:06:00 +08002580
Logan Chien70f94b42011-12-27 17:49:11 +08002581 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2582}
2583
2584
2585void MethodCompiler::EmitInsn_SPut(uint32_t dex_pc,
2586 Instruction const* insn,
2587 JType field_jty) {
Logan Chien14179c82012-01-17 17:06:34 +08002588
Elliott Hughesadb8c672012-03-06 16:49:32 -08002589 DecodedInstruction dec_insn(insn);
Logan Chien14179c82012-01-17 17:06:34 +08002590
Logan Chien933abf82012-04-11 12:24:31 +08002591 uint32_t field_idx = dec_insn.vB;
Logan Chien14179c82012-01-17 17:06:34 +08002592
Elliott Hughesadb8c672012-03-06 16:49:32 -08002593 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, field_jty, kField);
Logan Chien14179c82012-01-17 17:06:34 +08002594
Logan Chien933abf82012-04-11 12:24:31 +08002595 int field_offset;
2596 int ssb_index;
2597 bool is_referrers_class;
2598 bool is_volatile;
2599
2600 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
2601 field_idx, oat_compilation_unit_, field_offset, ssb_index,
2602 is_referrers_class, is_volatile, true);
2603
2604 if (!is_fast_path) {
Logan Chien14179c82012-01-17 17:06:34 +08002605 llvm::Function* runtime_func;
2606
2607 if (field_jty == kObject) {
2608 runtime_func = irb_.GetRuntime(SetObjectStatic);
2609 } else if (field_jty == kLong || field_jty == kDouble) {
2610 runtime_func = irb_.GetRuntime(Set64Static);
2611 } else {
2612 runtime_func = irb_.GetRuntime(Set32Static);
2613 }
2614
Elliott Hughesadb8c672012-03-06 16:49:32 -08002615 llvm::Constant* field_idx_value = irb_.getInt32(dec_insn.vB);
Logan Chien14179c82012-01-17 17:06:34 +08002616
2617 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2618
TDYa127c8dc1012012-04-19 07:03:33 -07002619 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002620
Logan Chien14179c82012-01-17 17:06:34 +08002621 irb_.CreateCall3(runtime_func, field_idx_value,
2622 method_object_addr, new_value);
2623
2624 EmitGuard_ExceptionLandingPad(dex_pc);
2625
2626 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002627 DCHECK_GE(field_offset, 0);
Logan Chien14179c82012-01-17 17:06:34 +08002628
Logan Chien933abf82012-04-11 12:24:31 +08002629 llvm::Value* static_storage_addr = NULL;
2630
2631 if (is_referrers_class) {
2632 // Fast path, static storage base is this method's class
2633 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2634
TDYa127ee1f59b2012-04-25 00:56:40 -07002635 static_storage_addr =
2636 irb_.LoadFromObjectOffset(method_object_addr,
2637 Method::DeclaringClassOffset().Int32Value(),
2638 irb_.getJObjectTy());
Logan Chien933abf82012-04-11 12:24:31 +08002639 } else {
2640 // Medium path, static storage base in a different class which
2641 // requires checks that the other class is initialized
2642 DCHECK_GE(ssb_index, 0);
2643 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
2644 }
2645
2646 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chien14179c82012-01-17 17:06:34 +08002647
2648 llvm::Value* static_field_addr =
2649 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
2650 irb_.getJType(field_jty, kField)->getPointerTo());
2651
Logan Chien933abf82012-04-11 12:24:31 +08002652 // TODO: Check is_volatile. We need to generate atomic store instruction
2653 // when is_volatile is true.
Logan Chien14179c82012-01-17 17:06:34 +08002654 irb_.CreateStore(new_value, static_field_addr);
TDYa12783bb6622012-04-17 02:20:34 -07002655
2656 if (field_jty == kObject) { // If put an object, mark the GC card table.
2657 EmitMarkGCCard(new_value, static_storage_addr);
2658 }
Logan Chien14179c82012-01-17 17:06:34 +08002659 }
2660
Logan Chien70f94b42011-12-27 17:49:11 +08002661 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2662}
2663
2664
Logan Chien1a121b92012-02-15 22:23:42 +08002665void MethodCompiler::
2666EmitLoadActualParameters(std::vector<llvm::Value*>& args,
2667 uint32_t callee_method_idx,
Elliott Hughesadb8c672012-03-06 16:49:32 -08002668 DecodedInstruction const& dec_insn,
Logan Chien7e7fabc2012-04-10 18:59:11 +08002669 InvokeArgFmt arg_fmt,
Logan Chien1a121b92012-02-15 22:23:42 +08002670 bool is_static) {
2671
2672 // Get method signature
2673 DexFile::MethodId const& method_id =
2674 dex_file_->GetMethodId(callee_method_idx);
2675
Logan Chien8faf8022012-02-24 12:25:29 +08002676 uint32_t shorty_size;
Logan Chien1a121b92012-02-15 22:23:42 +08002677 char const* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size);
Logan Chien8faf8022012-02-24 12:25:29 +08002678 CHECK_GE(shorty_size, 1u);
Logan Chien1a121b92012-02-15 22:23:42 +08002679
2680 // Load argument values according to the shorty (without "this")
2681 uint16_t reg_count = 0;
2682
2683 if (!is_static) {
2684 ++reg_count; // skip the "this" pointer
2685 }
2686
Logan Chien7e7fabc2012-04-10 18:59:11 +08002687 bool is_range = (arg_fmt == kArgRange);
2688
Logan Chien8faf8022012-02-24 12:25:29 +08002689 for (uint32_t i = 1; i < shorty_size; ++i) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002690 uint32_t reg_idx = (is_range) ? (dec_insn.vC + reg_count)
2691 : (dec_insn.arg[reg_count]);
Logan Chien1a121b92012-02-15 22:23:42 +08002692
2693 args.push_back(EmitLoadDalvikReg(reg_idx, shorty[i], kAccurate));
2694
2695 ++reg_count;
2696 if (shorty[i] == 'J' || shorty[i] == 'D') {
2697 // Wide types, such as long and double, are using a pair of registers
2698 // to store the value, so we have to increase arg_reg again.
2699 ++reg_count;
2700 }
2701 }
2702
Elliott Hughesadb8c672012-03-06 16:49:32 -08002703 DCHECK_EQ(reg_count, dec_insn.vA)
Logan Chien1a121b92012-02-15 22:23:42 +08002704 << "Actual argument mismatch for callee: "
2705 << PrettyMethod(callee_method_idx, *dex_file_);
2706}
2707
TDYa1270b686e52012-04-09 22:43:35 -07002708llvm::Value* MethodCompiler::EmitFixStub(llvm::Value* callee_method_object_addr,
2709 uint32_t method_idx,
2710 bool is_static) {
2711 // TODO: Remove this after we solve the link and trampoline related problems.
2712 llvm::Value* code_addr = irb_.CreateCall(irb_.GetRuntime(FixStub), callee_method_object_addr);
2713
2714 llvm::FunctionType* method_type = GetFunctionType(method_idx, is_static);
2715
2716 return irb_.CreatePointerCast(code_addr, method_type->getPointerTo());
TDYa12785321912012-04-01 15:24:56 -07002717}
Logan Chien1a121b92012-02-15 22:23:42 +08002718
Shih-wei Liao399ed3f2012-03-08 01:27:04 -08002719
Logan Chien7e7fabc2012-04-10 18:59:11 +08002720void MethodCompiler::EmitInsn_Invoke(uint32_t dex_pc,
2721 Instruction const* insn,
2722 InvokeType invoke_type,
2723 InvokeArgFmt arg_fmt) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002724 DecodedInstruction dec_insn(insn);
Logan Chien46fbb412012-02-15 22:29:08 +08002725
Logan Chien61c65dc2012-02-29 03:22:30 +08002726 bool is_static = (invoke_type == kStatic);
Logan Chien7e7fabc2012-04-10 18:59:11 +08002727 uint32_t callee_method_idx = dec_insn.vB;
Logan Chien61c65dc2012-02-29 03:22:30 +08002728
Logan Chien7e7fabc2012-04-10 18:59:11 +08002729 // Compute invoke related information for compiler decision
2730 int vtable_idx = -1;
Logan Chien92ad16d2012-03-18 05:48:55 +08002731 uintptr_t direct_code = 0; // Currently unused
Logan Chienfca64372012-04-23 14:57:01 +08002732 uintptr_t direct_method = 0;
Logan Chien61c65dc2012-02-29 03:22:30 +08002733 bool is_fast_path = compiler_->
Logan Chien7e7fabc2012-04-10 18:59:11 +08002734 ComputeInvokeInfo(callee_method_idx, oat_compilation_unit_,
Logan Chien92ad16d2012-03-18 05:48:55 +08002735 invoke_type, vtable_idx, direct_code, direct_method);
Logan Chien61c65dc2012-02-29 03:22:30 +08002736
Logan Chien7e7fabc2012-04-10 18:59:11 +08002737 // Load *this* actual parameter
Logan Chien1a121b92012-02-15 22:23:42 +08002738 llvm::Value* this_addr = NULL;
2739
2740 if (!is_static) {
2741 // Test: Is *this* parameter equal to null?
Logan Chien7e7fabc2012-04-10 18:59:11 +08002742 this_addr = (arg_fmt == kArgReg) ?
2743 EmitLoadDalvikReg(dec_insn.arg[0], kObject, kAccurate):
2744 EmitLoadDalvikReg(dec_insn.vC + 0, kObject, kAccurate);
2745
Logan Chien1a121b92012-02-15 22:23:42 +08002746 EmitGuard_NullPointerException(dex_pc, this_addr);
2747 }
2748
Logan Chien7e7fabc2012-04-10 18:59:11 +08002749 // Load the method object
TDYa1274e42a592012-04-10 20:13:54 -07002750 llvm::Value* callee_method_object_addr = NULL;
Logan Chien7e7fabc2012-04-10 18:59:11 +08002751
2752 if (!is_fast_path) {
TDYa1274e42a592012-04-10 20:13:54 -07002753 callee_method_object_addr =
Logan Chien7e7fabc2012-04-10 18:59:11 +08002754 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx, invoke_type,
2755 this_addr, dex_pc, is_fast_path);
2756 } else {
2757 switch (invoke_type) {
2758 case kStatic:
2759 case kDirect:
Logan Chienfca64372012-04-23 14:57:01 +08002760 if (direct_method != 0u &&
2761 direct_method != static_cast<uintptr_t>(-1)) {
2762 callee_method_object_addr =
TDYa12717826bf2012-04-24 01:15:10 -07002763 irb_.CreateIntToPtr(irb_.getPtrEquivInt(direct_method),
2764 irb_.getJObjectTy());
Logan Chienfca64372012-04-23 14:57:01 +08002765 } else {
2766 callee_method_object_addr =
2767 EmitLoadSDCalleeMethodObjectAddr(callee_method_idx);
2768 }
Logan Chien7e7fabc2012-04-10 18:59:11 +08002769 break;
2770
2771 case kVirtual:
2772 DCHECK(vtable_idx != -1);
TDYa1274e42a592012-04-10 20:13:54 -07002773 callee_method_object_addr =
Logan Chien7e7fabc2012-04-10 18:59:11 +08002774 EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr);
2775 break;
2776
2777 case kSuper:
2778 LOG(FATAL) << "invoke-super should be promoted to invoke-direct in "
2779 "the fast path.";
2780 break;
2781
2782 case kInterface:
TDYa1274e42a592012-04-10 20:13:54 -07002783 callee_method_object_addr =
Logan Chien7e7fabc2012-04-10 18:59:11 +08002784 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx,
2785 invoke_type, this_addr,
2786 dex_pc, is_fast_path);
2787 break;
2788 }
2789 }
2790
Logan Chien7e7fabc2012-04-10 18:59:11 +08002791 llvm::Value* code_addr =
TDYa127ee1f59b2012-04-25 00:56:40 -07002792 irb_.LoadFromObjectOffset(callee_method_object_addr,
2793 Method::GetCodeOffset().Int32Value(),
2794 GetFunctionType(callee_method_idx, is_static)->getPointerTo());
TDYa12785321912012-04-01 15:24:56 -07002795
Logan Chien1a121b92012-02-15 22:23:42 +08002796 // Load the actual parameter
2797 std::vector<llvm::Value*> args;
2798
2799 args.push_back(callee_method_object_addr); // method object for callee
2800
2801 if (!is_static) {
Logan Chien7e7fabc2012-04-10 18:59:11 +08002802 DCHECK(this_addr != NULL);
Logan Chien1a121b92012-02-15 22:23:42 +08002803 args.push_back(this_addr); // "this" object for callee
2804 }
2805
2806 EmitLoadActualParameters(args, callee_method_idx, dec_insn,
Logan Chien7e7fabc2012-04-10 18:59:11 +08002807 arg_fmt, is_static);
Logan Chien1a121b92012-02-15 22:23:42 +08002808
TDYa1275bb86012012-04-11 05:57:28 -07002809#if 0
Logan Chien8dfcbea2012-02-17 18:50:32 +08002810 // Invoke callee
TDYa127c8dc1012012-04-19 07:03:33 -07002811 EmitUpdateDexPC(dex_pc);
Logan Chien1a121b92012-02-15 22:23:42 +08002812 llvm::Value* retval = irb_.CreateCall(code_addr, args);
2813 EmitGuard_ExceptionLandingPad(dex_pc);
2814
Logan Chien7e7fabc2012-04-10 18:59:11 +08002815 uint32_t callee_access_flags = is_static ? kAccStatic : 0;
2816 UniquePtr<OatCompilationUnit> callee_oat_compilation_unit(
2817 oat_compilation_unit_->GetCallee(callee_method_idx, callee_access_flags));
2818
2819 char ret_shorty = callee_oat_compilation_unit->GetShorty()[0];
Shih-wei Liao90d50992012-02-19 03:32:05 -08002820 if (ret_shorty != 'V') {
Logan Chien1a121b92012-02-15 22:23:42 +08002821 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
2822 }
TDYa1275bb86012012-04-11 05:57:28 -07002823#else
2824 uint32_t callee_access_flags = is_static ? kAccStatic : 0;
2825 UniquePtr<OatCompilationUnit> callee_oat_compilation_unit(
2826 oat_compilation_unit_->GetCallee(callee_method_idx, callee_access_flags));
2827
2828 char ret_shorty = callee_oat_compilation_unit->GetShorty()[0];
2829
2830
TDYa127c8dc1012012-04-19 07:03:33 -07002831 EmitUpdateDexPC(dex_pc);
TDYa1275bb86012012-04-11 05:57:28 -07002832
2833
2834 llvm::BasicBlock* block_normal = CreateBasicBlockWithDexPC(dex_pc, "normal");
TDYa127ce154722012-04-21 16:43:29 -07002835 llvm::BasicBlock* block_stub = CreateBasicBlockWithDexPC(dex_pc, "stub");
TDYa1275bb86012012-04-11 05:57:28 -07002836 llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont");
2837
2838 llvm::Type* accurate_ret_type = irb_.getJType(ret_shorty, kAccurate);
2839 llvm::Value* retval_addr = NULL;
2840 if (ret_shorty != 'V') {
2841 retval_addr = irb_.CreateAlloca(accurate_ret_type);
2842 }
2843
2844
TDYa1275bb86012012-04-11 05:57:28 -07002845 llvm::Value* code_addr_int = irb_.CreatePtrToInt(code_addr, irb_.getPtrEquivIntTy());
TDYa127ce154722012-04-21 16:43:29 -07002846 llvm::Value* max_stub_int = irb_.getPtrEquivInt(special_stub::kMaxSpecialStub);
2847 llvm::Value* is_stub = irb_.CreateICmpULT(code_addr_int, max_stub_int);
2848 irb_.CreateCondBr(is_stub, block_stub, block_normal);
TDYa1275bb86012012-04-11 05:57:28 -07002849
2850
2851 irb_.SetInsertPoint(block_normal);
2852 {
2853 // Invoke callee
TDYa127ce154722012-04-21 16:43:29 -07002854 llvm::Value* retval = irb_.CreateCall(code_addr, args);
TDYa1275bb86012012-04-11 05:57:28 -07002855 if (ret_shorty != 'V') {
TDYa127ce154722012-04-21 16:43:29 -07002856 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
TDYa1275bb86012012-04-11 05:57:28 -07002857 }
2858 }
2859 irb_.CreateBr(block_continue);
2860
2861
TDYa127ce154722012-04-21 16:43:29 -07002862 irb_.SetInsertPoint(block_stub);
TDYa1275bb86012012-04-11 05:57:28 -07002863 {
TDYa127ce154722012-04-21 16:43:29 -07002864 { // lazy link
2865 // TODO: Remove this after we solve the link problem.
2866 llvm::BasicBlock* block_proxy_stub = CreateBasicBlockWithDexPC(dex_pc, "proxy");
2867 llvm::BasicBlock* block_link = CreateBasicBlockWithDexPC(dex_pc, "link");
2868
2869 irb_.CreateCondBr(irb_.CreateIsNull(code_addr), block_link, block_proxy_stub);
2870
2871
2872 irb_.SetInsertPoint(block_link);
2873 code_addr = EmitFixStub(callee_method_object_addr, callee_method_idx, is_static);
2874
2875 EmitGuard_ExceptionLandingPad(dex_pc);
2876
2877 llvm::Value* retval = irb_.CreateCall(code_addr, args);
2878 if (ret_shorty != 'V') {
2879 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
2880 }
2881 irb_.CreateBr(block_continue);
2882
2883
2884 irb_.SetInsertPoint(block_proxy_stub);
TDYa1275bb86012012-04-11 05:57:28 -07002885 }
TDYa127ce154722012-04-21 16:43:29 -07002886 { // proxy stub
2887 llvm::Value* temp_space_addr;
2888 if (ret_shorty != 'V') {
2889 temp_space_addr = irb_.CreateAlloca(irb_.getJValueTy());
2890 args.push_back(temp_space_addr);
2891 }
2892 // TODO: Remove this after we solve the proxy trampoline calling convention problem.
2893 irb_.CreateCall(irb_.GetRuntime(ProxyInvokeHandler), args);
2894 if (ret_shorty != 'V') {
2895 llvm::Value* result_addr =
2896 irb_.CreateBitCast(temp_space_addr, accurate_ret_type->getPointerTo());
2897 llvm::Value* retval = irb_.CreateLoad(result_addr);
2898 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
2899 }
TDYa1275bb86012012-04-11 05:57:28 -07002900 }
2901 }
2902 irb_.CreateBr(block_continue);
2903
2904
2905 irb_.SetInsertPoint(block_continue);
2906
TDYa1275bb86012012-04-11 05:57:28 -07002907 EmitGuard_ExceptionLandingPad(dex_pc);
2908#endif
Logan Chien1a121b92012-02-15 22:23:42 +08002909
Logan Chien70f94b42011-12-27 17:49:11 +08002910 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2911}
2912
2913
Logan Chien7e7fabc2012-04-10 18:59:11 +08002914llvm::Value* MethodCompiler::
2915EmitLoadSDCalleeMethodObjectAddr(uint32_t callee_method_idx) {
2916 llvm::Value* callee_method_object_field_addr =
2917 EmitLoadDexCacheResolvedMethodFieldAddr(callee_method_idx);
Logan Chien7caf37e2012-02-03 22:56:04 +08002918
Logan Chien7e7fabc2012-04-10 18:59:11 +08002919 return irb_.CreateLoad(callee_method_object_field_addr);
2920}
Logan Chien7caf37e2012-02-03 22:56:04 +08002921
Logan Chien7caf37e2012-02-03 22:56:04 +08002922
Logan Chien7e7fabc2012-04-10 18:59:11 +08002923llvm::Value* MethodCompiler::
2924EmitLoadVirtualCalleeMethodObjectAddr(int vtable_idx,
2925 llvm::Value* this_addr) {
2926 // Load class object of *this* pointer
TDYa127ee1f59b2012-04-25 00:56:40 -07002927 llvm::Value* class_object_addr =
2928 irb_.LoadFromObjectOffset(this_addr,
2929 Object::ClassOffset().Int32Value(),
2930 irb_.getJObjectTy());
Logan Chien7caf37e2012-02-03 22:56:04 +08002931
Logan Chien7e7fabc2012-04-10 18:59:11 +08002932 // Load vtable address
TDYa127ee1f59b2012-04-25 00:56:40 -07002933 llvm::Value* vtable_addr =
2934 irb_.LoadFromObjectOffset(class_object_addr,
2935 Class::VTableOffset().Int32Value(),
2936 irb_.getJObjectTy());
Logan Chien7e7fabc2012-04-10 18:59:11 +08002937
2938 // Load callee method object
2939 llvm::Value* vtable_idx_value =
2940 irb_.getPtrEquivInt(static_cast<uint64_t>(vtable_idx));
2941
2942 llvm::Value* method_field_addr =
2943 EmitArrayGEP(vtable_addr, vtable_idx_value, irb_.getJObjectTy(), kObject);
2944
2945 return irb_.CreateLoad(method_field_addr);
2946}
2947
2948
2949llvm::Value* MethodCompiler::
2950EmitCallRuntimeForCalleeMethodObjectAddr(uint32_t callee_method_idx,
2951 InvokeType invoke_type,
2952 llvm::Value* this_addr,
2953 uint32_t dex_pc,
2954 bool is_fast_path) {
2955
2956 llvm::Function* runtime_func = NULL;
2957
2958 switch (invoke_type) {
2959 case kStatic:
2960 runtime_func = irb_.GetRuntime(FindStaticMethodWithAccessCheck);
2961 break;
2962
2963 case kDirect:
2964 runtime_func = irb_.GetRuntime(FindDirectMethodWithAccessCheck);
2965 break;
2966
2967 case kVirtual:
2968 runtime_func = irb_.GetRuntime(FindVirtualMethodWithAccessCheck);
2969 break;
2970
2971 case kSuper:
2972 runtime_func = irb_.GetRuntime(FindSuperMethodWithAccessCheck);
2973 break;
2974
2975 case kInterface:
2976 if (is_fast_path) {
2977 runtime_func = irb_.GetRuntime(FindInterfaceMethod);
2978 } else {
2979 runtime_func = irb_.GetRuntime(FindInterfaceMethodWithAccessCheck);
2980 }
2981 break;
2982 }
Logan Chien7caf37e2012-02-03 22:56:04 +08002983
2984 llvm::Value* callee_method_idx_value = irb_.getInt32(callee_method_idx);
2985
Logan Chien7e7fabc2012-04-10 18:59:11 +08002986 if (this_addr == NULL) {
2987 DCHECK_EQ(invoke_type, kStatic);
2988 this_addr = irb_.getJNull();
2989 }
2990
2991 llvm::Value* caller_method_object_addr = EmitLoadMethodObjectAddr();
2992
TDYa127da83d972012-04-18 00:21:49 -07002993 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
2994
TDYa127c8dc1012012-04-19 07:03:33 -07002995 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002996
TDYa1270b686e52012-04-09 22:43:35 -07002997 llvm::Value* callee_method_object_addr =
TDYa127da83d972012-04-18 00:21:49 -07002998 irb_.CreateCall4(runtime_func,
Logan Chien7e7fabc2012-04-10 18:59:11 +08002999 callee_method_idx_value,
3000 this_addr,
TDYa127da83d972012-04-18 00:21:49 -07003001 caller_method_object_addr,
3002 thread_object_addr);
Logan Chien7caf37e2012-02-03 22:56:04 +08003003
3004 EmitGuard_ExceptionLandingPad(dex_pc);
3005
Logan Chien7e7fabc2012-04-10 18:59:11 +08003006 return callee_method_object_addr;
Logan Chien70f94b42011-12-27 17:49:11 +08003007}
3008
3009
3010void MethodCompiler::EmitInsn_Neg(uint32_t dex_pc,
3011 Instruction const* insn,
3012 JType op_jty) {
Logan Chien1b5685f2011-12-27 18:01:14 +08003013
Elliott Hughesadb8c672012-03-06 16:49:32 -08003014 DecodedInstruction dec_insn(insn);
Logan Chien1b5685f2011-12-27 18:01:14 +08003015
3016 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3017
Elliott Hughesadb8c672012-03-06 16:49:32 -08003018 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chien1b5685f2011-12-27 18:01:14 +08003019 llvm::Value* result_value = irb_.CreateNeg(src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003020 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chien1b5685f2011-12-27 18:01:14 +08003021
Logan Chien70f94b42011-12-27 17:49:11 +08003022 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3023}
3024
3025
3026void MethodCompiler::EmitInsn_Not(uint32_t dex_pc,
3027 Instruction const* insn,
3028 JType op_jty) {
Logan Chiene53750d2011-12-27 18:02:27 +08003029
Elliott Hughesadb8c672012-03-06 16:49:32 -08003030 DecodedInstruction dec_insn(insn);
Logan Chiene53750d2011-12-27 18:02:27 +08003031
3032 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3033
Elliott Hughesadb8c672012-03-06 16:49:32 -08003034 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chiene53750d2011-12-27 18:02:27 +08003035 llvm::Value* result_value =
3036 irb_.CreateXor(src_value, static_cast<uint64_t>(-1));
3037
Elliott Hughesadb8c672012-03-06 16:49:32 -08003038 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chiene53750d2011-12-27 18:02:27 +08003039
Logan Chien70f94b42011-12-27 17:49:11 +08003040 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3041}
3042
3043
3044void MethodCompiler::EmitInsn_SExt(uint32_t dex_pc,
3045 Instruction const* insn) {
Logan Chien61752ad2011-12-27 18:03:51 +08003046
Elliott Hughesadb8c672012-03-06 16:49:32 -08003047 DecodedInstruction dec_insn(insn);
Logan Chien61752ad2011-12-27 18:03:51 +08003048
Elliott Hughesadb8c672012-03-06 16:49:32 -08003049 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chien61752ad2011-12-27 18:03:51 +08003050 llvm::Value* result_value = irb_.CreateSExt(src_value, irb_.getJLongTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003051 EmitStoreDalvikReg(dec_insn.vA, kLong, kAccurate, result_value);
Logan Chien61752ad2011-12-27 18:03:51 +08003052
Logan Chien70f94b42011-12-27 17:49:11 +08003053 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3054}
3055
3056
3057void MethodCompiler::EmitInsn_Trunc(uint32_t dex_pc,
3058 Instruction const* insn) {
Logan Chien17a57662011-12-27 18:05:14 +08003059
Elliott Hughesadb8c672012-03-06 16:49:32 -08003060 DecodedInstruction dec_insn(insn);
Logan Chien17a57662011-12-27 18:05:14 +08003061
Elliott Hughesadb8c672012-03-06 16:49:32 -08003062 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kLong, kAccurate);
Logan Chien17a57662011-12-27 18:05:14 +08003063 llvm::Value* result_value = irb_.CreateTrunc(src_value, irb_.getJIntTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003064 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chien17a57662011-12-27 18:05:14 +08003065
Logan Chien70f94b42011-12-27 17:49:11 +08003066 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3067}
3068
3069
3070void MethodCompiler::EmitInsn_TruncAndSExt(uint32_t dex_pc,
3071 Instruction const* insn,
3072 unsigned N) {
Logan Chienb6744c52011-12-27 18:06:26 +08003073
Elliott Hughesadb8c672012-03-06 16:49:32 -08003074 DecodedInstruction dec_insn(insn);
Logan Chienb6744c52011-12-27 18:06:26 +08003075
Elliott Hughesadb8c672012-03-06 16:49:32 -08003076 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chienb6744c52011-12-27 18:06:26 +08003077
3078 llvm::Value* trunc_value =
3079 irb_.CreateTrunc(src_value, llvm::Type::getIntNTy(*context_, N));
3080
3081 llvm::Value* result_value = irb_.CreateSExt(trunc_value, irb_.getJIntTy());
3082
Elliott Hughesadb8c672012-03-06 16:49:32 -08003083 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chienb6744c52011-12-27 18:06:26 +08003084
Logan Chien70f94b42011-12-27 17:49:11 +08003085 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3086}
3087
3088
3089void MethodCompiler::EmitInsn_TruncAndZExt(uint32_t dex_pc,
3090 Instruction const* insn,
3091 unsigned N) {
Logan Chienb6744c52011-12-27 18:06:26 +08003092
Elliott Hughesadb8c672012-03-06 16:49:32 -08003093 DecodedInstruction dec_insn(insn);
Logan Chienb6744c52011-12-27 18:06:26 +08003094
Elliott Hughesadb8c672012-03-06 16:49:32 -08003095 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chienb6744c52011-12-27 18:06:26 +08003096
3097 llvm::Value* trunc_value =
3098 irb_.CreateTrunc(src_value, llvm::Type::getIntNTy(*context_, N));
3099
3100 llvm::Value* result_value = irb_.CreateZExt(trunc_value, irb_.getJIntTy());
3101
Elliott Hughesadb8c672012-03-06 16:49:32 -08003102 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chienb6744c52011-12-27 18:06:26 +08003103
Logan Chien70f94b42011-12-27 17:49:11 +08003104 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3105}
3106
3107
3108void MethodCompiler::EmitInsn_FNeg(uint32_t dex_pc,
3109 Instruction const* insn,
3110 JType op_jty) {
Logan Chien7a48b092011-12-27 18:07:45 +08003111
Elliott Hughesadb8c672012-03-06 16:49:32 -08003112 DecodedInstruction dec_insn(insn);
Logan Chien7a48b092011-12-27 18:07:45 +08003113
3114 DCHECK(op_jty == kFloat || op_jty == kDouble) << op_jty;
3115
Elliott Hughesadb8c672012-03-06 16:49:32 -08003116 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chien7a48b092011-12-27 18:07:45 +08003117 llvm::Value* result_value = irb_.CreateFNeg(src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003118 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chien7a48b092011-12-27 18:07:45 +08003119
Logan Chien70f94b42011-12-27 17:49:11 +08003120 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3121}
3122
3123
3124void MethodCompiler::EmitInsn_IntToFP(uint32_t dex_pc,
3125 Instruction const* insn,
3126 JType src_jty,
3127 JType dest_jty) {
Logan Chien62dd4532011-12-27 18:09:00 +08003128
Elliott Hughesadb8c672012-03-06 16:49:32 -08003129 DecodedInstruction dec_insn(insn);
Logan Chien62dd4532011-12-27 18:09:00 +08003130
3131 DCHECK(src_jty == kInt || src_jty == kLong) << src_jty;
3132 DCHECK(dest_jty == kFloat || dest_jty == kDouble) << dest_jty;
3133
Elliott Hughesadb8c672012-03-06 16:49:32 -08003134 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, src_jty, kAccurate);
Logan Chien62dd4532011-12-27 18:09:00 +08003135 llvm::Type* dest_type = irb_.getJType(dest_jty, kAccurate);
3136 llvm::Value* dest_value = irb_.CreateSIToFP(src_value, dest_type);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003137 EmitStoreDalvikReg(dec_insn.vA, dest_jty, kAccurate, dest_value);
Logan Chien62dd4532011-12-27 18:09:00 +08003138
Logan Chien70f94b42011-12-27 17:49:11 +08003139 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3140}
3141
3142
3143void MethodCompiler::EmitInsn_FPToInt(uint32_t dex_pc,
3144 Instruction const* insn,
3145 JType src_jty,
TDYa127a4746872012-04-11 23:48:55 -07003146 JType dest_jty,
3147 runtime_support::RuntimeId runtime_func_id) {
Logan Chien12dc1752011-12-27 18:10:15 +08003148
Elliott Hughesadb8c672012-03-06 16:49:32 -08003149 DecodedInstruction dec_insn(insn);
Logan Chien12dc1752011-12-27 18:10:15 +08003150
3151 DCHECK(src_jty == kFloat || src_jty == kDouble) << src_jty;
3152 DCHECK(dest_jty == kInt || dest_jty == kLong) << dest_jty;
3153
Elliott Hughesadb8c672012-03-06 16:49:32 -08003154 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, src_jty, kAccurate);
TDYa127a4746872012-04-11 23:48:55 -07003155 llvm::Value* dest_value = irb_.CreateCall(irb_.GetRuntime(runtime_func_id), src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003156 EmitStoreDalvikReg(dec_insn.vA, dest_jty, kAccurate, dest_value);
Logan Chien12dc1752011-12-27 18:10:15 +08003157
Logan Chien70f94b42011-12-27 17:49:11 +08003158 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3159}
3160
3161
3162void MethodCompiler::EmitInsn_FExt(uint32_t dex_pc,
3163 Instruction const* insn) {
Logan Chienc56ded92011-12-27 18:10:57 +08003164
Elliott Hughesadb8c672012-03-06 16:49:32 -08003165 DecodedInstruction dec_insn(insn);
Logan Chienc56ded92011-12-27 18:10:57 +08003166
Elliott Hughesadb8c672012-03-06 16:49:32 -08003167 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kFloat, kAccurate);
Logan Chienc56ded92011-12-27 18:10:57 +08003168 llvm::Value* result_value = irb_.CreateFPExt(src_value, irb_.getJDoubleTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003169 EmitStoreDalvikReg(dec_insn.vA, kDouble, kAccurate, result_value);
Logan Chienc56ded92011-12-27 18:10:57 +08003170
Logan Chien70f94b42011-12-27 17:49:11 +08003171 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3172}
3173
3174
3175void MethodCompiler::EmitInsn_FTrunc(uint32_t dex_pc,
3176 Instruction const* insn) {
Logan Chien927744f2011-12-27 18:11:52 +08003177
Elliott Hughesadb8c672012-03-06 16:49:32 -08003178 DecodedInstruction dec_insn(insn);
Logan Chien927744f2011-12-27 18:11:52 +08003179
Elliott Hughesadb8c672012-03-06 16:49:32 -08003180 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kDouble, kAccurate);
Logan Chien927744f2011-12-27 18:11:52 +08003181 llvm::Value* result_value = irb_.CreateFPTrunc(src_value, irb_.getJFloatTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003182 EmitStoreDalvikReg(dec_insn.vA, kFloat, kAccurate, result_value);
Logan Chien927744f2011-12-27 18:11:52 +08003183
Logan Chien70f94b42011-12-27 17:49:11 +08003184 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3185}
3186
3187
3188void MethodCompiler::EmitInsn_IntArithm(uint32_t dex_pc,
3189 Instruction const* insn,
3190 IntArithmKind arithm,
3191 JType op_jty,
3192 bool is_2addr) {
Logan Chienc3f7d962011-12-27 18:13:18 +08003193
Elliott Hughesadb8c672012-03-06 16:49:32 -08003194 DecodedInstruction dec_insn(insn);
Logan Chienc3f7d962011-12-27 18:13:18 +08003195
3196 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3197
3198 llvm::Value* src1_value;
3199 llvm::Value* src2_value;
3200
3201 if (is_2addr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003202 src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate);
3203 src2_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chienc3f7d962011-12-27 18:13:18 +08003204 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003205 src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
3206 src2_value = EmitLoadDalvikReg(dec_insn.vC, op_jty, kAccurate);
Logan Chienc3f7d962011-12-27 18:13:18 +08003207 }
3208
3209 llvm::Value* result_value =
3210 EmitIntArithmResultComputation(dex_pc, src1_value, src2_value,
3211 arithm, op_jty);
3212
Elliott Hughesadb8c672012-03-06 16:49:32 -08003213 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chienc3f7d962011-12-27 18:13:18 +08003214
Logan Chien70f94b42011-12-27 17:49:11 +08003215 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3216}
3217
3218
3219void MethodCompiler::EmitInsn_IntArithmImmediate(uint32_t dex_pc,
3220 Instruction const* insn,
3221 IntArithmKind arithm) {
Logan Chienc3f7d962011-12-27 18:13:18 +08003222
Elliott Hughesadb8c672012-03-06 16:49:32 -08003223 DecodedInstruction dec_insn(insn);
Logan Chienc3f7d962011-12-27 18:13:18 +08003224
Elliott Hughesadb8c672012-03-06 16:49:32 -08003225 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chienc3f7d962011-12-27 18:13:18 +08003226
Elliott Hughesadb8c672012-03-06 16:49:32 -08003227 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC);
Logan Chienc3f7d962011-12-27 18:13:18 +08003228
3229 llvm::Value* result_value =
3230 EmitIntArithmResultComputation(dex_pc, src_value, imm_value, arithm, kInt);
3231
Elliott Hughesadb8c672012-03-06 16:49:32 -08003232 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chienc3f7d962011-12-27 18:13:18 +08003233
Logan Chien70f94b42011-12-27 17:49:11 +08003234 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3235}
3236
3237
Logan Chienc3f7d962011-12-27 18:13:18 +08003238llvm::Value*
3239MethodCompiler::EmitIntArithmResultComputation(uint32_t dex_pc,
3240 llvm::Value* lhs,
3241 llvm::Value* rhs,
3242 IntArithmKind arithm,
3243 JType op_jty) {
3244 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3245
3246 switch (arithm) {
3247 case kIntArithm_Add:
3248 return irb_.CreateAdd(lhs, rhs);
3249
3250 case kIntArithm_Sub:
3251 return irb_.CreateSub(lhs, rhs);
3252
3253 case kIntArithm_Mul:
3254 return irb_.CreateMul(lhs, rhs);
3255
3256 case kIntArithm_Div:
Logan Chienc3f7d962011-12-27 18:13:18 +08003257 case kIntArithm_Rem:
TDYa127f8641ce2012-04-02 06:40:40 -07003258 return EmitIntDivRemResultComputation(dex_pc, lhs, rhs, arithm, op_jty);
Logan Chienc3f7d962011-12-27 18:13:18 +08003259
3260 case kIntArithm_And:
3261 return irb_.CreateAnd(lhs, rhs);
3262
3263 case kIntArithm_Or:
3264 return irb_.CreateOr(lhs, rhs);
3265
3266 case kIntArithm_Xor:
3267 return irb_.CreateXor(lhs, rhs);
3268
Logan Chienc3f7d962011-12-27 18:13:18 +08003269 default:
3270 LOG(FATAL) << "Unknown integer arithmetic kind: " << arithm;
3271 return NULL;
3272 }
3273}
3274
3275
TDYa127f8641ce2012-04-02 06:40:40 -07003276llvm::Value*
3277MethodCompiler::EmitIntDivRemResultComputation(uint32_t dex_pc,
3278 llvm::Value* dividend,
3279 llvm::Value* divisor,
3280 IntArithmKind arithm,
3281 JType op_jty) {
3282 // Throw exception if the divisor is 0.
3283 EmitGuard_DivZeroException(dex_pc, divisor, op_jty);
3284
3285 // Check the special case: MININT / -1 = MININT
3286 // That case will cause overflow, which is undefined behavior in llvm.
3287 // So we check the divisor is -1 or not, if the divisor is -1, we do
3288 // the special path to avoid undefined behavior.
3289 llvm::Type* op_type = irb_.getJType(op_jty, kAccurate);
3290 llvm::Value* zero = irb_.getJZero(op_jty);
3291 llvm::Value* neg_one = llvm::ConstantInt::getSigned(op_type, -1);
3292 llvm::Value* result = irb_.CreateAlloca(op_type);
3293
3294 llvm::BasicBlock* eq_neg_one = CreateBasicBlockWithDexPC(dex_pc, "eq_neg_one");
3295 llvm::BasicBlock* ne_neg_one = CreateBasicBlockWithDexPC(dex_pc, "ne_neg_one");
3296 llvm::BasicBlock* neg_one_cont = CreateBasicBlockWithDexPC(dex_pc, "neg_one_cont");
3297
3298 llvm::Value* is_equal_neg_one = EmitConditionResult(divisor, neg_one, kCondBranch_EQ);
3299 irb_.CreateCondBr(is_equal_neg_one, eq_neg_one, ne_neg_one);
3300
3301 // If divisor == -1
3302 irb_.SetInsertPoint(eq_neg_one);
3303 llvm::Value* eq_result;
3304 if (arithm == kIntArithm_Div) {
3305 // We can just change from "dividend div -1" to "neg dividend".
3306 // The sub don't care the sign/unsigned because of two's complement representation.
3307 // And the behavior is what we want:
3308 // -(2^n) (2^n)-1
3309 // MININT < k <= MAXINT -> mul k -1 = -k
3310 // MININT == k -> mul k -1 = k
3311 //
3312 // LLVM use sub to represent 'neg'
3313 eq_result = irb_.CreateSub(zero, dividend);
3314 } else {
3315 // Everything modulo -1 will be 0.
3316 eq_result = zero;
3317 }
3318 irb_.CreateStore(eq_result, result);
3319 irb_.CreateBr(neg_one_cont);
3320
3321 // If divisor != -1, just do the division.
3322 irb_.SetInsertPoint(ne_neg_one);
3323 llvm::Value* ne_result;
3324 if (arithm == kIntArithm_Div) {
3325 ne_result = irb_.CreateSDiv(dividend, divisor);
3326 } else {
3327 ne_result = irb_.CreateSRem(dividend, divisor);
3328 }
3329 irb_.CreateStore(ne_result, result);
3330 irb_.CreateBr(neg_one_cont);
3331
3332 irb_.SetInsertPoint(neg_one_cont);
3333 return irb_.CreateLoad(result);
3334}
3335
3336
Logan Chien5539ad02012-04-02 14:36:55 +08003337void MethodCompiler::EmitInsn_IntShiftArithm(uint32_t dex_pc,
3338 Instruction const* insn,
3339 IntShiftArithmKind arithm,
3340 JType op_jty,
3341 bool is_2addr) {
3342
3343 DecodedInstruction dec_insn(insn);
3344
3345 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3346
3347 llvm::Value* src1_value;
3348 llvm::Value* src2_value;
3349
3350 // NOTE: The 2nd operand of the shift arithmetic instruction is
3351 // 32-bit integer regardless of the 1st operand.
3352 if (is_2addr) {
3353 src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate);
3354 src2_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
3355 } else {
3356 src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
3357 src2_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate);
3358 }
3359
3360 llvm::Value* result_value =
3361 EmitIntShiftArithmResultComputation(dex_pc, src1_value, src2_value,
3362 arithm, op_jty);
3363
3364 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
3365
3366 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3367}
3368
3369
3370void MethodCompiler::
3371EmitInsn_IntShiftArithmImmediate(uint32_t dex_pc,
3372 Instruction const* insn,
3373 IntShiftArithmKind arithm) {
3374
3375 DecodedInstruction dec_insn(insn);
3376
3377 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
3378
3379 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC);
3380
3381 llvm::Value* result_value =
3382 EmitIntShiftArithmResultComputation(dex_pc, src_value, imm_value,
3383 arithm, kInt);
3384
3385 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
3386
3387 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3388}
3389
3390
3391llvm::Value*
3392MethodCompiler::EmitIntShiftArithmResultComputation(uint32_t dex_pc,
3393 llvm::Value* lhs,
3394 llvm::Value* rhs,
3395 IntShiftArithmKind arithm,
3396 JType op_jty) {
3397 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3398
3399 if (op_jty == kInt) {
3400 rhs = irb_.CreateAnd(rhs, 0x1f);
3401 } else {
3402 llvm::Value* masked_rhs = irb_.CreateAnd(rhs, 0x3f);
3403 rhs = irb_.CreateZExt(masked_rhs, irb_.getJLongTy());
3404 }
3405
3406 switch (arithm) {
3407 case kIntArithm_Shl:
3408 return irb_.CreateShl(lhs, rhs);
3409
3410 case kIntArithm_Shr:
3411 return irb_.CreateAShr(lhs, rhs);
3412
3413 case kIntArithm_UShr:
3414 return irb_.CreateLShr(lhs, rhs);
3415
3416 default:
3417 LOG(FATAL) << "Unknown integer shift arithmetic kind: " << arithm;
3418 return NULL;
3419 }
3420}
3421
3422
Logan Chien70f94b42011-12-27 17:49:11 +08003423void MethodCompiler::EmitInsn_RSubImmediate(uint32_t dex_pc,
3424 Instruction const* insn) {
Logan Chien65c62d42011-12-27 18:14:18 +08003425
Elliott Hughesadb8c672012-03-06 16:49:32 -08003426 DecodedInstruction dec_insn(insn);
Logan Chien65c62d42011-12-27 18:14:18 +08003427
Elliott Hughesadb8c672012-03-06 16:49:32 -08003428 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
3429 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC);
Logan Chien65c62d42011-12-27 18:14:18 +08003430 llvm::Value* result_value = irb_.CreateSub(imm_value, src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003431 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chien65c62d42011-12-27 18:14:18 +08003432
Logan Chien70f94b42011-12-27 17:49:11 +08003433 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3434}
3435
3436
3437void MethodCompiler::EmitInsn_FPArithm(uint32_t dex_pc,
3438 Instruction const* insn,
3439 FPArithmKind arithm,
3440 JType op_jty,
3441 bool is_2addr) {
Logan Chien76e1c792011-12-27 18:15:01 +08003442
Elliott Hughesadb8c672012-03-06 16:49:32 -08003443 DecodedInstruction dec_insn(insn);
Logan Chien76e1c792011-12-27 18:15:01 +08003444
3445 DCHECK(op_jty == kFloat || op_jty == kDouble) << op_jty;
3446
3447 llvm::Value* src1_value;
3448 llvm::Value* src2_value;
3449
3450 if (is_2addr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003451 src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate);
3452 src2_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chien76e1c792011-12-27 18:15:01 +08003453 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003454 src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
3455 src2_value = EmitLoadDalvikReg(dec_insn.vC, op_jty, kAccurate);
Logan Chien76e1c792011-12-27 18:15:01 +08003456 }
3457
3458 llvm::Value* result_value =
3459 EmitFPArithmResultComputation(dex_pc, src1_value, src2_value, arithm);
3460
Elliott Hughesadb8c672012-03-06 16:49:32 -08003461 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chien76e1c792011-12-27 18:15:01 +08003462
Logan Chien70f94b42011-12-27 17:49:11 +08003463 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3464}
3465
3466
Logan Chien76e1c792011-12-27 18:15:01 +08003467llvm::Value*
3468MethodCompiler::EmitFPArithmResultComputation(uint32_t dex_pc,
3469 llvm::Value *lhs,
3470 llvm::Value *rhs,
3471 FPArithmKind arithm) {
3472 switch (arithm) {
3473 case kFPArithm_Add:
3474 return irb_.CreateFAdd(lhs, rhs);
3475
3476 case kFPArithm_Sub:
3477 return irb_.CreateFSub(lhs, rhs);
3478
3479 case kFPArithm_Mul:
3480 return irb_.CreateFMul(lhs, rhs);
3481
3482 case kFPArithm_Div:
3483 return irb_.CreateFDiv(lhs, rhs);
3484
3485 case kFPArithm_Rem:
3486 return irb_.CreateFRem(lhs, rhs);
3487
3488 default:
3489 LOG(FATAL) << "Unknown floating-point arithmetic kind: " << arithm;
3490 return NULL;
3491 }
3492}
3493
3494
Logan Chienc3f7d962011-12-27 18:13:18 +08003495void MethodCompiler::EmitGuard_DivZeroException(uint32_t dex_pc,
3496 llvm::Value* denominator,
3497 JType op_jty) {
3498 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3499
3500 llvm::Constant* zero = irb_.getJZero(op_jty);
3501
3502 llvm::Value* equal_zero = irb_.CreateICmpEQ(denominator, zero);
3503
3504 llvm::BasicBlock* block_exception = CreateBasicBlockWithDexPC(dex_pc, "div0");
3505
3506 llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont");
3507
3508 irb_.CreateCondBr(equal_zero, block_exception, block_continue);
3509
3510 irb_.SetInsertPoint(block_exception);
TDYa127c8dc1012012-04-19 07:03:33 -07003511 EmitUpdateDexPC(dex_pc);
Logan Chienc3f7d962011-12-27 18:13:18 +08003512 irb_.CreateCall(irb_.GetRuntime(ThrowDivZeroException));
3513 EmitBranchExceptionLandingPad(dex_pc);
3514
3515 irb_.SetInsertPoint(block_continue);
3516}
3517
3518
Logan Chien61bb6142012-02-03 15:34:53 +08003519void MethodCompiler::EmitGuard_NullPointerException(uint32_t dex_pc,
3520 llvm::Value* object) {
3521 llvm::Value* equal_null = irb_.CreateICmpEQ(object, irb_.getJNull());
3522
3523 llvm::BasicBlock* block_exception =
3524 CreateBasicBlockWithDexPC(dex_pc, "nullp");
3525
3526 llvm::BasicBlock* block_continue =
3527 CreateBasicBlockWithDexPC(dex_pc, "cont");
3528
3529 irb_.CreateCondBr(equal_null, block_exception, block_continue);
3530
3531 irb_.SetInsertPoint(block_exception);
TDYa127c8dc1012012-04-19 07:03:33 -07003532 EmitUpdateDexPC(dex_pc);
TDYa1273f9137d2012-04-08 15:59:19 -07003533 irb_.CreateCall(irb_.GetRuntime(ThrowNullPointerException), irb_.getInt32(dex_pc));
Logan Chien61bb6142012-02-03 15:34:53 +08003534 EmitBranchExceptionLandingPad(dex_pc);
3535
3536 irb_.SetInsertPoint(block_continue);
3537}
3538
3539
Logan Chienbb4d12a2012-02-17 14:10:01 +08003540llvm::Value* MethodCompiler::EmitLoadDexCacheAddr(MemberOffset offset) {
3541 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
3542
TDYa127ee1f59b2012-04-25 00:56:40 -07003543 return irb_.LoadFromObjectOffset(method_object_addr,
3544 offset.Int32Value(),
3545 irb_.getJObjectTy());
Logan Chienbb4d12a2012-02-17 14:10:01 +08003546}
3547
3548
Logan Chienbb4d12a2012-02-17 14:10:01 +08003549llvm::Value* MethodCompiler::
3550EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx) {
3551 llvm::Value* static_storage_dex_cache_addr =
3552 EmitLoadDexCacheAddr(Method::DexCacheInitializedStaticStorageOffset());
3553
3554 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
3555
3556 return EmitArrayGEP(static_storage_dex_cache_addr, type_idx_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08003557 irb_.getJObjectTy(), kObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003558}
3559
3560
3561llvm::Value* MethodCompiler::
3562EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx) {
3563 llvm::Value* resolved_type_dex_cache_addr =
3564 EmitLoadDexCacheAddr(Method::DexCacheResolvedTypesOffset());
3565
3566 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
3567
3568 return EmitArrayGEP(resolved_type_dex_cache_addr, type_idx_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08003569 irb_.getJObjectTy(), kObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003570}
3571
3572
3573llvm::Value* MethodCompiler::
Logan Chien61c65dc2012-02-29 03:22:30 +08003574EmitLoadDexCacheResolvedMethodFieldAddr(uint32_t method_idx) {
3575 llvm::Value* resolved_method_dex_cache_addr =
3576 EmitLoadDexCacheAddr(Method::DexCacheResolvedMethodsOffset());
3577
3578 llvm::Value* method_idx_value = irb_.getPtrEquivInt(method_idx);
3579
3580 return EmitArrayGEP(resolved_method_dex_cache_addr, method_idx_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08003581 irb_.getJObjectTy(), kObject);
Logan Chien61c65dc2012-02-29 03:22:30 +08003582}
3583
3584
3585llvm::Value* MethodCompiler::
Logan Chienbb4d12a2012-02-17 14:10:01 +08003586EmitLoadDexCacheStringFieldAddr(uint32_t string_idx) {
3587 llvm::Value* string_dex_cache_addr =
3588 EmitLoadDexCacheAddr(Method::DexCacheStringsOffset());
3589
3590 llvm::Value* string_idx_value = irb_.getPtrEquivInt(string_idx);
3591
3592 return EmitArrayGEP(string_dex_cache_addr, string_idx_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08003593 irb_.getJObjectTy(), kObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003594}
3595
3596
Logan Chien83426162011-12-09 09:29:50 +08003597CompiledMethod *MethodCompiler::Compile() {
Logan Chien0b827102011-12-20 19:46:14 +08003598 // Code generation
3599 CreateFunction();
3600
3601 EmitPrologue();
3602 EmitInstructions();
Logan Chienc670a8d2011-12-20 21:25:56 +08003603 EmitPrologueLastBranch();
Logan Chien0b827102011-12-20 19:46:14 +08003604
Logan Chiend6c239a2011-12-23 15:11:45 +08003605 // Verify the generated bitcode
TDYa127853cd092012-04-21 22:15:31 -07003606 VERIFY_LLVM_FUNCTION(*func_);
Logan Chiend6c239a2011-12-23 15:11:45 +08003607
Logan Chien8b977d32012-02-21 19:14:55 +08003608 // Add the memory usage approximation of the compilation unit
3609 cunit_->AddMemUsageApproximation(code_item_->insns_size_in_code_units_ * 900);
3610 // NOTE: From statistic, the bitcode size is 4.5 times bigger than the
3611 // Dex file. Besides, we have to convert the code unit into bytes.
3612 // Thus, we got our magic number 9.
3613
Logan Chien110bcba2012-04-16 19:11:28 +08003614 CompiledMethod* compiled_method =
3615 new CompiledMethod(cunit_->GetInstructionSet(),
3616 cunit_->GetElfIndex(),
3617 elf_func_idx_);
3618
3619 cunit_->RegisterCompiledMethod(func_, compiled_method);
3620
3621 return compiled_method;
Logan Chien0b827102011-12-20 19:46:14 +08003622}
3623
3624
3625llvm::Value* MethodCompiler::EmitLoadMethodObjectAddr() {
3626 return func_->arg_begin();
Shih-wei Liaod1fec812012-02-13 09:51:10 -08003627}
Logan Chien83426162011-12-09 09:29:50 +08003628
3629
Logan Chien5bcc04e2012-01-30 14:15:12 +08003630void MethodCompiler::EmitBranchExceptionLandingPad(uint32_t dex_pc) {
3631 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
3632 irb_.CreateBr(lpad);
3633 } else {
3634 irb_.CreateBr(GetUnwindBasicBlock());
3635 }
3636}
3637
3638
3639void MethodCompiler::EmitGuard_ExceptionLandingPad(uint32_t dex_pc) {
3640 llvm::Value* exception_pending =
3641 irb_.CreateCall(irb_.GetRuntime(IsExceptionPending));
3642
3643 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
3644
3645 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
3646 irb_.CreateCondBr(exception_pending, lpad, block_cont);
3647 } else {
3648 irb_.CreateCondBr(exception_pending, GetUnwindBasicBlock(), block_cont);
3649 }
3650
3651 irb_.SetInsertPoint(block_cont);
3652}
3653
3654
Logan Chien924072f2012-01-30 15:07:24 +08003655void MethodCompiler::EmitGuard_GarbageCollectionSuspend(uint32_t dex_pc) {
3656 llvm::Value* runtime_func = irb_.GetRuntime(TestSuspend);
TDYa127853cd092012-04-21 22:15:31 -07003657
3658 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
3659
TDYa127c8dc1012012-04-19 07:03:33 -07003660 EmitUpdateDexPC(dex_pc);
TDYa127853cd092012-04-21 22:15:31 -07003661
3662 irb_.CreateCall(runtime_func, thread_object_addr);
Logan Chien924072f2012-01-30 15:07:24 +08003663
3664 EmitGuard_ExceptionLandingPad(dex_pc);
3665}
3666
3667
Logan Chiend6c239a2011-12-23 15:11:45 +08003668llvm::BasicBlock* MethodCompiler::
3669CreateBasicBlockWithDexPC(uint32_t dex_pc, char const* postfix) {
3670 std::string name;
3671
3672 if (postfix) {
TDYa12799489132012-04-29 01:27:58 -07003673 StringAppendF(&name, "B%04x.%s", dex_pc, postfix);
Logan Chiend6c239a2011-12-23 15:11:45 +08003674 } else {
TDYa12799489132012-04-29 01:27:58 -07003675 StringAppendF(&name, "B%04x", dex_pc);
Logan Chiend6c239a2011-12-23 15:11:45 +08003676 }
3677
3678 return llvm::BasicBlock::Create(*context_, name, func_);
3679}
3680
3681
3682llvm::BasicBlock* MethodCompiler::GetBasicBlock(uint32_t dex_pc) {
3683 DCHECK(dex_pc < code_item_->insns_size_in_code_units_);
3684
3685 llvm::BasicBlock* basic_block = basic_blocks_[dex_pc];
3686
3687 if (!basic_block) {
3688 basic_block = CreateBasicBlockWithDexPC(dex_pc);
3689 basic_blocks_[dex_pc] = basic_block;
3690 }
3691
3692 return basic_block;
3693}
3694
3695
3696llvm::BasicBlock*
3697MethodCompiler::GetNextBasicBlock(uint32_t dex_pc) {
3698 Instruction const* insn = Instruction::At(code_item_->insns_ + dex_pc);
3699 return GetBasicBlock(dex_pc + insn->SizeInCodeUnits());
3700}
3701
3702
Logan Chien5bcc04e2012-01-30 14:15:12 +08003703int32_t MethodCompiler::GetTryItemOffset(uint32_t dex_pc) {
3704 // TODO: Since we are emitting the dex instructions in ascending order
3705 // w.r.t. address, we can cache the lastest try item offset so that we
3706 // don't have to do binary search for every query.
3707
3708 int32_t min = 0;
3709 int32_t max = code_item_->tries_size_ - 1;
3710
3711 while (min <= max) {
3712 int32_t mid = min + (max - min) / 2;
3713
3714 DexFile::TryItem const* ti = DexFile::GetTryItems(*code_item_, mid);
3715 uint32_t start = ti->start_addr_;
3716 uint32_t end = start + ti->insn_count_;
3717
3718 if (dex_pc < start) {
3719 max = mid - 1;
3720 } else if (dex_pc >= end) {
3721 min = mid + 1;
3722 } else {
3723 return mid; // found
3724 }
3725 }
3726
3727 return -1; // not found
3728}
3729
3730
3731llvm::BasicBlock* MethodCompiler::GetLandingPadBasicBlock(uint32_t dex_pc) {
3732 // Find the try item for this address in this method
3733 int32_t ti_offset = GetTryItemOffset(dex_pc);
3734
3735 if (ti_offset == -1) {
3736 return NULL; // No landing pad is available for this address.
3737 }
3738
3739 // Check for the existing landing pad basic block
3740 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
3741 llvm::BasicBlock* block_lpad = basic_block_landing_pads_[ti_offset];
3742
3743 if (block_lpad) {
3744 // We have generated landing pad for this try item already. Return the
3745 // same basic block.
3746 return block_lpad;
3747 }
3748
3749 // Get try item from code item
3750 DexFile::TryItem const* ti = DexFile::GetTryItems(*code_item_, ti_offset);
3751
3752 // Create landing pad basic block
3753 block_lpad = llvm::BasicBlock::Create(*context_,
3754 StringPrintf("lpad%d", ti_offset),
3755 func_);
3756
3757 // Change IRBuilder insert point
3758 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3759 irb_.SetInsertPoint(block_lpad);
3760
3761 // Find catch block with matching type
3762 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
3763
Logan Chien736df022012-04-27 16:25:57 +08003764 llvm::Value* ti_offset_value = irb_.getInt32(ti_offset);
Logan Chien5bcc04e2012-01-30 14:15:12 +08003765
3766 llvm::Value* catch_handler_index_value =
3767 irb_.CreateCall2(irb_.GetRuntime(FindCatchBlock),
Logan Chien736df022012-04-27 16:25:57 +08003768 method_object_addr, ti_offset_value);
Logan Chien5bcc04e2012-01-30 14:15:12 +08003769
3770 // Switch instruction (Go to unwind basic block by default)
3771 llvm::SwitchInst* sw =
3772 irb_.CreateSwitch(catch_handler_index_value, GetUnwindBasicBlock());
3773
3774 // Cases with matched catch block
3775 CatchHandlerIterator iter(*code_item_, ti->start_addr_);
3776
3777 for (uint32_t c = 0; iter.HasNext(); iter.Next(), ++c) {
3778 sw->addCase(irb_.getInt32(c), GetBasicBlock(iter.GetHandlerAddress()));
3779 }
3780
3781 // Restore the orignal insert point for IRBuilder
3782 irb_.restoreIP(irb_ip_original);
3783
3784 // Cache this landing pad
3785 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
3786 basic_block_landing_pads_[ti_offset] = block_lpad;
3787
3788 return block_lpad;
3789}
3790
3791
3792llvm::BasicBlock* MethodCompiler::GetUnwindBasicBlock() {
3793 // Check the existing unwinding baisc block block
3794 if (basic_block_unwind_ != NULL) {
3795 return basic_block_unwind_;
3796 }
3797
3798 // Create new basic block for unwinding
3799 basic_block_unwind_ =
3800 llvm::BasicBlock::Create(*context_, "exception_unwind", func_);
3801
3802 // Change IRBuilder insert point
3803 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3804 irb_.SetInsertPoint(basic_block_unwind_);
3805
Logan Chien8dfcbea2012-02-17 18:50:32 +08003806 // Pop the shadow frame
3807 EmitPopShadowFrame();
3808
Logan Chien5bcc04e2012-01-30 14:15:12 +08003809 // Emit the code to return default value (zero) for the given return type.
Logan Chiendd361c92012-04-10 23:40:37 +08003810 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
Logan Chien5bcc04e2012-01-30 14:15:12 +08003811 if (ret_shorty == 'V') {
3812 irb_.CreateRetVoid();
3813 } else {
3814 irb_.CreateRet(irb_.getJZero(ret_shorty));
3815 }
3816
3817 // Restore the orignal insert point for IRBuilder
3818 irb_.restoreIP(irb_ip_original);
3819
3820 return basic_block_unwind_;
3821}
3822
3823
Logan Chienc670a8d2011-12-20 21:25:56 +08003824llvm::Value* MethodCompiler::AllocDalvikLocalVarReg(RegCategory cat,
3825 uint32_t reg_idx) {
3826
3827 // Save current IR builder insert point
3828 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3829
3830 // Alloca
3831 llvm::Value* reg_addr = NULL;
3832
3833 switch (cat) {
3834 case kRegCat1nr:
3835 irb_.SetInsertPoint(basic_block_reg_alloca_);
3836 reg_addr = irb_.CreateAlloca(irb_.getJIntTy(), 0,
3837 StringPrintf("r%u", reg_idx));
Logan Chienc670a8d2011-12-20 21:25:56 +08003838 break;
3839
3840 case kRegCat2:
3841 irb_.SetInsertPoint(basic_block_reg_alloca_);
3842 reg_addr = irb_.CreateAlloca(irb_.getJLongTy(), 0,
3843 StringPrintf("w%u", reg_idx));
Logan Chienc670a8d2011-12-20 21:25:56 +08003844 break;
3845
3846 case kRegObject:
Logan Chien8dfcbea2012-02-17 18:50:32 +08003847 {
3848 irb_.SetInsertPoint(basic_block_shadow_frame_alloca_);
Logan Chienc670a8d2011-12-20 21:25:56 +08003849
Logan Chien8dfcbea2012-02-17 18:50:32 +08003850 llvm::Value* gep_index[] = {
3851 irb_.getInt32(0), // No pointer displacement
3852 irb_.getInt32(1), // SIRT
3853 irb_.getInt32(reg_idx) // Pointer field
3854 };
3855
3856 reg_addr = irb_.CreateGEP(shadow_frame_, gep_index,
3857 StringPrintf("p%u", reg_idx));
Logan Chien8dfcbea2012-02-17 18:50:32 +08003858 }
Logan Chienc670a8d2011-12-20 21:25:56 +08003859 break;
3860
3861 default:
3862 LOG(FATAL) << "Unknown register category for allocation: " << cat;
3863 }
3864
3865 // Restore IRBuilder insert point
3866 irb_.restoreIP(irb_ip_original);
3867
3868 DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL));
3869 return reg_addr;
3870}
3871
3872
3873llvm::Value* MethodCompiler::AllocDalvikRetValReg(RegCategory cat) {
3874 // Save current IR builder insert point
3875 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3876
3877 // Alloca
3878 llvm::Value* reg_addr = NULL;
3879
3880 switch (cat) {
3881 case kRegCat1nr:
3882 irb_.SetInsertPoint(basic_block_reg_alloca_);
3883 reg_addr = irb_.CreateAlloca(irb_.getJIntTy(), 0, "r_res");
3884 break;
3885
3886 case kRegCat2:
3887 irb_.SetInsertPoint(basic_block_reg_alloca_);
3888 reg_addr = irb_.CreateAlloca(irb_.getJLongTy(), 0, "w_res");
3889 break;
3890
3891 case kRegObject:
3892 irb_.SetInsertPoint(basic_block_reg_alloca_);
3893 reg_addr = irb_.CreateAlloca(irb_.getJObjectTy(), 0, "p_res");
3894 break;
3895
3896 default:
3897 LOG(FATAL) << "Unknown register category for allocation: " << cat;
3898 }
3899
3900 // Restore IRBuilder insert point
3901 irb_.restoreIP(irb_ip_original);
3902
3903 DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL));
3904 return reg_addr;
3905}
3906
3907
Logan Chien8dfcbea2012-02-17 18:50:32 +08003908void MethodCompiler::EmitPopShadowFrame() {
3909 irb_.CreateCall(irb_.GetRuntime(PopShadowFrame));
3910}
3911
3912
TDYa127c8dc1012012-04-19 07:03:33 -07003913void MethodCompiler::EmitUpdateDexPC(uint32_t dex_pc) {
3914 irb_.StoreToObjectOffset(shadow_frame_,
3915 ShadowFrame::DexPCOffset(),
3916 irb_.getInt32(dex_pc));
Logan Chien8dfcbea2012-02-17 18:50:32 +08003917}
3918
3919
Logan Chien83426162011-12-09 09:29:50 +08003920} // namespace compiler_llvm
3921} // namespace art