blob: c8976cdeb87ad1a257ae95ec39e33686bced747b [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
Logan Chien70f94b42011-12-27 17:49:11 +08001539 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1540}
1541
1542
1543void MethodCompiler::EmitInsn_MonitorExit(uint32_t dex_pc,
1544 Instruction const* insn) {
Logan Chien9e0dbe42012-01-13 12:11:37 +08001545
Elliott Hughesadb8c672012-03-06 16:49:32 -08001546 DecodedInstruction dec_insn(insn);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001547
1548 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001549 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001550
1551 EmitGuard_NullPointerException(dex_pc, object_addr);
1552
TDYa127c8dc1012012-04-19 07:03:33 -07001553 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001554
TDYa127706e9b62012-04-19 12:24:26 -07001555 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1556
1557 irb_.CreateCall2(irb_.GetRuntime(UnlockObject), object_addr, thread_object_addr);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001558 EmitGuard_ExceptionLandingPad(dex_pc);
1559
Logan Chien70f94b42011-12-27 17:49:11 +08001560 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1561}
1562
1563
1564void MethodCompiler::EmitInsn_CheckCast(uint32_t dex_pc,
1565 Instruction const* insn) {
Logan Chienfc880952012-01-15 23:53:10 +08001566
Elliott Hughesadb8c672012-03-06 16:49:32 -08001567 DecodedInstruction dec_insn(insn);
Logan Chienfc880952012-01-15 23:53:10 +08001568
1569 llvm::BasicBlock* block_test_class =
1570 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1571
1572 llvm::BasicBlock* block_test_sub_class =
1573 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
1574
1575 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001576 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chienfc880952012-01-15 23:53:10 +08001577
1578 // Test: Is the reference equal to null? Act as no-op when it is null.
1579 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
1580
1581 irb_.CreateCondBr(equal_null,
1582 GetNextBasicBlock(dex_pc),
1583 block_test_class);
1584
1585 // Test: Is the object instantiated from the given class?
1586 irb_.SetInsertPoint(block_test_class);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001587 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vB);
Logan Chienfc880952012-01-15 23:53:10 +08001588 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1589
1590 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
1591
1592 llvm::Value* object_type_field_addr =
1593 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
1594
1595 llvm::Value* object_type_object_addr =
1596 irb_.CreateLoad(object_type_field_addr);
1597
1598 llvm::Value* equal_class =
1599 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
1600
1601 irb_.CreateCondBr(equal_class,
1602 GetNextBasicBlock(dex_pc),
1603 block_test_sub_class);
1604
1605 // Test: Is the object instantiated from the subclass of the given class?
1606 irb_.SetInsertPoint(block_test_sub_class);
1607
TDYa127c8dc1012012-04-19 07:03:33 -07001608 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001609
Logan Chienfc880952012-01-15 23:53:10 +08001610 irb_.CreateCall2(irb_.GetRuntime(CheckCast),
1611 type_object_addr, object_type_object_addr);
1612
1613 EmitGuard_ExceptionLandingPad(dex_pc);
1614
Logan Chien70f94b42011-12-27 17:49:11 +08001615 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1616}
1617
1618
1619void MethodCompiler::EmitInsn_InstanceOf(uint32_t dex_pc,
1620 Instruction const* insn) {
Logan Chien68725e22012-01-15 22:25:34 +08001621
Elliott Hughesadb8c672012-03-06 16:49:32 -08001622 DecodedInstruction dec_insn(insn);
Logan Chien68725e22012-01-15 22:25:34 +08001623
1624 llvm::Constant* zero = irb_.getJInt(0);
1625 llvm::Constant* one = irb_.getJInt(1);
1626
1627 llvm::BasicBlock* block_nullp = CreateBasicBlockWithDexPC(dex_pc, "nullp");
1628
1629 llvm::BasicBlock* block_test_class =
1630 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1631
1632 llvm::BasicBlock* block_class_equals =
1633 CreateBasicBlockWithDexPC(dex_pc, "class_eq");
1634
1635 llvm::BasicBlock* block_test_sub_class =
1636 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
1637
1638 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001639 EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
Logan Chien68725e22012-01-15 22:25:34 +08001640
1641 // Overview of the following code :
1642 // We check for null, if so, then false, otherwise check for class == . If so
1643 // then true, otherwise do callout slowpath.
1644 //
1645 // Test: Is the reference equal to null? Set 0 when it is null.
1646 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
1647
1648 irb_.CreateCondBr(equal_null, block_nullp, block_test_class);
1649
1650 irb_.SetInsertPoint(block_nullp);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001651 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, zero);
Logan Chien68725e22012-01-15 22:25:34 +08001652 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1653
1654 // Test: Is the object instantiated from the given class?
1655 irb_.SetInsertPoint(block_test_class);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001656 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vC);
Logan Chien68725e22012-01-15 22:25:34 +08001657 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1658
1659 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
1660
1661 llvm::Value* object_type_field_addr =
1662 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
1663
1664 llvm::Value* object_type_object_addr =
1665 irb_.CreateLoad(object_type_field_addr);
1666
1667 llvm::Value* equal_class =
1668 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
1669
1670 irb_.CreateCondBr(equal_class, block_class_equals, block_test_sub_class);
1671
1672 irb_.SetInsertPoint(block_class_equals);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001673 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, one);
Logan Chien68725e22012-01-15 22:25:34 +08001674 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1675
1676 // Test: Is the object instantiated from the subclass of the given class?
1677 irb_.SetInsertPoint(block_test_sub_class);
1678
1679 llvm::Value* result =
1680 irb_.CreateCall2(irb_.GetRuntime(IsAssignable),
1681 type_object_addr, object_type_object_addr);
1682
Elliott Hughesadb8c672012-03-06 16:49:32 -08001683 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result);
Logan Chien68725e22012-01-15 22:25:34 +08001684
Logan Chien70f94b42011-12-27 17:49:11 +08001685 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1686}
1687
1688
Logan Chien61bb6142012-02-03 15:34:53 +08001689llvm::Value* MethodCompiler::EmitLoadArrayLength(llvm::Value* array) {
Logan Chien61bb6142012-02-03 15:34:53 +08001690 // Load array length
TDYa127ee1f59b2012-04-25 00:56:40 -07001691 return irb_.LoadFromObjectOffset(array,
1692 Array::LengthOffset().Int32Value(),
1693 irb_.getJIntTy());
Logan Chien61bb6142012-02-03 15:34:53 +08001694}
1695
1696
Logan Chien70f94b42011-12-27 17:49:11 +08001697void MethodCompiler::EmitInsn_ArrayLength(uint32_t dex_pc,
1698 Instruction const* insn) {
Logan Chien61bb6142012-02-03 15:34:53 +08001699
Elliott Hughesadb8c672012-03-06 16:49:32 -08001700 DecodedInstruction dec_insn(insn);
Logan Chien61bb6142012-02-03 15:34:53 +08001701
1702 // Get the array object address
Elliott Hughesadb8c672012-03-06 16:49:32 -08001703 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
Logan Chien61bb6142012-02-03 15:34:53 +08001704 EmitGuard_NullPointerException(dex_pc, array_addr);
1705
1706 // Get the array length and store it to the register
1707 llvm::Value* array_len = EmitLoadArrayLength(array_addr);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001708 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, array_len);
Logan Chien61bb6142012-02-03 15:34:53 +08001709
Logan Chien70f94b42011-12-27 17:49:11 +08001710 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1711}
1712
1713
1714void MethodCompiler::EmitInsn_NewInstance(uint32_t dex_pc,
1715 Instruction const* insn) {
Logan Chien032bdad2012-01-16 09:59:23 +08001716
Elliott Hughesadb8c672012-03-06 16:49:32 -08001717 DecodedInstruction dec_insn(insn);
Logan Chien032bdad2012-01-16 09:59:23 +08001718
1719 llvm::Function* runtime_func;
Logan Chien1a032b12012-04-11 11:43:04 +08001720 if (compiler_->CanAccessInstantiableTypeWithoutChecks(
1721 method_idx_, dex_cache_, *dex_file_, dec_insn.vB)) {
Logan Chien032bdad2012-01-16 09:59:23 +08001722 runtime_func = irb_.GetRuntime(AllocObject);
1723 } else {
1724 runtime_func = irb_.GetRuntime(AllocObjectWithAccessCheck);
1725 }
1726
Elliott Hughesadb8c672012-03-06 16:49:32 -08001727 llvm::Constant* type_index_value = irb_.getInt32(dec_insn.vB);
Logan Chien032bdad2012-01-16 09:59:23 +08001728
1729 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1730
TDYa127da83d972012-04-18 00:21:49 -07001731 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1732
TDYa127c8dc1012012-04-19 07:03:33 -07001733 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001734
Logan Chien032bdad2012-01-16 09:59:23 +08001735 llvm::Value* object_addr =
TDYa127da83d972012-04-18 00:21:49 -07001736 irb_.CreateCall3(runtime_func, type_index_value, method_object_addr, thread_object_addr);
Logan Chien032bdad2012-01-16 09:59:23 +08001737
1738 EmitGuard_ExceptionLandingPad(dex_pc);
1739
Elliott Hughesadb8c672012-03-06 16:49:32 -08001740 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, object_addr);
Logan Chien032bdad2012-01-16 09:59:23 +08001741
Logan Chien70f94b42011-12-27 17:49:11 +08001742 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1743}
1744
1745
Logan Chiena2cc6a32012-01-16 10:38:41 +08001746llvm::Value* MethodCompiler::EmitAllocNewArray(uint32_t dex_pc,
1747 int32_t length,
1748 uint32_t type_idx,
1749 bool is_filled_new_array) {
1750 llvm::Function* runtime_func;
1751
1752 bool skip_access_check =
1753 compiler_->CanAccessTypeWithoutChecks(method_idx_, dex_cache_,
1754 *dex_file_, type_idx);
1755
TDYa127a849cb62012-04-01 05:59:34 -07001756 llvm::Value* array_length_value;
1757
Logan Chiena2cc6a32012-01-16 10:38:41 +08001758 if (is_filled_new_array) {
1759 runtime_func = skip_access_check ?
1760 irb_.GetRuntime(CheckAndAllocArray) :
1761 irb_.GetRuntime(CheckAndAllocArrayWithAccessCheck);
TDYa127a849cb62012-04-01 05:59:34 -07001762 array_length_value = irb_.getInt32(length);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001763 } else {
1764 runtime_func = skip_access_check ?
1765 irb_.GetRuntime(AllocArray) :
1766 irb_.GetRuntime(AllocArrayWithAccessCheck);
TDYa127a849cb62012-04-01 05:59:34 -07001767 array_length_value = EmitLoadDalvikReg(length, kInt, kAccurate);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001768 }
1769
1770 llvm::Constant* type_index_value = irb_.getInt32(type_idx);
1771
1772 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1773
TDYa127da83d972012-04-18 00:21:49 -07001774 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1775
TDYa127c8dc1012012-04-19 07:03:33 -07001776 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001777
Logan Chiena2cc6a32012-01-16 10:38:41 +08001778 llvm::Value* object_addr =
TDYa127da83d972012-04-18 00:21:49 -07001779 irb_.CreateCall4(runtime_func, type_index_value, method_object_addr,
1780 array_length_value, thread_object_addr);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001781
1782 EmitGuard_ExceptionLandingPad(dex_pc);
1783
1784 return object_addr;
1785}
1786
1787
Logan Chien70f94b42011-12-27 17:49:11 +08001788void MethodCompiler::EmitInsn_NewArray(uint32_t dex_pc,
1789 Instruction const* insn) {
Logan Chiena2cc6a32012-01-16 10:38:41 +08001790
Elliott Hughesadb8c672012-03-06 16:49:32 -08001791 DecodedInstruction dec_insn(insn);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001792
1793 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001794 EmitAllocNewArray(dex_pc, dec_insn.vB, dec_insn.vC, false);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001795
Elliott Hughesadb8c672012-03-06 16:49:32 -08001796 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, object_addr);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001797
Logan Chien70f94b42011-12-27 17:49:11 +08001798 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1799}
1800
1801
1802void MethodCompiler::EmitInsn_FilledNewArray(uint32_t dex_pc,
1803 Instruction const* insn,
1804 bool is_range) {
Logan Chiena85fb2f2012-01-16 12:52:56 +08001805
Elliott Hughesadb8c672012-03-06 16:49:32 -08001806 DecodedInstruction dec_insn(insn);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001807
1808 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001809 EmitAllocNewArray(dex_pc, dec_insn.vA, dec_insn.vB, true);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001810
Elliott Hughesadb8c672012-03-06 16:49:32 -08001811 if (dec_insn.vA > 0) {
Logan Chiendd361c92012-04-10 23:40:37 +08001812 // Resolve the element type
TDYa127183cf262012-04-11 07:53:21 -07001813 Class* klass = dex_cache_->GetResolvedType(dec_insn.vB)->GetComponentType();
Logan Chiendd361c92012-04-10 23:40:37 +08001814 // TODO: Avoid the usage of the dex_cache_. Try to figure out a better
1815 // way to distinguish [I and [L.
Logan Chiena85fb2f2012-01-16 12:52:56 +08001816 CHECK_NE(klass, static_cast<Class*>(NULL));
Logan Chiena85fb2f2012-01-16 12:52:56 +08001817
Logan Chiendd361c92012-04-10 23:40:37 +08001818 uint32_t alignment;
1819 llvm::Constant* elem_size;
1820 llvm::PointerType* field_type;
Logan Chiena85fb2f2012-01-16 12:52:56 +08001821
Logan Chiendd361c92012-04-10 23:40:37 +08001822 // NOTE: Currently filled-new-array only supports 'L', '[', and 'I'
1823 // as the element, thus we are only checking 2 cases: primitive int and
1824 // non-primitive type.
Logan Chiena85fb2f2012-01-16 12:52:56 +08001825 if (klass->IsPrimitiveInt()) {
Logan Chiendd361c92012-04-10 23:40:37 +08001826 alignment = sizeof(int32_t);
1827 elem_size = irb_.getPtrEquivInt(sizeof(int32_t));
Logan Chiena85fb2f2012-01-16 12:52:56 +08001828 field_type = irb_.getJIntTy()->getPointerTo();
1829 } else {
1830 CHECK(!klass->IsPrimitive());
Logan Chiendd361c92012-04-10 23:40:37 +08001831 alignment = irb_.getSizeOfPtrEquivInt();
1832 elem_size = irb_.getSizeOfPtrEquivIntValue();
Logan Chiena85fb2f2012-01-16 12:52:56 +08001833 field_type = irb_.getJObjectTy()->getPointerTo();
1834 }
1835
Logan Chiendd361c92012-04-10 23:40:37 +08001836 llvm::Value* data_field_offset =
1837 irb_.getPtrEquivInt(Array::DataOffset(alignment).Int32Value());
1838
1839 llvm::Value* data_field_addr =
1840 irb_.CreatePtrDisp(object_addr, data_field_offset, field_type);
1841
Logan Chiena85fb2f2012-01-16 12:52:56 +08001842 // TODO: Tune this code. Currently we are generating one instruction for
1843 // one element which may be very space consuming. Maybe changing to use
1844 // memcpy may help; however, since we can't guarantee that the alloca of
1845 // dalvik register are continuous, we can't perform such optimization yet.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001846 for (uint32_t i = 0; i < dec_insn.vA; ++i) {
Logan Chiena85fb2f2012-01-16 12:52:56 +08001847 int reg_index;
1848 if (is_range) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001849 reg_index = dec_insn.vC + i;
Logan Chiena85fb2f2012-01-16 12:52:56 +08001850 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001851 reg_index = dec_insn.arg[i];
Logan Chiena85fb2f2012-01-16 12:52:56 +08001852 }
1853
1854 llvm::Value* reg_value;
1855 if (klass->IsPrimitiveInt()) {
1856 reg_value = EmitLoadDalvikReg(reg_index, kInt, kAccurate);
1857 } else {
1858 reg_value = EmitLoadDalvikReg(reg_index, kObject, kAccurate);
1859 }
1860
1861 irb_.CreateStore(reg_value, data_field_addr);
1862
Logan Chiendd361c92012-04-10 23:40:37 +08001863 data_field_addr =
1864 irb_.CreatePtrDisp(data_field_addr, elem_size, field_type);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001865 }
1866 }
1867
1868 EmitStoreDalvikRetValReg(kObject, kAccurate, object_addr);
1869
Logan Chien70f94b42011-12-27 17:49:11 +08001870 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1871}
1872
1873
1874void MethodCompiler::EmitInsn_FillArrayData(uint32_t dex_pc,
1875 Instruction const* insn) {
Logan Chiene58b6582012-01-16 17:13:13 +08001876
Elliott Hughesadb8c672012-03-06 16:49:32 -08001877 DecodedInstruction dec_insn(insn);
Logan Chiene58b6582012-01-16 17:13:13 +08001878
1879 // Read the payload
Logan Chiene58b6582012-01-16 17:13:13 +08001880 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
Elliott Hughesadb8c672012-03-06 16:49:32 -08001881 static_cast<int32_t>(dec_insn.vB);
Logan Chiene58b6582012-01-16 17:13:13 +08001882
Logan Chien19c350a2012-05-01 19:21:32 +08001883 const Instruction::ArrayDataPayload* payload =
1884 reinterpret_cast<const Instruction::ArrayDataPayload*>(
1885 code_item_->insns_ + payload_offset);
Logan Chiene58b6582012-01-16 17:13:13 +08001886
Logan Chien86f50672012-04-24 13:08:45 +08001887 // Load array object
Elliott Hughesadb8c672012-03-06 16:49:32 -08001888 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiene58b6582012-01-16 17:13:13 +08001889
Logan Chien86f50672012-04-24 13:08:45 +08001890 if (payload->element_count == 0) {
1891 // When the number of the elements in the payload is zero, we don't have
1892 // to copy any numbers. However, we should check whether the array object
1893 // address is equal to null or not.
1894 EmitGuard_NullPointerException(dex_pc, array_addr);
1895 } else {
1896 // To save the code size, we are going to call the runtime function to
1897 // copy the content from DexFile.
Logan Chiene58b6582012-01-16 17:13:13 +08001898
Logan Chien86f50672012-04-24 13:08:45 +08001899 // NOTE: We will check for the NullPointerException in the runtime.
Logan Chiene58b6582012-01-16 17:13:13 +08001900
Logan Chien86f50672012-04-24 13:08:45 +08001901 llvm::Function* runtime_func = irb_.GetRuntime(FillArrayData);
Logan Chiene58b6582012-01-16 17:13:13 +08001902
Logan Chien86f50672012-04-24 13:08:45 +08001903 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
Logan Chiene58b6582012-01-16 17:13:13 +08001904
Logan Chien86f50672012-04-24 13:08:45 +08001905 EmitUpdateDexPC(dex_pc);
Logan Chiene58b6582012-01-16 17:13:13 +08001906
Logan Chien86f50672012-04-24 13:08:45 +08001907 irb_.CreateCall4(runtime_func,
1908 method_object_addr, irb_.getInt32(dex_pc),
1909 array_addr, irb_.getInt32(payload_offset));
Logan Chiene58b6582012-01-16 17:13:13 +08001910
Logan Chien86f50672012-04-24 13:08:45 +08001911 EmitGuard_ExceptionLandingPad(dex_pc);
Logan Chiene58b6582012-01-16 17:13:13 +08001912 }
1913
Logan Chien70f94b42011-12-27 17:49:11 +08001914 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1915}
1916
1917
1918void MethodCompiler::EmitInsn_UnconditionalBranch(uint32_t dex_pc,
1919 Instruction const* insn) {
Logan Chiena466c162011-12-27 17:55:46 +08001920
Elliott Hughesadb8c672012-03-06 16:49:32 -08001921 DecodedInstruction dec_insn(insn);
Logan Chiena466c162011-12-27 17:55:46 +08001922
Elliott Hughesadb8c672012-03-06 16:49:32 -08001923 int32_t branch_offset = dec_insn.vA;
Logan Chiena466c162011-12-27 17:55:46 +08001924
1925 if (branch_offset <= 0) {
1926 // Garbage collection safe-point on backward branch
1927 EmitGuard_GarbageCollectionSuspend(dex_pc);
1928 }
1929
1930 irb_.CreateBr(GetBasicBlock(dex_pc + branch_offset));
Logan Chien70f94b42011-12-27 17:49:11 +08001931}
1932
1933
1934void MethodCompiler::EmitInsn_PackedSwitch(uint32_t dex_pc,
1935 Instruction const* insn) {
Logan Chien7a89b6d2011-12-27 17:56:56 +08001936
Elliott Hughesadb8c672012-03-06 16:49:32 -08001937 DecodedInstruction dec_insn(insn);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001938
Logan Chien7a89b6d2011-12-27 17:56:56 +08001939 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
Elliott Hughesadb8c672012-03-06 16:49:32 -08001940 static_cast<int32_t>(dec_insn.vB);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001941
Logan Chien19c350a2012-05-01 19:21:32 +08001942 const Instruction::PackedSwitchPayload* payload =
1943 reinterpret_cast<const Instruction::PackedSwitchPayload*>(
1944 code_item_->insns_ + payload_offset);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001945
Elliott Hughesadb8c672012-03-06 16:49:32 -08001946 llvm::Value* value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001947
1948 llvm::SwitchInst* sw =
Logan Chien19c350a2012-05-01 19:21:32 +08001949 irb_.CreateSwitch(value, GetNextBasicBlock(dex_pc), payload->case_count);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001950
Logan Chien19c350a2012-05-01 19:21:32 +08001951 for (uint16_t i = 0; i < payload->case_count; ++i) {
1952 sw->addCase(irb_.getInt32(payload->first_key + i),
1953 GetBasicBlock(dex_pc + payload->targets[i]));
Logan Chien7a89b6d2011-12-27 17:56:56 +08001954 }
Logan Chien70f94b42011-12-27 17:49:11 +08001955}
1956
1957
1958void MethodCompiler::EmitInsn_SparseSwitch(uint32_t dex_pc,
1959 Instruction const* insn) {
Logan Chien7a89b6d2011-12-27 17:56:56 +08001960
Elliott Hughesadb8c672012-03-06 16:49:32 -08001961 DecodedInstruction dec_insn(insn);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001962
Logan Chien7a89b6d2011-12-27 17:56:56 +08001963 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
Elliott Hughesadb8c672012-03-06 16:49:32 -08001964 static_cast<int32_t>(dec_insn.vB);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001965
Logan Chien19c350a2012-05-01 19:21:32 +08001966 const Instruction::SparseSwitchPayload* payload =
1967 reinterpret_cast<const Instruction::SparseSwitchPayload*>(
1968 code_item_->insns_ + payload_offset);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001969
Logan Chien19c350a2012-05-01 19:21:32 +08001970 const int32_t* keys = payload->GetKeys();
1971 const int32_t* targets = payload->GetTargets();
Logan Chien7a89b6d2011-12-27 17:56:56 +08001972
Elliott Hughesadb8c672012-03-06 16:49:32 -08001973 llvm::Value* value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001974
1975 llvm::SwitchInst* sw =
Logan Chien19c350a2012-05-01 19:21:32 +08001976 irb_.CreateSwitch(value, GetNextBasicBlock(dex_pc), payload->case_count);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001977
Logan Chien19c350a2012-05-01 19:21:32 +08001978 for (size_t i = 0; i < payload->case_count; ++i) {
Logan Chien7a89b6d2011-12-27 17:56:56 +08001979 sw->addCase(irb_.getInt32(keys[i]), GetBasicBlock(dex_pc + targets[i]));
1980 }
Logan Chien70f94b42011-12-27 17:49:11 +08001981}
1982
1983
1984void MethodCompiler::EmitInsn_FPCompare(uint32_t dex_pc,
1985 Instruction const* insn,
1986 JType fp_jty,
1987 bool gt_bias) {
Logan Chien2c37e8e2011-12-27 17:58:46 +08001988
Elliott Hughesadb8c672012-03-06 16:49:32 -08001989 DecodedInstruction dec_insn(insn);
Logan Chien2c37e8e2011-12-27 17:58:46 +08001990
1991 DCHECK(fp_jty == kFloat || fp_jty == kDouble) << "JType: " << fp_jty;
1992
Elliott Hughesadb8c672012-03-06 16:49:32 -08001993 llvm::Value* src1_value = EmitLoadDalvikReg(dec_insn.vB, fp_jty, kAccurate);
1994 llvm::Value* src2_value = EmitLoadDalvikReg(dec_insn.vC, fp_jty, kAccurate);
Logan Chien2c37e8e2011-12-27 17:58:46 +08001995
1996 llvm::Value* cmp_eq = irb_.CreateFCmpOEQ(src1_value, src2_value);
1997 llvm::Value* cmp_lt;
1998
1999 if (gt_bias) {
2000 cmp_lt = irb_.CreateFCmpOLT(src1_value, src2_value);
2001 } else {
2002 cmp_lt = irb_.CreateFCmpULT(src1_value, src2_value);
2003 }
2004
2005 llvm::Value* result = EmitCompareResultSelection(cmp_eq, cmp_lt);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002006 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002007
Logan Chien70f94b42011-12-27 17:49:11 +08002008 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2009}
2010
2011
2012void MethodCompiler::EmitInsn_LongCompare(uint32_t dex_pc,
2013 Instruction const* insn) {
Logan Chien2c37e8e2011-12-27 17:58:46 +08002014
Elliott Hughesadb8c672012-03-06 16:49:32 -08002015 DecodedInstruction dec_insn(insn);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002016
Elliott Hughesadb8c672012-03-06 16:49:32 -08002017 llvm::Value* src1_value = EmitLoadDalvikReg(dec_insn.vB, kLong, kAccurate);
2018 llvm::Value* src2_value = EmitLoadDalvikReg(dec_insn.vC, kLong, kAccurate);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002019
2020 llvm::Value* cmp_eq = irb_.CreateICmpEQ(src1_value, src2_value);
2021 llvm::Value* cmp_lt = irb_.CreateICmpSLT(src1_value, src2_value);
2022
2023 llvm::Value* result = EmitCompareResultSelection(cmp_eq, cmp_lt);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002024 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002025
Logan Chien70f94b42011-12-27 17:49:11 +08002026 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2027}
2028
2029
Logan Chien2c37e8e2011-12-27 17:58:46 +08002030llvm::Value* MethodCompiler::EmitCompareResultSelection(llvm::Value* cmp_eq,
2031 llvm::Value* cmp_lt) {
2032
2033 llvm::Constant* zero = irb_.getJInt(0);
2034 llvm::Constant* pos1 = irb_.getJInt(1);
2035 llvm::Constant* neg1 = irb_.getJInt(-1);
2036
2037 llvm::Value* result_lt = irb_.CreateSelect(cmp_lt, neg1, pos1);
2038 llvm::Value* result_eq = irb_.CreateSelect(cmp_eq, zero, result_lt);
2039
2040 return result_eq;
2041}
2042
2043
Logan Chien70f94b42011-12-27 17:49:11 +08002044void MethodCompiler::EmitInsn_BinaryConditionalBranch(uint32_t dex_pc,
2045 Instruction const* insn,
2046 CondBranchKind cond) {
Logan Chiena78e3c82011-12-27 17:59:35 +08002047
Elliott Hughesadb8c672012-03-06 16:49:32 -08002048 DecodedInstruction dec_insn(insn);
Logan Chiena78e3c82011-12-27 17:59:35 +08002049
Elliott Hughesadb8c672012-03-06 16:49:32 -08002050 int8_t src1_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vA);
2051 int8_t src2_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vB);
Logan Chiena78e3c82011-12-27 17:59:35 +08002052
2053 DCHECK_NE(kRegUnknown, src1_reg_cat);
2054 DCHECK_NE(kRegUnknown, src2_reg_cat);
2055 DCHECK_NE(kRegCat2, src1_reg_cat);
2056 DCHECK_NE(kRegCat2, src2_reg_cat);
2057
Elliott Hughesadb8c672012-03-06 16:49:32 -08002058 int32_t branch_offset = dec_insn.vC;
Logan Chiena78e3c82011-12-27 17:59:35 +08002059
2060 if (branch_offset <= 0) {
2061 // Garbage collection safe-point on backward branch
2062 EmitGuard_GarbageCollectionSuspend(dex_pc);
2063 }
2064
Logan Chiena78e3c82011-12-27 17:59:35 +08002065 llvm::Value* src1_value;
2066 llvm::Value* src2_value;
2067
TDYa1278e9b4492012-04-24 15:50:27 -07002068 if (src1_reg_cat == kRegZero && src2_reg_cat == kRegZero) {
2069 src1_value = irb_.getInt32(0);
2070 src2_value = irb_.getInt32(0);
2071 } else if (src1_reg_cat != kRegZero && src2_reg_cat != kRegZero) {
Logan Chiena78e3c82011-12-27 17:59:35 +08002072 CHECK_EQ(src1_reg_cat, src2_reg_cat);
2073
2074 if (src1_reg_cat == kRegCat1nr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002075 src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
2076 src2_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002077 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002078 src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
2079 src2_value = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002080 }
2081 } else {
2082 DCHECK(src1_reg_cat == kRegZero ||
2083 src2_reg_cat == kRegZero);
2084
2085 if (src1_reg_cat == kRegZero) {
2086 if (src2_reg_cat == kRegCat1nr) {
2087 src1_value = irb_.getJInt(0);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002088 src2_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002089 } else {
2090 src1_value = irb_.getJNull();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002091 src2_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002092 }
2093 } else { // src2_reg_cat == kRegZero
2094 if (src2_reg_cat == kRegCat1nr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002095 src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002096 src2_value = irb_.getJInt(0);
2097 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002098 src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002099 src2_value = irb_.getJNull();
2100 }
2101 }
2102 }
2103
2104 llvm::Value* cond_value =
2105 EmitConditionResult(src1_value, src2_value, cond);
2106
2107 irb_.CreateCondBr(cond_value,
2108 GetBasicBlock(dex_pc + branch_offset),
2109 GetNextBasicBlock(dex_pc));
Logan Chien70f94b42011-12-27 17:49:11 +08002110}
2111
2112
2113void MethodCompiler::EmitInsn_UnaryConditionalBranch(uint32_t dex_pc,
2114 Instruction const* insn,
2115 CondBranchKind cond) {
Logan Chiena78e3c82011-12-27 17:59:35 +08002116
Elliott Hughesadb8c672012-03-06 16:49:32 -08002117 DecodedInstruction dec_insn(insn);
Logan Chiena78e3c82011-12-27 17:59:35 +08002118
Elliott Hughesadb8c672012-03-06 16:49:32 -08002119 int8_t src_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vA);
Logan Chiena78e3c82011-12-27 17:59:35 +08002120
2121 DCHECK_NE(kRegUnknown, src_reg_cat);
2122 DCHECK_NE(kRegCat2, src_reg_cat);
2123
Elliott Hughesadb8c672012-03-06 16:49:32 -08002124 int32_t branch_offset = dec_insn.vB;
Logan Chiena78e3c82011-12-27 17:59:35 +08002125
2126 if (branch_offset <= 0) {
2127 // Garbage collection safe-point on backward branch
2128 EmitGuard_GarbageCollectionSuspend(dex_pc);
2129 }
2130
Logan Chiena78e3c82011-12-27 17:59:35 +08002131 llvm::Value* src1_value;
2132 llvm::Value* src2_value;
2133
TDYa1278e9b4492012-04-24 15:50:27 -07002134 if (src_reg_cat == kRegZero) {
2135 src1_value = irb_.getInt32(0);
2136 src2_value = irb_.getInt32(0);
2137 } else if (src_reg_cat == kRegCat1nr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002138 src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002139 src2_value = irb_.getInt32(0);
2140 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002141 src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002142 src2_value = irb_.getJNull();
2143 }
2144
2145 llvm::Value* cond_value =
2146 EmitConditionResult(src1_value, src2_value, cond);
2147
2148 irb_.CreateCondBr(cond_value,
2149 GetBasicBlock(dex_pc + branch_offset),
2150 GetNextBasicBlock(dex_pc));
2151}
2152
2153
2154RegCategory MethodCompiler::GetInferredRegCategory(uint32_t dex_pc,
2155 uint16_t reg_idx) {
Logan Chiendd361c92012-04-10 23:40:37 +08002156
2157 Compiler::MethodReference mref(dex_file_, method_idx_);
2158
2159 InferredRegCategoryMap const* map =
Ian Rogers776ac1f2012-04-13 23:36:36 -07002160 verifier::MethodVerifier::GetInferredRegCategoryMap(mref);
Logan Chiendd361c92012-04-10 23:40:37 +08002161
Logan Chiena78e3c82011-12-27 17:59:35 +08002162 CHECK_NE(map, static_cast<InferredRegCategoryMap*>(NULL));
2163
2164 return map->GetRegCategory(dex_pc, reg_idx);
2165}
2166
2167
2168llvm::Value* MethodCompiler::EmitConditionResult(llvm::Value* lhs,
2169 llvm::Value* rhs,
2170 CondBranchKind cond) {
2171 switch (cond) {
2172 case kCondBranch_EQ:
2173 return irb_.CreateICmpEQ(lhs, rhs);
2174
2175 case kCondBranch_NE:
2176 return irb_.CreateICmpNE(lhs, rhs);
2177
2178 case kCondBranch_LT:
2179 return irb_.CreateICmpSLT(lhs, rhs);
2180
2181 case kCondBranch_GE:
2182 return irb_.CreateICmpSGE(lhs, rhs);
2183
2184 case kCondBranch_GT:
2185 return irb_.CreateICmpSGT(lhs, rhs);
2186
2187 case kCondBranch_LE:
2188 return irb_.CreateICmpSLE(lhs, rhs);
2189
2190 default: // Unreachable
2191 LOG(FATAL) << "Unknown conditional branch kind: " << cond;
2192 return NULL;
2193 }
Logan Chien70f94b42011-12-27 17:49:11 +08002194}
2195
TDYa12783bb6622012-04-17 02:20:34 -07002196void MethodCompiler::EmitMarkGCCard(llvm::Value* value, llvm::Value* target_addr) {
2197 // Using runtime support, let the target can override by InlineAssembly.
2198 llvm::Function* runtime_func = irb_.GetRuntime(MarkGCCard);
2199
2200 irb_.CreateCall2(runtime_func, value, target_addr);
2201}
Logan Chien70f94b42011-12-27 17:49:11 +08002202
Logan Chiene27fdbb2012-01-02 23:27:26 +08002203void
2204MethodCompiler::EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc,
2205 llvm::Value* array,
2206 llvm::Value* index) {
2207 llvm::Value* array_len = EmitLoadArrayLength(array);
2208
2209 llvm::Value* cmp = irb_.CreateICmpUGE(index, array_len);
2210
2211 llvm::BasicBlock* block_exception =
2212 CreateBasicBlockWithDexPC(dex_pc, "overflow");
2213
2214 llvm::BasicBlock* block_continue =
2215 CreateBasicBlockWithDexPC(dex_pc, "cont");
2216
2217 irb_.CreateCondBr(cmp, block_exception, block_continue);
2218
2219 irb_.SetInsertPoint(block_exception);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002220
TDYa127c8dc1012012-04-19 07:03:33 -07002221 EmitUpdateDexPC(dex_pc);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002222 irb_.CreateCall2(irb_.GetRuntime(ThrowIndexOutOfBounds), index, array_len);
2223 EmitBranchExceptionLandingPad(dex_pc);
2224
2225 irb_.SetInsertPoint(block_continue);
2226}
2227
2228
2229void MethodCompiler::EmitGuard_ArrayException(uint32_t dex_pc,
2230 llvm::Value* array,
2231 llvm::Value* index) {
2232 EmitGuard_NullPointerException(dex_pc, array);
2233 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array, index);
2234}
2235
2236
2237// Emit Array GetElementPtr
2238llvm::Value* MethodCompiler::EmitArrayGEP(llvm::Value* array_addr,
2239 llvm::Value* index_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08002240 llvm::Type* elem_type,
2241 JType elem_jty) {
2242
2243 int data_offset;
2244 if (elem_jty == kLong || elem_jty == kDouble ||
2245 (elem_jty == kObject && sizeof(uint64_t) == sizeof(Object*))) {
2246 data_offset = Array::DataOffset(sizeof(int64_t)).Int32Value();
2247 } else {
2248 data_offset = Array::DataOffset(sizeof(int32_t)).Int32Value();
2249 }
Logan Chiene27fdbb2012-01-02 23:27:26 +08002250
2251 llvm::Constant* data_offset_value =
Ian Rogers04ec04e2012-02-28 16:15:33 -08002252 irb_.getPtrEquivInt(data_offset);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002253
2254 llvm::Value* array_data_addr =
2255 irb_.CreatePtrDisp(array_addr, data_offset_value,
2256 elem_type->getPointerTo());
2257
2258 return irb_.CreateGEP(array_data_addr, index_value);
2259}
2260
2261
Logan Chien70f94b42011-12-27 17:49:11 +08002262void MethodCompiler::EmitInsn_AGet(uint32_t dex_pc,
2263 Instruction const* insn,
2264 JType elem_jty) {
Logan Chiene27fdbb2012-01-02 23:27:26 +08002265
Elliott Hughesadb8c672012-03-06 16:49:32 -08002266 DecodedInstruction dec_insn(insn);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002267
Elliott Hughesadb8c672012-03-06 16:49:32 -08002268 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
2269 llvm::Value* index_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002270
2271 EmitGuard_ArrayException(dex_pc, array_addr, index_value);
2272
2273 llvm::Type* elem_type = irb_.getJType(elem_jty, kArray);
2274
2275 llvm::Value* array_elem_addr =
Ian Rogers04ec04e2012-02-28 16:15:33 -08002276 EmitArrayGEP(array_addr, index_value, elem_type, elem_jty);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002277
2278 llvm::Value* array_elem_value = irb_.CreateLoad(array_elem_addr);
2279
Elliott Hughesadb8c672012-03-06 16:49:32 -08002280 EmitStoreDalvikReg(dec_insn.vA, elem_jty, kArray, array_elem_value);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002281
Logan Chien70f94b42011-12-27 17:49:11 +08002282 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2283}
2284
2285
2286void MethodCompiler::EmitInsn_APut(uint32_t dex_pc,
2287 Instruction const* insn,
2288 JType elem_jty) {
Logan Chien8dabb432012-01-02 23:29:32 +08002289
Elliott Hughesadb8c672012-03-06 16:49:32 -08002290 DecodedInstruction dec_insn(insn);
Logan Chien8dabb432012-01-02 23:29:32 +08002291
Elliott Hughesadb8c672012-03-06 16:49:32 -08002292 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
2293 llvm::Value* index_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate);
Logan Chien8dabb432012-01-02 23:29:32 +08002294
2295 EmitGuard_ArrayException(dex_pc, array_addr, index_value);
2296
2297 llvm::Type* elem_type = irb_.getJType(elem_jty, kArray);
2298
2299 llvm::Value* array_elem_addr =
Ian Rogers04ec04e2012-02-28 16:15:33 -08002300 EmitArrayGEP(array_addr, index_value, elem_type, elem_jty);
Logan Chien8dabb432012-01-02 23:29:32 +08002301
Elliott Hughesadb8c672012-03-06 16:49:32 -08002302 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, elem_jty, kArray);
Logan Chien8dabb432012-01-02 23:29:32 +08002303
TDYa12783bb6622012-04-17 02:20:34 -07002304 if (elem_jty == kObject) { // If put an object, check the type, and mark GC card table.
TDYa1271b86d072012-04-05 17:38:56 -07002305 llvm::Function* runtime_func = irb_.GetRuntime(CheckPutArrayElement);
2306
2307 irb_.CreateCall2(runtime_func, new_value, array_addr);
2308
2309 EmitGuard_ExceptionLandingPad(dex_pc);
TDYa12783bb6622012-04-17 02:20:34 -07002310
2311 EmitMarkGCCard(new_value, array_addr);
TDYa1271b86d072012-04-05 17:38:56 -07002312 }
2313
Logan Chien8dabb432012-01-02 23:29:32 +08002314 irb_.CreateStore(new_value, array_elem_addr);
2315
Logan Chien70f94b42011-12-27 17:49:11 +08002316 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2317}
2318
2319
2320void MethodCompiler::EmitInsn_IGet(uint32_t dex_pc,
2321 Instruction const* insn,
2322 JType field_jty) {
Logan Chien48f1d2a2012-01-02 22:49:53 +08002323
Elliott Hughesadb8c672012-03-06 16:49:32 -08002324 DecodedInstruction dec_insn(insn);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002325
Elliott Hughesadb8c672012-03-06 16:49:32 -08002326 uint32_t reg_idx = dec_insn.vB;
2327 uint32_t field_idx = dec_insn.vC;
Logan Chien48f1d2a2012-01-02 22:49:53 +08002328
Logan Chien48f1d2a2012-01-02 22:49:53 +08002329 llvm::Value* object_addr = EmitLoadDalvikReg(reg_idx, kObject, kAccurate);
2330
2331 EmitGuard_NullPointerException(dex_pc, object_addr);
2332
2333 llvm::Value* field_value;
2334
Logan Chien933abf82012-04-11 12:24:31 +08002335 int field_offset;
2336 bool is_volatile;
2337 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
2338 field_idx, oat_compilation_unit_, field_offset, is_volatile, false);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002339
Logan Chien933abf82012-04-11 12:24:31 +08002340 if (!is_fast_path) {
Logan Chien48f1d2a2012-01-02 22:49:53 +08002341 llvm::Function* runtime_func;
2342
2343 if (field_jty == kObject) {
Logan Chien3b2b2e72012-03-06 16:11:45 +08002344 runtime_func = irb_.GetRuntime(GetObjectInstance);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002345 } else if (field_jty == kLong || field_jty == kDouble) {
Logan Chien3b2b2e72012-03-06 16:11:45 +08002346 runtime_func = irb_.GetRuntime(Get64Instance);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002347 } else {
Logan Chien3b2b2e72012-03-06 16:11:45 +08002348 runtime_func = irb_.GetRuntime(Get32Instance);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002349 }
2350
2351 llvm::ConstantInt* field_idx_value = irb_.getInt32(field_idx);
2352
2353 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2354
TDYa127c8dc1012012-04-19 07:03:33 -07002355 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002356
Logan Chien3b2b2e72012-03-06 16:11:45 +08002357 field_value = irb_.CreateCall3(runtime_func, field_idx_value,
2358 method_object_addr, object_addr);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002359
2360 EmitGuard_ExceptionLandingPad(dex_pc);
2361
2362 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002363 DCHECK_GE(field_offset, 0);
2364
Logan Chien48f1d2a2012-01-02 22:49:53 +08002365 llvm::PointerType* field_type =
2366 irb_.getJType(field_jty, kField)->getPointerTo();
2367
Logan Chien933abf82012-04-11 12:24:31 +08002368 llvm::ConstantInt* field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002369
2370 llvm::Value* field_addr =
Logan Chien933abf82012-04-11 12:24:31 +08002371 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002372
Logan Chien933abf82012-04-11 12:24:31 +08002373 // TODO: Check is_volatile. We need to generate atomic load instruction
2374 // when is_volatile is true.
Logan Chien48f1d2a2012-01-02 22:49:53 +08002375 field_value = irb_.CreateLoad(field_addr);
2376 }
2377
Elliott Hughesadb8c672012-03-06 16:49:32 -08002378 EmitStoreDalvikReg(dec_insn.vA, field_jty, kField, field_value);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002379
Logan Chien70f94b42011-12-27 17:49:11 +08002380 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2381}
2382
2383
2384void MethodCompiler::EmitInsn_IPut(uint32_t dex_pc,
2385 Instruction const* insn,
2386 JType field_jty) {
Logan Chiendd6aa872012-01-03 16:06:32 +08002387
Elliott Hughesadb8c672012-03-06 16:49:32 -08002388 DecodedInstruction dec_insn(insn);
Logan Chiendd6aa872012-01-03 16:06:32 +08002389
Elliott Hughesadb8c672012-03-06 16:49:32 -08002390 uint32_t reg_idx = dec_insn.vB;
2391 uint32_t field_idx = dec_insn.vC;
Logan Chiendd6aa872012-01-03 16:06:32 +08002392
Logan Chiendd6aa872012-01-03 16:06:32 +08002393 llvm::Value* object_addr = EmitLoadDalvikReg(reg_idx, kObject, kAccurate);
2394
2395 EmitGuard_NullPointerException(dex_pc, object_addr);
2396
Elliott Hughesadb8c672012-03-06 16:49:32 -08002397 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, field_jty, kField);
Logan Chiendd6aa872012-01-03 16:06:32 +08002398
Logan Chien933abf82012-04-11 12:24:31 +08002399 int field_offset;
2400 bool is_volatile;
2401 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
2402 field_idx, oat_compilation_unit_, field_offset, is_volatile, true);
Logan Chiendd6aa872012-01-03 16:06:32 +08002403
Logan Chien933abf82012-04-11 12:24:31 +08002404 if (!is_fast_path) {
Logan Chiendd6aa872012-01-03 16:06:32 +08002405 llvm::Function* runtime_func;
2406
2407 if (field_jty == kObject) {
2408 runtime_func = irb_.GetRuntime(SetObjectInstance);
2409 } else if (field_jty == kLong || field_jty == kDouble) {
2410 runtime_func = irb_.GetRuntime(Set64Instance);
2411 } else {
2412 runtime_func = irb_.GetRuntime(Set32Instance);
2413 }
2414
2415 llvm::Value* field_idx_value = irb_.getInt32(field_idx);
2416
2417 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2418
TDYa127c8dc1012012-04-19 07:03:33 -07002419 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002420
Logan Chien3b2b2e72012-03-06 16:11:45 +08002421 irb_.CreateCall4(runtime_func, field_idx_value,
2422 method_object_addr, object_addr, new_value);
Logan Chiendd6aa872012-01-03 16:06:32 +08002423
2424 EmitGuard_ExceptionLandingPad(dex_pc);
2425
2426 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002427 DCHECK_GE(field_offset, 0);
2428
Logan Chiendd6aa872012-01-03 16:06:32 +08002429 llvm::PointerType* field_type =
2430 irb_.getJType(field_jty, kField)->getPointerTo();
2431
Logan Chien933abf82012-04-11 12:24:31 +08002432 llvm::Value* field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chiendd6aa872012-01-03 16:06:32 +08002433
2434 llvm::Value* field_addr =
Logan Chien933abf82012-04-11 12:24:31 +08002435 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
Logan Chiendd6aa872012-01-03 16:06:32 +08002436
Logan Chien933abf82012-04-11 12:24:31 +08002437 // TODO: Check is_volatile. We need to generate atomic store instruction
2438 // when is_volatile is true.
Logan Chiendd6aa872012-01-03 16:06:32 +08002439 irb_.CreateStore(new_value, field_addr);
TDYa12783bb6622012-04-17 02:20:34 -07002440
2441 if (field_jty == kObject) { // If put an object, mark the GC card table.
2442 EmitMarkGCCard(new_value, object_addr);
2443 }
Logan Chiendd6aa872012-01-03 16:06:32 +08002444 }
2445
Logan Chien70f94b42011-12-27 17:49:11 +08002446 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2447}
2448
2449
Logan Chien438c4b62012-01-17 16:06:00 +08002450llvm::Value* MethodCompiler::EmitLoadStaticStorage(uint32_t dex_pc,
2451 uint32_t type_idx) {
2452 llvm::BasicBlock* block_load_static =
2453 CreateBasicBlockWithDexPC(dex_pc, "load_static");
2454
2455 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
2456
2457 // Load static storage from dex cache
2458 llvm::Value* storage_field_addr =
2459 EmitLoadDexCacheStaticStorageFieldAddr(type_idx);
2460
2461 llvm::Value* storage_object_addr = irb_.CreateLoad(storage_field_addr);
2462
2463 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
2464
2465 // Test: Is the static storage of this class initialized?
2466 llvm::Value* equal_null =
2467 irb_.CreateICmpEQ(storage_object_addr, irb_.getJNull());
2468
2469 irb_.CreateCondBr(equal_null, block_load_static, block_cont);
2470
2471 // Failback routine to load the class object
2472 irb_.SetInsertPoint(block_load_static);
2473
2474 llvm::Function* runtime_func =
2475 irb_.GetRuntime(InitializeStaticStorage);
2476
2477 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
2478
2479 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2480
TDYa127706e9b62012-04-19 12:24:26 -07002481 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
2482
TDYa127c8dc1012012-04-19 07:03:33 -07002483 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002484
Logan Chien438c4b62012-01-17 16:06:00 +08002485 llvm::Value* loaded_storage_object_addr =
TDYa127706e9b62012-04-19 12:24:26 -07002486 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
Logan Chien438c4b62012-01-17 16:06:00 +08002487
2488 EmitGuard_ExceptionLandingPad(dex_pc);
2489
2490 llvm::BasicBlock* block_after_load_static = irb_.GetInsertBlock();
2491
2492 irb_.CreateBr(block_cont);
2493
2494 // Now the class object must be loaded
2495 irb_.SetInsertPoint(block_cont);
2496
2497 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
2498
2499 phi->addIncoming(storage_object_addr, block_original);
2500 phi->addIncoming(loaded_storage_object_addr, block_after_load_static);
2501
2502 return phi;
2503}
2504
2505
Logan Chien70f94b42011-12-27 17:49:11 +08002506void MethodCompiler::EmitInsn_SGet(uint32_t dex_pc,
2507 Instruction const* insn,
2508 JType field_jty) {
Logan Chien438c4b62012-01-17 16:06:00 +08002509
Elliott Hughesadb8c672012-03-06 16:49:32 -08002510 DecodedInstruction dec_insn(insn);
Logan Chien438c4b62012-01-17 16:06:00 +08002511
Logan Chien933abf82012-04-11 12:24:31 +08002512 uint32_t field_idx = dec_insn.vB;
Logan Chien438c4b62012-01-17 16:06:00 +08002513
Logan Chien933abf82012-04-11 12:24:31 +08002514 int field_offset;
2515 int ssb_index;
2516 bool is_referrers_class;
2517 bool is_volatile;
2518
2519 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
2520 field_idx, oat_compilation_unit_, field_offset, ssb_index,
2521 is_referrers_class, is_volatile, false);
Logan Chien438c4b62012-01-17 16:06:00 +08002522
2523 llvm::Value* static_field_value;
2524
Logan Chien933abf82012-04-11 12:24:31 +08002525 if (!is_fast_path) {
Logan Chien438c4b62012-01-17 16:06:00 +08002526 llvm::Function* runtime_func;
2527
2528 if (field_jty == kObject) {
2529 runtime_func = irb_.GetRuntime(GetObjectStatic);
2530 } else if (field_jty == kLong || field_jty == kDouble) {
2531 runtime_func = irb_.GetRuntime(Get64Static);
2532 } else {
2533 runtime_func = irb_.GetRuntime(Get32Static);
2534 }
2535
Elliott Hughesadb8c672012-03-06 16:49:32 -08002536 llvm::Constant* field_idx_value = irb_.getInt32(dec_insn.vB);
Logan Chien438c4b62012-01-17 16:06:00 +08002537
2538 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2539
TDYa127c8dc1012012-04-19 07:03:33 -07002540 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002541
Logan Chien438c4b62012-01-17 16:06:00 +08002542 static_field_value =
2543 irb_.CreateCall2(runtime_func, field_idx_value, method_object_addr);
2544
2545 EmitGuard_ExceptionLandingPad(dex_pc);
2546
2547 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002548 DCHECK_GE(field_offset, 0);
Logan Chien438c4b62012-01-17 16:06:00 +08002549
Logan Chien933abf82012-04-11 12:24:31 +08002550 llvm::Value* static_storage_addr = NULL;
2551
2552 if (is_referrers_class) {
2553 // Fast path, static storage base is this method's class
2554 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2555
TDYa127ee1f59b2012-04-25 00:56:40 -07002556 static_storage_addr =
2557 irb_.LoadFromObjectOffset(method_object_addr,
2558 Method::DeclaringClassOffset().Int32Value(),
2559 irb_.getJObjectTy());
Logan Chien933abf82012-04-11 12:24:31 +08002560 } else {
2561 // Medium path, static storage base in a different class which
2562 // requires checks that the other class is initialized
2563 DCHECK_GE(ssb_index, 0);
2564 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
2565 }
2566
2567 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chien438c4b62012-01-17 16:06:00 +08002568
2569 llvm::Value* static_field_addr =
2570 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
2571 irb_.getJType(field_jty, kField)->getPointerTo());
2572
Logan Chien933abf82012-04-11 12:24:31 +08002573 // TODO: Check is_volatile. We need to generate atomic load instruction
2574 // when is_volatile is true.
Logan Chien438c4b62012-01-17 16:06:00 +08002575 static_field_value = irb_.CreateLoad(static_field_addr);
2576 }
2577
Elliott Hughesadb8c672012-03-06 16:49:32 -08002578 EmitStoreDalvikReg(dec_insn.vA, field_jty, kField, static_field_value);
Logan Chien438c4b62012-01-17 16:06:00 +08002579
Logan Chien70f94b42011-12-27 17:49:11 +08002580 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2581}
2582
2583
2584void MethodCompiler::EmitInsn_SPut(uint32_t dex_pc,
2585 Instruction const* insn,
2586 JType field_jty) {
Logan Chien14179c82012-01-17 17:06:34 +08002587
Elliott Hughesadb8c672012-03-06 16:49:32 -08002588 DecodedInstruction dec_insn(insn);
Logan Chien14179c82012-01-17 17:06:34 +08002589
Logan Chien933abf82012-04-11 12:24:31 +08002590 uint32_t field_idx = dec_insn.vB;
Logan Chien14179c82012-01-17 17:06:34 +08002591
Elliott Hughesadb8c672012-03-06 16:49:32 -08002592 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, field_jty, kField);
Logan Chien14179c82012-01-17 17:06:34 +08002593
Logan Chien933abf82012-04-11 12:24:31 +08002594 int field_offset;
2595 int ssb_index;
2596 bool is_referrers_class;
2597 bool is_volatile;
2598
2599 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
2600 field_idx, oat_compilation_unit_, field_offset, ssb_index,
2601 is_referrers_class, is_volatile, true);
2602
2603 if (!is_fast_path) {
Logan Chien14179c82012-01-17 17:06:34 +08002604 llvm::Function* runtime_func;
2605
2606 if (field_jty == kObject) {
2607 runtime_func = irb_.GetRuntime(SetObjectStatic);
2608 } else if (field_jty == kLong || field_jty == kDouble) {
2609 runtime_func = irb_.GetRuntime(Set64Static);
2610 } else {
2611 runtime_func = irb_.GetRuntime(Set32Static);
2612 }
2613
Elliott Hughesadb8c672012-03-06 16:49:32 -08002614 llvm::Constant* field_idx_value = irb_.getInt32(dec_insn.vB);
Logan Chien14179c82012-01-17 17:06:34 +08002615
2616 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2617
TDYa127c8dc1012012-04-19 07:03:33 -07002618 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002619
Logan Chien14179c82012-01-17 17:06:34 +08002620 irb_.CreateCall3(runtime_func, field_idx_value,
2621 method_object_addr, new_value);
2622
2623 EmitGuard_ExceptionLandingPad(dex_pc);
2624
2625 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002626 DCHECK_GE(field_offset, 0);
Logan Chien14179c82012-01-17 17:06:34 +08002627
Logan Chien933abf82012-04-11 12:24:31 +08002628 llvm::Value* static_storage_addr = NULL;
2629
2630 if (is_referrers_class) {
2631 // Fast path, static storage base is this method's class
2632 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2633
TDYa127ee1f59b2012-04-25 00:56:40 -07002634 static_storage_addr =
2635 irb_.LoadFromObjectOffset(method_object_addr,
2636 Method::DeclaringClassOffset().Int32Value(),
2637 irb_.getJObjectTy());
Logan Chien933abf82012-04-11 12:24:31 +08002638 } else {
2639 // Medium path, static storage base in a different class which
2640 // requires checks that the other class is initialized
2641 DCHECK_GE(ssb_index, 0);
2642 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
2643 }
2644
2645 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chien14179c82012-01-17 17:06:34 +08002646
2647 llvm::Value* static_field_addr =
2648 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
2649 irb_.getJType(field_jty, kField)->getPointerTo());
2650
Logan Chien933abf82012-04-11 12:24:31 +08002651 // TODO: Check is_volatile. We need to generate atomic store instruction
2652 // when is_volatile is true.
Logan Chien14179c82012-01-17 17:06:34 +08002653 irb_.CreateStore(new_value, static_field_addr);
TDYa12783bb6622012-04-17 02:20:34 -07002654
2655 if (field_jty == kObject) { // If put an object, mark the GC card table.
2656 EmitMarkGCCard(new_value, static_storage_addr);
2657 }
Logan Chien14179c82012-01-17 17:06:34 +08002658 }
2659
Logan Chien70f94b42011-12-27 17:49:11 +08002660 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2661}
2662
2663
Logan Chien1a121b92012-02-15 22:23:42 +08002664void MethodCompiler::
2665EmitLoadActualParameters(std::vector<llvm::Value*>& args,
2666 uint32_t callee_method_idx,
Elliott Hughesadb8c672012-03-06 16:49:32 -08002667 DecodedInstruction const& dec_insn,
Logan Chien7e7fabc2012-04-10 18:59:11 +08002668 InvokeArgFmt arg_fmt,
Logan Chien1a121b92012-02-15 22:23:42 +08002669 bool is_static) {
2670
2671 // Get method signature
2672 DexFile::MethodId const& method_id =
2673 dex_file_->GetMethodId(callee_method_idx);
2674
Logan Chien8faf8022012-02-24 12:25:29 +08002675 uint32_t shorty_size;
Logan Chien1a121b92012-02-15 22:23:42 +08002676 char const* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size);
Logan Chien8faf8022012-02-24 12:25:29 +08002677 CHECK_GE(shorty_size, 1u);
Logan Chien1a121b92012-02-15 22:23:42 +08002678
2679 // Load argument values according to the shorty (without "this")
2680 uint16_t reg_count = 0;
2681
2682 if (!is_static) {
2683 ++reg_count; // skip the "this" pointer
2684 }
2685
Logan Chien7e7fabc2012-04-10 18:59:11 +08002686 bool is_range = (arg_fmt == kArgRange);
2687
Logan Chien8faf8022012-02-24 12:25:29 +08002688 for (uint32_t i = 1; i < shorty_size; ++i) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002689 uint32_t reg_idx = (is_range) ? (dec_insn.vC + reg_count)
2690 : (dec_insn.arg[reg_count]);
Logan Chien1a121b92012-02-15 22:23:42 +08002691
2692 args.push_back(EmitLoadDalvikReg(reg_idx, shorty[i], kAccurate));
2693
2694 ++reg_count;
2695 if (shorty[i] == 'J' || shorty[i] == 'D') {
2696 // Wide types, such as long and double, are using a pair of registers
2697 // to store the value, so we have to increase arg_reg again.
2698 ++reg_count;
2699 }
2700 }
2701
Elliott Hughesadb8c672012-03-06 16:49:32 -08002702 DCHECK_EQ(reg_count, dec_insn.vA)
Logan Chien1a121b92012-02-15 22:23:42 +08002703 << "Actual argument mismatch for callee: "
2704 << PrettyMethod(callee_method_idx, *dex_file_);
2705}
2706
TDYa1270b686e52012-04-09 22:43:35 -07002707llvm::Value* MethodCompiler::EmitFixStub(llvm::Value* callee_method_object_addr,
2708 uint32_t method_idx,
2709 bool is_static) {
2710 // TODO: Remove this after we solve the link and trampoline related problems.
2711 llvm::Value* code_addr = irb_.CreateCall(irb_.GetRuntime(FixStub), callee_method_object_addr);
2712
2713 llvm::FunctionType* method_type = GetFunctionType(method_idx, is_static);
2714
2715 return irb_.CreatePointerCast(code_addr, method_type->getPointerTo());
TDYa12785321912012-04-01 15:24:56 -07002716}
Logan Chien1a121b92012-02-15 22:23:42 +08002717
Shih-wei Liao399ed3f2012-03-08 01:27:04 -08002718
Logan Chien7e7fabc2012-04-10 18:59:11 +08002719void MethodCompiler::EmitInsn_Invoke(uint32_t dex_pc,
2720 Instruction const* insn,
2721 InvokeType invoke_type,
2722 InvokeArgFmt arg_fmt) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002723 DecodedInstruction dec_insn(insn);
Logan Chien46fbb412012-02-15 22:29:08 +08002724
Logan Chien61c65dc2012-02-29 03:22:30 +08002725 bool is_static = (invoke_type == kStatic);
Logan Chien7e7fabc2012-04-10 18:59:11 +08002726 uint32_t callee_method_idx = dec_insn.vB;
Logan Chien61c65dc2012-02-29 03:22:30 +08002727
Logan Chien7e7fabc2012-04-10 18:59:11 +08002728 // Compute invoke related information for compiler decision
2729 int vtable_idx = -1;
Logan Chien92ad16d2012-03-18 05:48:55 +08002730 uintptr_t direct_code = 0; // Currently unused
Logan Chienfca64372012-04-23 14:57:01 +08002731 uintptr_t direct_method = 0;
Logan Chien61c65dc2012-02-29 03:22:30 +08002732 bool is_fast_path = compiler_->
Logan Chien7e7fabc2012-04-10 18:59:11 +08002733 ComputeInvokeInfo(callee_method_idx, oat_compilation_unit_,
Logan Chien92ad16d2012-03-18 05:48:55 +08002734 invoke_type, vtable_idx, direct_code, direct_method);
Logan Chien61c65dc2012-02-29 03:22:30 +08002735
Logan Chien7e7fabc2012-04-10 18:59:11 +08002736 // Load *this* actual parameter
Logan Chien1a121b92012-02-15 22:23:42 +08002737 llvm::Value* this_addr = NULL;
2738
2739 if (!is_static) {
2740 // Test: Is *this* parameter equal to null?
Logan Chien7e7fabc2012-04-10 18:59:11 +08002741 this_addr = (arg_fmt == kArgReg) ?
2742 EmitLoadDalvikReg(dec_insn.arg[0], kObject, kAccurate):
2743 EmitLoadDalvikReg(dec_insn.vC + 0, kObject, kAccurate);
2744
Logan Chien1a121b92012-02-15 22:23:42 +08002745 EmitGuard_NullPointerException(dex_pc, this_addr);
2746 }
2747
Logan Chien7e7fabc2012-04-10 18:59:11 +08002748 // Load the method object
TDYa1274e42a592012-04-10 20:13:54 -07002749 llvm::Value* callee_method_object_addr = NULL;
Logan Chien7e7fabc2012-04-10 18:59:11 +08002750
2751 if (!is_fast_path) {
TDYa1274e42a592012-04-10 20:13:54 -07002752 callee_method_object_addr =
Logan Chien7e7fabc2012-04-10 18:59:11 +08002753 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx, invoke_type,
2754 this_addr, dex_pc, is_fast_path);
2755 } else {
2756 switch (invoke_type) {
2757 case kStatic:
2758 case kDirect:
Logan Chienfca64372012-04-23 14:57:01 +08002759 if (direct_method != 0u &&
2760 direct_method != static_cast<uintptr_t>(-1)) {
2761 callee_method_object_addr =
TDYa12717826bf2012-04-24 01:15:10 -07002762 irb_.CreateIntToPtr(irb_.getPtrEquivInt(direct_method),
2763 irb_.getJObjectTy());
Logan Chienfca64372012-04-23 14:57:01 +08002764 } else {
2765 callee_method_object_addr =
2766 EmitLoadSDCalleeMethodObjectAddr(callee_method_idx);
2767 }
Logan Chien7e7fabc2012-04-10 18:59:11 +08002768 break;
2769
2770 case kVirtual:
2771 DCHECK(vtable_idx != -1);
TDYa1274e42a592012-04-10 20:13:54 -07002772 callee_method_object_addr =
Logan Chien7e7fabc2012-04-10 18:59:11 +08002773 EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr);
2774 break;
2775
2776 case kSuper:
2777 LOG(FATAL) << "invoke-super should be promoted to invoke-direct in "
2778 "the fast path.";
2779 break;
2780
2781 case kInterface:
TDYa1274e42a592012-04-10 20:13:54 -07002782 callee_method_object_addr =
Logan Chien7e7fabc2012-04-10 18:59:11 +08002783 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx,
2784 invoke_type, this_addr,
2785 dex_pc, is_fast_path);
2786 break;
2787 }
2788 }
2789
Logan Chien7e7fabc2012-04-10 18:59:11 +08002790 llvm::Value* code_addr =
TDYa127ee1f59b2012-04-25 00:56:40 -07002791 irb_.LoadFromObjectOffset(callee_method_object_addr,
2792 Method::GetCodeOffset().Int32Value(),
2793 GetFunctionType(callee_method_idx, is_static)->getPointerTo());
TDYa12785321912012-04-01 15:24:56 -07002794
Logan Chien1a121b92012-02-15 22:23:42 +08002795 // Load the actual parameter
2796 std::vector<llvm::Value*> args;
2797
2798 args.push_back(callee_method_object_addr); // method object for callee
2799
2800 if (!is_static) {
Logan Chien7e7fabc2012-04-10 18:59:11 +08002801 DCHECK(this_addr != NULL);
Logan Chien1a121b92012-02-15 22:23:42 +08002802 args.push_back(this_addr); // "this" object for callee
2803 }
2804
2805 EmitLoadActualParameters(args, callee_method_idx, dec_insn,
Logan Chien7e7fabc2012-04-10 18:59:11 +08002806 arg_fmt, is_static);
Logan Chien1a121b92012-02-15 22:23:42 +08002807
TDYa1275bb86012012-04-11 05:57:28 -07002808#if 0
Logan Chien8dfcbea2012-02-17 18:50:32 +08002809 // Invoke callee
TDYa127c8dc1012012-04-19 07:03:33 -07002810 EmitUpdateDexPC(dex_pc);
Logan Chien1a121b92012-02-15 22:23:42 +08002811 llvm::Value* retval = irb_.CreateCall(code_addr, args);
2812 EmitGuard_ExceptionLandingPad(dex_pc);
2813
Logan Chien7e7fabc2012-04-10 18:59:11 +08002814 uint32_t callee_access_flags = is_static ? kAccStatic : 0;
2815 UniquePtr<OatCompilationUnit> callee_oat_compilation_unit(
2816 oat_compilation_unit_->GetCallee(callee_method_idx, callee_access_flags));
2817
2818 char ret_shorty = callee_oat_compilation_unit->GetShorty()[0];
Shih-wei Liao90d50992012-02-19 03:32:05 -08002819 if (ret_shorty != 'V') {
Logan Chien1a121b92012-02-15 22:23:42 +08002820 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
2821 }
TDYa1275bb86012012-04-11 05:57:28 -07002822#else
2823 uint32_t callee_access_flags = is_static ? kAccStatic : 0;
2824 UniquePtr<OatCompilationUnit> callee_oat_compilation_unit(
2825 oat_compilation_unit_->GetCallee(callee_method_idx, callee_access_flags));
2826
2827 char ret_shorty = callee_oat_compilation_unit->GetShorty()[0];
2828
2829
TDYa127c8dc1012012-04-19 07:03:33 -07002830 EmitUpdateDexPC(dex_pc);
TDYa1275bb86012012-04-11 05:57:28 -07002831
2832
2833 llvm::BasicBlock* block_normal = CreateBasicBlockWithDexPC(dex_pc, "normal");
TDYa127ce154722012-04-21 16:43:29 -07002834 llvm::BasicBlock* block_stub = CreateBasicBlockWithDexPC(dex_pc, "stub");
TDYa1275bb86012012-04-11 05:57:28 -07002835 llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont");
2836
2837 llvm::Type* accurate_ret_type = irb_.getJType(ret_shorty, kAccurate);
2838 llvm::Value* retval_addr = NULL;
2839 if (ret_shorty != 'V') {
2840 retval_addr = irb_.CreateAlloca(accurate_ret_type);
2841 }
2842
2843
TDYa1275bb86012012-04-11 05:57:28 -07002844 llvm::Value* code_addr_int = irb_.CreatePtrToInt(code_addr, irb_.getPtrEquivIntTy());
TDYa127ce154722012-04-21 16:43:29 -07002845 llvm::Value* max_stub_int = irb_.getPtrEquivInt(special_stub::kMaxSpecialStub);
2846 llvm::Value* is_stub = irb_.CreateICmpULT(code_addr_int, max_stub_int);
2847 irb_.CreateCondBr(is_stub, block_stub, block_normal);
TDYa1275bb86012012-04-11 05:57:28 -07002848
2849
2850 irb_.SetInsertPoint(block_normal);
2851 {
2852 // Invoke callee
TDYa127ce154722012-04-21 16:43:29 -07002853 llvm::Value* retval = irb_.CreateCall(code_addr, args);
TDYa1275bb86012012-04-11 05:57:28 -07002854 if (ret_shorty != 'V') {
TDYa127ce154722012-04-21 16:43:29 -07002855 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
TDYa1275bb86012012-04-11 05:57:28 -07002856 }
2857 }
2858 irb_.CreateBr(block_continue);
2859
2860
TDYa127ce154722012-04-21 16:43:29 -07002861 irb_.SetInsertPoint(block_stub);
TDYa1275bb86012012-04-11 05:57:28 -07002862 {
TDYa127ce154722012-04-21 16:43:29 -07002863 { // lazy link
2864 // TODO: Remove this after we solve the link problem.
2865 llvm::BasicBlock* block_proxy_stub = CreateBasicBlockWithDexPC(dex_pc, "proxy");
2866 llvm::BasicBlock* block_link = CreateBasicBlockWithDexPC(dex_pc, "link");
2867
2868 irb_.CreateCondBr(irb_.CreateIsNull(code_addr), block_link, block_proxy_stub);
2869
2870
2871 irb_.SetInsertPoint(block_link);
2872 code_addr = EmitFixStub(callee_method_object_addr, callee_method_idx, is_static);
2873
2874 EmitGuard_ExceptionLandingPad(dex_pc);
2875
2876 llvm::Value* retval = irb_.CreateCall(code_addr, args);
2877 if (ret_shorty != 'V') {
2878 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
2879 }
2880 irb_.CreateBr(block_continue);
2881
2882
2883 irb_.SetInsertPoint(block_proxy_stub);
TDYa1275bb86012012-04-11 05:57:28 -07002884 }
TDYa127ce154722012-04-21 16:43:29 -07002885 { // proxy stub
2886 llvm::Value* temp_space_addr;
2887 if (ret_shorty != 'V') {
2888 temp_space_addr = irb_.CreateAlloca(irb_.getJValueTy());
2889 args.push_back(temp_space_addr);
2890 }
2891 // TODO: Remove this after we solve the proxy trampoline calling convention problem.
2892 irb_.CreateCall(irb_.GetRuntime(ProxyInvokeHandler), args);
2893 if (ret_shorty != 'V') {
2894 llvm::Value* result_addr =
2895 irb_.CreateBitCast(temp_space_addr, accurate_ret_type->getPointerTo());
2896 llvm::Value* retval = irb_.CreateLoad(result_addr);
2897 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
2898 }
TDYa1275bb86012012-04-11 05:57:28 -07002899 }
2900 }
2901 irb_.CreateBr(block_continue);
2902
2903
2904 irb_.SetInsertPoint(block_continue);
2905
TDYa1275bb86012012-04-11 05:57:28 -07002906 EmitGuard_ExceptionLandingPad(dex_pc);
2907#endif
Logan Chien1a121b92012-02-15 22:23:42 +08002908
Logan Chien70f94b42011-12-27 17:49:11 +08002909 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2910}
2911
2912
Logan Chien7e7fabc2012-04-10 18:59:11 +08002913llvm::Value* MethodCompiler::
2914EmitLoadSDCalleeMethodObjectAddr(uint32_t callee_method_idx) {
2915 llvm::Value* callee_method_object_field_addr =
2916 EmitLoadDexCacheResolvedMethodFieldAddr(callee_method_idx);
Logan Chien7caf37e2012-02-03 22:56:04 +08002917
Logan Chien7e7fabc2012-04-10 18:59:11 +08002918 return irb_.CreateLoad(callee_method_object_field_addr);
2919}
Logan Chien7caf37e2012-02-03 22:56:04 +08002920
Logan Chien7caf37e2012-02-03 22:56:04 +08002921
Logan Chien7e7fabc2012-04-10 18:59:11 +08002922llvm::Value* MethodCompiler::
2923EmitLoadVirtualCalleeMethodObjectAddr(int vtable_idx,
2924 llvm::Value* this_addr) {
2925 // Load class object of *this* pointer
TDYa127ee1f59b2012-04-25 00:56:40 -07002926 llvm::Value* class_object_addr =
2927 irb_.LoadFromObjectOffset(this_addr,
2928 Object::ClassOffset().Int32Value(),
2929 irb_.getJObjectTy());
Logan Chien7caf37e2012-02-03 22:56:04 +08002930
Logan Chien7e7fabc2012-04-10 18:59:11 +08002931 // Load vtable address
TDYa127ee1f59b2012-04-25 00:56:40 -07002932 llvm::Value* vtable_addr =
2933 irb_.LoadFromObjectOffset(class_object_addr,
2934 Class::VTableOffset().Int32Value(),
2935 irb_.getJObjectTy());
Logan Chien7e7fabc2012-04-10 18:59:11 +08002936
2937 // Load callee method object
2938 llvm::Value* vtable_idx_value =
2939 irb_.getPtrEquivInt(static_cast<uint64_t>(vtable_idx));
2940
2941 llvm::Value* method_field_addr =
2942 EmitArrayGEP(vtable_addr, vtable_idx_value, irb_.getJObjectTy(), kObject);
2943
2944 return irb_.CreateLoad(method_field_addr);
2945}
2946
2947
2948llvm::Value* MethodCompiler::
2949EmitCallRuntimeForCalleeMethodObjectAddr(uint32_t callee_method_idx,
2950 InvokeType invoke_type,
2951 llvm::Value* this_addr,
2952 uint32_t dex_pc,
2953 bool is_fast_path) {
2954
2955 llvm::Function* runtime_func = NULL;
2956
2957 switch (invoke_type) {
2958 case kStatic:
2959 runtime_func = irb_.GetRuntime(FindStaticMethodWithAccessCheck);
2960 break;
2961
2962 case kDirect:
2963 runtime_func = irb_.GetRuntime(FindDirectMethodWithAccessCheck);
2964 break;
2965
2966 case kVirtual:
2967 runtime_func = irb_.GetRuntime(FindVirtualMethodWithAccessCheck);
2968 break;
2969
2970 case kSuper:
2971 runtime_func = irb_.GetRuntime(FindSuperMethodWithAccessCheck);
2972 break;
2973
2974 case kInterface:
2975 if (is_fast_path) {
2976 runtime_func = irb_.GetRuntime(FindInterfaceMethod);
2977 } else {
2978 runtime_func = irb_.GetRuntime(FindInterfaceMethodWithAccessCheck);
2979 }
2980 break;
2981 }
Logan Chien7caf37e2012-02-03 22:56:04 +08002982
2983 llvm::Value* callee_method_idx_value = irb_.getInt32(callee_method_idx);
2984
Logan Chien7e7fabc2012-04-10 18:59:11 +08002985 if (this_addr == NULL) {
2986 DCHECK_EQ(invoke_type, kStatic);
2987 this_addr = irb_.getJNull();
2988 }
2989
2990 llvm::Value* caller_method_object_addr = EmitLoadMethodObjectAddr();
2991
TDYa127da83d972012-04-18 00:21:49 -07002992 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
2993
TDYa127c8dc1012012-04-19 07:03:33 -07002994 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002995
TDYa1270b686e52012-04-09 22:43:35 -07002996 llvm::Value* callee_method_object_addr =
TDYa127da83d972012-04-18 00:21:49 -07002997 irb_.CreateCall4(runtime_func,
Logan Chien7e7fabc2012-04-10 18:59:11 +08002998 callee_method_idx_value,
2999 this_addr,
TDYa127da83d972012-04-18 00:21:49 -07003000 caller_method_object_addr,
3001 thread_object_addr);
Logan Chien7caf37e2012-02-03 22:56:04 +08003002
3003 EmitGuard_ExceptionLandingPad(dex_pc);
3004
Logan Chien7e7fabc2012-04-10 18:59:11 +08003005 return callee_method_object_addr;
Logan Chien70f94b42011-12-27 17:49:11 +08003006}
3007
3008
3009void MethodCompiler::EmitInsn_Neg(uint32_t dex_pc,
3010 Instruction const* insn,
3011 JType op_jty) {
Logan Chien1b5685f2011-12-27 18:01:14 +08003012
Elliott Hughesadb8c672012-03-06 16:49:32 -08003013 DecodedInstruction dec_insn(insn);
Logan Chien1b5685f2011-12-27 18:01:14 +08003014
3015 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3016
Elliott Hughesadb8c672012-03-06 16:49:32 -08003017 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chien1b5685f2011-12-27 18:01:14 +08003018 llvm::Value* result_value = irb_.CreateNeg(src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003019 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chien1b5685f2011-12-27 18:01:14 +08003020
Logan Chien70f94b42011-12-27 17:49:11 +08003021 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3022}
3023
3024
3025void MethodCompiler::EmitInsn_Not(uint32_t dex_pc,
3026 Instruction const* insn,
3027 JType op_jty) {
Logan Chiene53750d2011-12-27 18:02:27 +08003028
Elliott Hughesadb8c672012-03-06 16:49:32 -08003029 DecodedInstruction dec_insn(insn);
Logan Chiene53750d2011-12-27 18:02:27 +08003030
3031 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3032
Elliott Hughesadb8c672012-03-06 16:49:32 -08003033 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chiene53750d2011-12-27 18:02:27 +08003034 llvm::Value* result_value =
3035 irb_.CreateXor(src_value, static_cast<uint64_t>(-1));
3036
Elliott Hughesadb8c672012-03-06 16:49:32 -08003037 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chiene53750d2011-12-27 18:02:27 +08003038
Logan Chien70f94b42011-12-27 17:49:11 +08003039 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3040}
3041
3042
3043void MethodCompiler::EmitInsn_SExt(uint32_t dex_pc,
3044 Instruction const* insn) {
Logan Chien61752ad2011-12-27 18:03:51 +08003045
Elliott Hughesadb8c672012-03-06 16:49:32 -08003046 DecodedInstruction dec_insn(insn);
Logan Chien61752ad2011-12-27 18:03:51 +08003047
Elliott Hughesadb8c672012-03-06 16:49:32 -08003048 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chien61752ad2011-12-27 18:03:51 +08003049 llvm::Value* result_value = irb_.CreateSExt(src_value, irb_.getJLongTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003050 EmitStoreDalvikReg(dec_insn.vA, kLong, kAccurate, result_value);
Logan Chien61752ad2011-12-27 18:03:51 +08003051
Logan Chien70f94b42011-12-27 17:49:11 +08003052 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3053}
3054
3055
3056void MethodCompiler::EmitInsn_Trunc(uint32_t dex_pc,
3057 Instruction const* insn) {
Logan Chien17a57662011-12-27 18:05:14 +08003058
Elliott Hughesadb8c672012-03-06 16:49:32 -08003059 DecodedInstruction dec_insn(insn);
Logan Chien17a57662011-12-27 18:05:14 +08003060
Elliott Hughesadb8c672012-03-06 16:49:32 -08003061 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kLong, kAccurate);
Logan Chien17a57662011-12-27 18:05:14 +08003062 llvm::Value* result_value = irb_.CreateTrunc(src_value, irb_.getJIntTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003063 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chien17a57662011-12-27 18:05:14 +08003064
Logan Chien70f94b42011-12-27 17:49:11 +08003065 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3066}
3067
3068
3069void MethodCompiler::EmitInsn_TruncAndSExt(uint32_t dex_pc,
3070 Instruction const* insn,
3071 unsigned N) {
Logan Chienb6744c52011-12-27 18:06:26 +08003072
Elliott Hughesadb8c672012-03-06 16:49:32 -08003073 DecodedInstruction dec_insn(insn);
Logan Chienb6744c52011-12-27 18:06:26 +08003074
Elliott Hughesadb8c672012-03-06 16:49:32 -08003075 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chienb6744c52011-12-27 18:06:26 +08003076
3077 llvm::Value* trunc_value =
3078 irb_.CreateTrunc(src_value, llvm::Type::getIntNTy(*context_, N));
3079
3080 llvm::Value* result_value = irb_.CreateSExt(trunc_value, irb_.getJIntTy());
3081
Elliott Hughesadb8c672012-03-06 16:49:32 -08003082 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chienb6744c52011-12-27 18:06:26 +08003083
Logan Chien70f94b42011-12-27 17:49:11 +08003084 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3085}
3086
3087
3088void MethodCompiler::EmitInsn_TruncAndZExt(uint32_t dex_pc,
3089 Instruction const* insn,
3090 unsigned N) {
Logan Chienb6744c52011-12-27 18:06:26 +08003091
Elliott Hughesadb8c672012-03-06 16:49:32 -08003092 DecodedInstruction dec_insn(insn);
Logan Chienb6744c52011-12-27 18:06:26 +08003093
Elliott Hughesadb8c672012-03-06 16:49:32 -08003094 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chienb6744c52011-12-27 18:06:26 +08003095
3096 llvm::Value* trunc_value =
3097 irb_.CreateTrunc(src_value, llvm::Type::getIntNTy(*context_, N));
3098
3099 llvm::Value* result_value = irb_.CreateZExt(trunc_value, irb_.getJIntTy());
3100
Elliott Hughesadb8c672012-03-06 16:49:32 -08003101 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chienb6744c52011-12-27 18:06:26 +08003102
Logan Chien70f94b42011-12-27 17:49:11 +08003103 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3104}
3105
3106
3107void MethodCompiler::EmitInsn_FNeg(uint32_t dex_pc,
3108 Instruction const* insn,
3109 JType op_jty) {
Logan Chien7a48b092011-12-27 18:07:45 +08003110
Elliott Hughesadb8c672012-03-06 16:49:32 -08003111 DecodedInstruction dec_insn(insn);
Logan Chien7a48b092011-12-27 18:07:45 +08003112
3113 DCHECK(op_jty == kFloat || op_jty == kDouble) << op_jty;
3114
Elliott Hughesadb8c672012-03-06 16:49:32 -08003115 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chien7a48b092011-12-27 18:07:45 +08003116 llvm::Value* result_value = irb_.CreateFNeg(src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003117 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chien7a48b092011-12-27 18:07:45 +08003118
Logan Chien70f94b42011-12-27 17:49:11 +08003119 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3120}
3121
3122
3123void MethodCompiler::EmitInsn_IntToFP(uint32_t dex_pc,
3124 Instruction const* insn,
3125 JType src_jty,
3126 JType dest_jty) {
Logan Chien62dd4532011-12-27 18:09:00 +08003127
Elliott Hughesadb8c672012-03-06 16:49:32 -08003128 DecodedInstruction dec_insn(insn);
Logan Chien62dd4532011-12-27 18:09:00 +08003129
3130 DCHECK(src_jty == kInt || src_jty == kLong) << src_jty;
3131 DCHECK(dest_jty == kFloat || dest_jty == kDouble) << dest_jty;
3132
Elliott Hughesadb8c672012-03-06 16:49:32 -08003133 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, src_jty, kAccurate);
Logan Chien62dd4532011-12-27 18:09:00 +08003134 llvm::Type* dest_type = irb_.getJType(dest_jty, kAccurate);
3135 llvm::Value* dest_value = irb_.CreateSIToFP(src_value, dest_type);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003136 EmitStoreDalvikReg(dec_insn.vA, dest_jty, kAccurate, dest_value);
Logan Chien62dd4532011-12-27 18:09:00 +08003137
Logan Chien70f94b42011-12-27 17:49:11 +08003138 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3139}
3140
3141
3142void MethodCompiler::EmitInsn_FPToInt(uint32_t dex_pc,
3143 Instruction const* insn,
3144 JType src_jty,
TDYa127a4746872012-04-11 23:48:55 -07003145 JType dest_jty,
3146 runtime_support::RuntimeId runtime_func_id) {
Logan Chien12dc1752011-12-27 18:10:15 +08003147
Elliott Hughesadb8c672012-03-06 16:49:32 -08003148 DecodedInstruction dec_insn(insn);
Logan Chien12dc1752011-12-27 18:10:15 +08003149
3150 DCHECK(src_jty == kFloat || src_jty == kDouble) << src_jty;
3151 DCHECK(dest_jty == kInt || dest_jty == kLong) << dest_jty;
3152
Elliott Hughesadb8c672012-03-06 16:49:32 -08003153 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, src_jty, kAccurate);
TDYa127a4746872012-04-11 23:48:55 -07003154 llvm::Value* dest_value = irb_.CreateCall(irb_.GetRuntime(runtime_func_id), src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003155 EmitStoreDalvikReg(dec_insn.vA, dest_jty, kAccurate, dest_value);
Logan Chien12dc1752011-12-27 18:10:15 +08003156
Logan Chien70f94b42011-12-27 17:49:11 +08003157 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3158}
3159
3160
3161void MethodCompiler::EmitInsn_FExt(uint32_t dex_pc,
3162 Instruction const* insn) {
Logan Chienc56ded92011-12-27 18:10:57 +08003163
Elliott Hughesadb8c672012-03-06 16:49:32 -08003164 DecodedInstruction dec_insn(insn);
Logan Chienc56ded92011-12-27 18:10:57 +08003165
Elliott Hughesadb8c672012-03-06 16:49:32 -08003166 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kFloat, kAccurate);
Logan Chienc56ded92011-12-27 18:10:57 +08003167 llvm::Value* result_value = irb_.CreateFPExt(src_value, irb_.getJDoubleTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003168 EmitStoreDalvikReg(dec_insn.vA, kDouble, kAccurate, result_value);
Logan Chienc56ded92011-12-27 18:10:57 +08003169
Logan Chien70f94b42011-12-27 17:49:11 +08003170 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3171}
3172
3173
3174void MethodCompiler::EmitInsn_FTrunc(uint32_t dex_pc,
3175 Instruction const* insn) {
Logan Chien927744f2011-12-27 18:11:52 +08003176
Elliott Hughesadb8c672012-03-06 16:49:32 -08003177 DecodedInstruction dec_insn(insn);
Logan Chien927744f2011-12-27 18:11:52 +08003178
Elliott Hughesadb8c672012-03-06 16:49:32 -08003179 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kDouble, kAccurate);
Logan Chien927744f2011-12-27 18:11:52 +08003180 llvm::Value* result_value = irb_.CreateFPTrunc(src_value, irb_.getJFloatTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003181 EmitStoreDalvikReg(dec_insn.vA, kFloat, kAccurate, result_value);
Logan Chien927744f2011-12-27 18:11:52 +08003182
Logan Chien70f94b42011-12-27 17:49:11 +08003183 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3184}
3185
3186
3187void MethodCompiler::EmitInsn_IntArithm(uint32_t dex_pc,
3188 Instruction const* insn,
3189 IntArithmKind arithm,
3190 JType op_jty,
3191 bool is_2addr) {
Logan Chienc3f7d962011-12-27 18:13:18 +08003192
Elliott Hughesadb8c672012-03-06 16:49:32 -08003193 DecodedInstruction dec_insn(insn);
Logan Chienc3f7d962011-12-27 18:13:18 +08003194
3195 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3196
3197 llvm::Value* src1_value;
3198 llvm::Value* src2_value;
3199
3200 if (is_2addr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003201 src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate);
3202 src2_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chienc3f7d962011-12-27 18:13:18 +08003203 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003204 src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
3205 src2_value = EmitLoadDalvikReg(dec_insn.vC, op_jty, kAccurate);
Logan Chienc3f7d962011-12-27 18:13:18 +08003206 }
3207
3208 llvm::Value* result_value =
3209 EmitIntArithmResultComputation(dex_pc, src1_value, src2_value,
3210 arithm, op_jty);
3211
Elliott Hughesadb8c672012-03-06 16:49:32 -08003212 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chienc3f7d962011-12-27 18:13:18 +08003213
Logan Chien70f94b42011-12-27 17:49:11 +08003214 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3215}
3216
3217
3218void MethodCompiler::EmitInsn_IntArithmImmediate(uint32_t dex_pc,
3219 Instruction const* insn,
3220 IntArithmKind arithm) {
Logan Chienc3f7d962011-12-27 18:13:18 +08003221
Elliott Hughesadb8c672012-03-06 16:49:32 -08003222 DecodedInstruction dec_insn(insn);
Logan Chienc3f7d962011-12-27 18:13:18 +08003223
Elliott Hughesadb8c672012-03-06 16:49:32 -08003224 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chienc3f7d962011-12-27 18:13:18 +08003225
Elliott Hughesadb8c672012-03-06 16:49:32 -08003226 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC);
Logan Chienc3f7d962011-12-27 18:13:18 +08003227
3228 llvm::Value* result_value =
3229 EmitIntArithmResultComputation(dex_pc, src_value, imm_value, arithm, kInt);
3230
Elliott Hughesadb8c672012-03-06 16:49:32 -08003231 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chienc3f7d962011-12-27 18:13:18 +08003232
Logan Chien70f94b42011-12-27 17:49:11 +08003233 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3234}
3235
3236
Logan Chienc3f7d962011-12-27 18:13:18 +08003237llvm::Value*
3238MethodCompiler::EmitIntArithmResultComputation(uint32_t dex_pc,
3239 llvm::Value* lhs,
3240 llvm::Value* rhs,
3241 IntArithmKind arithm,
3242 JType op_jty) {
3243 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3244
3245 switch (arithm) {
3246 case kIntArithm_Add:
3247 return irb_.CreateAdd(lhs, rhs);
3248
3249 case kIntArithm_Sub:
3250 return irb_.CreateSub(lhs, rhs);
3251
3252 case kIntArithm_Mul:
3253 return irb_.CreateMul(lhs, rhs);
3254
3255 case kIntArithm_Div:
Logan Chienc3f7d962011-12-27 18:13:18 +08003256 case kIntArithm_Rem:
TDYa127f8641ce2012-04-02 06:40:40 -07003257 return EmitIntDivRemResultComputation(dex_pc, lhs, rhs, arithm, op_jty);
Logan Chienc3f7d962011-12-27 18:13:18 +08003258
3259 case kIntArithm_And:
3260 return irb_.CreateAnd(lhs, rhs);
3261
3262 case kIntArithm_Or:
3263 return irb_.CreateOr(lhs, rhs);
3264
3265 case kIntArithm_Xor:
3266 return irb_.CreateXor(lhs, rhs);
3267
Logan Chienc3f7d962011-12-27 18:13:18 +08003268 default:
3269 LOG(FATAL) << "Unknown integer arithmetic kind: " << arithm;
3270 return NULL;
3271 }
3272}
3273
3274
TDYa127f8641ce2012-04-02 06:40:40 -07003275llvm::Value*
3276MethodCompiler::EmitIntDivRemResultComputation(uint32_t dex_pc,
3277 llvm::Value* dividend,
3278 llvm::Value* divisor,
3279 IntArithmKind arithm,
3280 JType op_jty) {
3281 // Throw exception if the divisor is 0.
3282 EmitGuard_DivZeroException(dex_pc, divisor, op_jty);
3283
3284 // Check the special case: MININT / -1 = MININT
3285 // That case will cause overflow, which is undefined behavior in llvm.
3286 // So we check the divisor is -1 or not, if the divisor is -1, we do
3287 // the special path to avoid undefined behavior.
3288 llvm::Type* op_type = irb_.getJType(op_jty, kAccurate);
3289 llvm::Value* zero = irb_.getJZero(op_jty);
3290 llvm::Value* neg_one = llvm::ConstantInt::getSigned(op_type, -1);
3291 llvm::Value* result = irb_.CreateAlloca(op_type);
3292
3293 llvm::BasicBlock* eq_neg_one = CreateBasicBlockWithDexPC(dex_pc, "eq_neg_one");
3294 llvm::BasicBlock* ne_neg_one = CreateBasicBlockWithDexPC(dex_pc, "ne_neg_one");
3295 llvm::BasicBlock* neg_one_cont = CreateBasicBlockWithDexPC(dex_pc, "neg_one_cont");
3296
3297 llvm::Value* is_equal_neg_one = EmitConditionResult(divisor, neg_one, kCondBranch_EQ);
3298 irb_.CreateCondBr(is_equal_neg_one, eq_neg_one, ne_neg_one);
3299
3300 // If divisor == -1
3301 irb_.SetInsertPoint(eq_neg_one);
3302 llvm::Value* eq_result;
3303 if (arithm == kIntArithm_Div) {
3304 // We can just change from "dividend div -1" to "neg dividend".
3305 // The sub don't care the sign/unsigned because of two's complement representation.
3306 // And the behavior is what we want:
3307 // -(2^n) (2^n)-1
3308 // MININT < k <= MAXINT -> mul k -1 = -k
3309 // MININT == k -> mul k -1 = k
3310 //
3311 // LLVM use sub to represent 'neg'
3312 eq_result = irb_.CreateSub(zero, dividend);
3313 } else {
3314 // Everything modulo -1 will be 0.
3315 eq_result = zero;
3316 }
3317 irb_.CreateStore(eq_result, result);
3318 irb_.CreateBr(neg_one_cont);
3319
3320 // If divisor != -1, just do the division.
3321 irb_.SetInsertPoint(ne_neg_one);
3322 llvm::Value* ne_result;
3323 if (arithm == kIntArithm_Div) {
3324 ne_result = irb_.CreateSDiv(dividend, divisor);
3325 } else {
3326 ne_result = irb_.CreateSRem(dividend, divisor);
3327 }
3328 irb_.CreateStore(ne_result, result);
3329 irb_.CreateBr(neg_one_cont);
3330
3331 irb_.SetInsertPoint(neg_one_cont);
3332 return irb_.CreateLoad(result);
3333}
3334
3335
Logan Chien5539ad02012-04-02 14:36:55 +08003336void MethodCompiler::EmitInsn_IntShiftArithm(uint32_t dex_pc,
3337 Instruction const* insn,
3338 IntShiftArithmKind arithm,
3339 JType op_jty,
3340 bool is_2addr) {
3341
3342 DecodedInstruction dec_insn(insn);
3343
3344 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3345
3346 llvm::Value* src1_value;
3347 llvm::Value* src2_value;
3348
3349 // NOTE: The 2nd operand of the shift arithmetic instruction is
3350 // 32-bit integer regardless of the 1st operand.
3351 if (is_2addr) {
3352 src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate);
3353 src2_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
3354 } else {
3355 src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
3356 src2_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate);
3357 }
3358
3359 llvm::Value* result_value =
3360 EmitIntShiftArithmResultComputation(dex_pc, src1_value, src2_value,
3361 arithm, op_jty);
3362
3363 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
3364
3365 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3366}
3367
3368
3369void MethodCompiler::
3370EmitInsn_IntShiftArithmImmediate(uint32_t dex_pc,
3371 Instruction const* insn,
3372 IntShiftArithmKind arithm) {
3373
3374 DecodedInstruction dec_insn(insn);
3375
3376 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
3377
3378 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC);
3379
3380 llvm::Value* result_value =
3381 EmitIntShiftArithmResultComputation(dex_pc, src_value, imm_value,
3382 arithm, kInt);
3383
3384 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
3385
3386 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3387}
3388
3389
3390llvm::Value*
3391MethodCompiler::EmitIntShiftArithmResultComputation(uint32_t dex_pc,
3392 llvm::Value* lhs,
3393 llvm::Value* rhs,
3394 IntShiftArithmKind arithm,
3395 JType op_jty) {
3396 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3397
3398 if (op_jty == kInt) {
3399 rhs = irb_.CreateAnd(rhs, 0x1f);
3400 } else {
3401 llvm::Value* masked_rhs = irb_.CreateAnd(rhs, 0x3f);
3402 rhs = irb_.CreateZExt(masked_rhs, irb_.getJLongTy());
3403 }
3404
3405 switch (arithm) {
3406 case kIntArithm_Shl:
3407 return irb_.CreateShl(lhs, rhs);
3408
3409 case kIntArithm_Shr:
3410 return irb_.CreateAShr(lhs, rhs);
3411
3412 case kIntArithm_UShr:
3413 return irb_.CreateLShr(lhs, rhs);
3414
3415 default:
3416 LOG(FATAL) << "Unknown integer shift arithmetic kind: " << arithm;
3417 return NULL;
3418 }
3419}
3420
3421
Logan Chien70f94b42011-12-27 17:49:11 +08003422void MethodCompiler::EmitInsn_RSubImmediate(uint32_t dex_pc,
3423 Instruction const* insn) {
Logan Chien65c62d42011-12-27 18:14:18 +08003424
Elliott Hughesadb8c672012-03-06 16:49:32 -08003425 DecodedInstruction dec_insn(insn);
Logan Chien65c62d42011-12-27 18:14:18 +08003426
Elliott Hughesadb8c672012-03-06 16:49:32 -08003427 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
3428 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC);
Logan Chien65c62d42011-12-27 18:14:18 +08003429 llvm::Value* result_value = irb_.CreateSub(imm_value, src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003430 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chien65c62d42011-12-27 18:14:18 +08003431
Logan Chien70f94b42011-12-27 17:49:11 +08003432 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3433}
3434
3435
3436void MethodCompiler::EmitInsn_FPArithm(uint32_t dex_pc,
3437 Instruction const* insn,
3438 FPArithmKind arithm,
3439 JType op_jty,
3440 bool is_2addr) {
Logan Chien76e1c792011-12-27 18:15:01 +08003441
Elliott Hughesadb8c672012-03-06 16:49:32 -08003442 DecodedInstruction dec_insn(insn);
Logan Chien76e1c792011-12-27 18:15:01 +08003443
3444 DCHECK(op_jty == kFloat || op_jty == kDouble) << op_jty;
3445
3446 llvm::Value* src1_value;
3447 llvm::Value* src2_value;
3448
3449 if (is_2addr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003450 src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate);
3451 src2_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chien76e1c792011-12-27 18:15:01 +08003452 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003453 src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
3454 src2_value = EmitLoadDalvikReg(dec_insn.vC, op_jty, kAccurate);
Logan Chien76e1c792011-12-27 18:15:01 +08003455 }
3456
3457 llvm::Value* result_value =
3458 EmitFPArithmResultComputation(dex_pc, src1_value, src2_value, arithm);
3459
Elliott Hughesadb8c672012-03-06 16:49:32 -08003460 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chien76e1c792011-12-27 18:15:01 +08003461
Logan Chien70f94b42011-12-27 17:49:11 +08003462 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3463}
3464
3465
Logan Chien76e1c792011-12-27 18:15:01 +08003466llvm::Value*
3467MethodCompiler::EmitFPArithmResultComputation(uint32_t dex_pc,
3468 llvm::Value *lhs,
3469 llvm::Value *rhs,
3470 FPArithmKind arithm) {
3471 switch (arithm) {
3472 case kFPArithm_Add:
3473 return irb_.CreateFAdd(lhs, rhs);
3474
3475 case kFPArithm_Sub:
3476 return irb_.CreateFSub(lhs, rhs);
3477
3478 case kFPArithm_Mul:
3479 return irb_.CreateFMul(lhs, rhs);
3480
3481 case kFPArithm_Div:
3482 return irb_.CreateFDiv(lhs, rhs);
3483
3484 case kFPArithm_Rem:
3485 return irb_.CreateFRem(lhs, rhs);
3486
3487 default:
3488 LOG(FATAL) << "Unknown floating-point arithmetic kind: " << arithm;
3489 return NULL;
3490 }
3491}
3492
3493
Logan Chienc3f7d962011-12-27 18:13:18 +08003494void MethodCompiler::EmitGuard_DivZeroException(uint32_t dex_pc,
3495 llvm::Value* denominator,
3496 JType op_jty) {
3497 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3498
3499 llvm::Constant* zero = irb_.getJZero(op_jty);
3500
3501 llvm::Value* equal_zero = irb_.CreateICmpEQ(denominator, zero);
3502
3503 llvm::BasicBlock* block_exception = CreateBasicBlockWithDexPC(dex_pc, "div0");
3504
3505 llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont");
3506
3507 irb_.CreateCondBr(equal_zero, block_exception, block_continue);
3508
3509 irb_.SetInsertPoint(block_exception);
TDYa127c8dc1012012-04-19 07:03:33 -07003510 EmitUpdateDexPC(dex_pc);
Logan Chienc3f7d962011-12-27 18:13:18 +08003511 irb_.CreateCall(irb_.GetRuntime(ThrowDivZeroException));
3512 EmitBranchExceptionLandingPad(dex_pc);
3513
3514 irb_.SetInsertPoint(block_continue);
3515}
3516
3517
Logan Chien61bb6142012-02-03 15:34:53 +08003518void MethodCompiler::EmitGuard_NullPointerException(uint32_t dex_pc,
3519 llvm::Value* object) {
3520 llvm::Value* equal_null = irb_.CreateICmpEQ(object, irb_.getJNull());
3521
3522 llvm::BasicBlock* block_exception =
3523 CreateBasicBlockWithDexPC(dex_pc, "nullp");
3524
3525 llvm::BasicBlock* block_continue =
3526 CreateBasicBlockWithDexPC(dex_pc, "cont");
3527
3528 irb_.CreateCondBr(equal_null, block_exception, block_continue);
3529
3530 irb_.SetInsertPoint(block_exception);
TDYa127c8dc1012012-04-19 07:03:33 -07003531 EmitUpdateDexPC(dex_pc);
TDYa1273f9137d2012-04-08 15:59:19 -07003532 irb_.CreateCall(irb_.GetRuntime(ThrowNullPointerException), irb_.getInt32(dex_pc));
Logan Chien61bb6142012-02-03 15:34:53 +08003533 EmitBranchExceptionLandingPad(dex_pc);
3534
3535 irb_.SetInsertPoint(block_continue);
3536}
3537
3538
Logan Chienbb4d12a2012-02-17 14:10:01 +08003539llvm::Value* MethodCompiler::EmitLoadDexCacheAddr(MemberOffset offset) {
3540 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
3541
TDYa127ee1f59b2012-04-25 00:56:40 -07003542 return irb_.LoadFromObjectOffset(method_object_addr,
3543 offset.Int32Value(),
3544 irb_.getJObjectTy());
Logan Chienbb4d12a2012-02-17 14:10:01 +08003545}
3546
3547
Logan Chienbb4d12a2012-02-17 14:10:01 +08003548llvm::Value* MethodCompiler::
3549EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx) {
3550 llvm::Value* static_storage_dex_cache_addr =
3551 EmitLoadDexCacheAddr(Method::DexCacheInitializedStaticStorageOffset());
3552
3553 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
3554
3555 return EmitArrayGEP(static_storage_dex_cache_addr, type_idx_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08003556 irb_.getJObjectTy(), kObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003557}
3558
3559
3560llvm::Value* MethodCompiler::
3561EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx) {
3562 llvm::Value* resolved_type_dex_cache_addr =
3563 EmitLoadDexCacheAddr(Method::DexCacheResolvedTypesOffset());
3564
3565 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
3566
3567 return EmitArrayGEP(resolved_type_dex_cache_addr, type_idx_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08003568 irb_.getJObjectTy(), kObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003569}
3570
3571
3572llvm::Value* MethodCompiler::
Logan Chien61c65dc2012-02-29 03:22:30 +08003573EmitLoadDexCacheResolvedMethodFieldAddr(uint32_t method_idx) {
3574 llvm::Value* resolved_method_dex_cache_addr =
3575 EmitLoadDexCacheAddr(Method::DexCacheResolvedMethodsOffset());
3576
3577 llvm::Value* method_idx_value = irb_.getPtrEquivInt(method_idx);
3578
3579 return EmitArrayGEP(resolved_method_dex_cache_addr, method_idx_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08003580 irb_.getJObjectTy(), kObject);
Logan Chien61c65dc2012-02-29 03:22:30 +08003581}
3582
3583
3584llvm::Value* MethodCompiler::
Logan Chienbb4d12a2012-02-17 14:10:01 +08003585EmitLoadDexCacheStringFieldAddr(uint32_t string_idx) {
3586 llvm::Value* string_dex_cache_addr =
3587 EmitLoadDexCacheAddr(Method::DexCacheStringsOffset());
3588
3589 llvm::Value* string_idx_value = irb_.getPtrEquivInt(string_idx);
3590
3591 return EmitArrayGEP(string_dex_cache_addr, string_idx_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08003592 irb_.getJObjectTy(), kObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003593}
3594
3595
Logan Chien83426162011-12-09 09:29:50 +08003596CompiledMethod *MethodCompiler::Compile() {
Logan Chien0b827102011-12-20 19:46:14 +08003597 // Code generation
3598 CreateFunction();
3599
3600 EmitPrologue();
3601 EmitInstructions();
Logan Chienc670a8d2011-12-20 21:25:56 +08003602 EmitPrologueLastBranch();
Logan Chien0b827102011-12-20 19:46:14 +08003603
Logan Chiend6c239a2011-12-23 15:11:45 +08003604 // Verify the generated bitcode
TDYa127853cd092012-04-21 22:15:31 -07003605 VERIFY_LLVM_FUNCTION(*func_);
Logan Chiend6c239a2011-12-23 15:11:45 +08003606
Logan Chien8b977d32012-02-21 19:14:55 +08003607 // Add the memory usage approximation of the compilation unit
3608 cunit_->AddMemUsageApproximation(code_item_->insns_size_in_code_units_ * 900);
3609 // NOTE: From statistic, the bitcode size is 4.5 times bigger than the
3610 // Dex file. Besides, we have to convert the code unit into bytes.
3611 // Thus, we got our magic number 9.
3612
Logan Chien110bcba2012-04-16 19:11:28 +08003613 CompiledMethod* compiled_method =
3614 new CompiledMethod(cunit_->GetInstructionSet(),
3615 cunit_->GetElfIndex(),
3616 elf_func_idx_);
3617
3618 cunit_->RegisterCompiledMethod(func_, compiled_method);
3619
3620 return compiled_method;
Logan Chien0b827102011-12-20 19:46:14 +08003621}
3622
3623
3624llvm::Value* MethodCompiler::EmitLoadMethodObjectAddr() {
3625 return func_->arg_begin();
Shih-wei Liaod1fec812012-02-13 09:51:10 -08003626}
Logan Chien83426162011-12-09 09:29:50 +08003627
3628
Logan Chien5bcc04e2012-01-30 14:15:12 +08003629void MethodCompiler::EmitBranchExceptionLandingPad(uint32_t dex_pc) {
3630 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
3631 irb_.CreateBr(lpad);
3632 } else {
3633 irb_.CreateBr(GetUnwindBasicBlock());
3634 }
3635}
3636
3637
3638void MethodCompiler::EmitGuard_ExceptionLandingPad(uint32_t dex_pc) {
3639 llvm::Value* exception_pending =
3640 irb_.CreateCall(irb_.GetRuntime(IsExceptionPending));
3641
3642 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
3643
3644 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
3645 irb_.CreateCondBr(exception_pending, lpad, block_cont);
3646 } else {
3647 irb_.CreateCondBr(exception_pending, GetUnwindBasicBlock(), block_cont);
3648 }
3649
3650 irb_.SetInsertPoint(block_cont);
3651}
3652
3653
Logan Chien924072f2012-01-30 15:07:24 +08003654void MethodCompiler::EmitGuard_GarbageCollectionSuspend(uint32_t dex_pc) {
3655 llvm::Value* runtime_func = irb_.GetRuntime(TestSuspend);
TDYa127853cd092012-04-21 22:15:31 -07003656
3657 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
3658
TDYa127c8dc1012012-04-19 07:03:33 -07003659 EmitUpdateDexPC(dex_pc);
TDYa127853cd092012-04-21 22:15:31 -07003660
3661 irb_.CreateCall(runtime_func, thread_object_addr);
Logan Chien924072f2012-01-30 15:07:24 +08003662}
3663
3664
Logan Chiend6c239a2011-12-23 15:11:45 +08003665llvm::BasicBlock* MethodCompiler::
3666CreateBasicBlockWithDexPC(uint32_t dex_pc, char const* postfix) {
3667 std::string name;
3668
3669 if (postfix) {
TDYa12799489132012-04-29 01:27:58 -07003670 StringAppendF(&name, "B%04x.%s", dex_pc, postfix);
Logan Chiend6c239a2011-12-23 15:11:45 +08003671 } else {
TDYa12799489132012-04-29 01:27:58 -07003672 StringAppendF(&name, "B%04x", dex_pc);
Logan Chiend6c239a2011-12-23 15:11:45 +08003673 }
3674
3675 return llvm::BasicBlock::Create(*context_, name, func_);
3676}
3677
3678
3679llvm::BasicBlock* MethodCompiler::GetBasicBlock(uint32_t dex_pc) {
3680 DCHECK(dex_pc < code_item_->insns_size_in_code_units_);
3681
3682 llvm::BasicBlock* basic_block = basic_blocks_[dex_pc];
3683
3684 if (!basic_block) {
3685 basic_block = CreateBasicBlockWithDexPC(dex_pc);
3686 basic_blocks_[dex_pc] = basic_block;
3687 }
3688
3689 return basic_block;
3690}
3691
3692
3693llvm::BasicBlock*
3694MethodCompiler::GetNextBasicBlock(uint32_t dex_pc) {
3695 Instruction const* insn = Instruction::At(code_item_->insns_ + dex_pc);
3696 return GetBasicBlock(dex_pc + insn->SizeInCodeUnits());
3697}
3698
3699
Logan Chien5bcc04e2012-01-30 14:15:12 +08003700int32_t MethodCompiler::GetTryItemOffset(uint32_t dex_pc) {
3701 // TODO: Since we are emitting the dex instructions in ascending order
3702 // w.r.t. address, we can cache the lastest try item offset so that we
3703 // don't have to do binary search for every query.
3704
3705 int32_t min = 0;
3706 int32_t max = code_item_->tries_size_ - 1;
3707
3708 while (min <= max) {
3709 int32_t mid = min + (max - min) / 2;
3710
3711 DexFile::TryItem const* ti = DexFile::GetTryItems(*code_item_, mid);
3712 uint32_t start = ti->start_addr_;
3713 uint32_t end = start + ti->insn_count_;
3714
3715 if (dex_pc < start) {
3716 max = mid - 1;
3717 } else if (dex_pc >= end) {
3718 min = mid + 1;
3719 } else {
3720 return mid; // found
3721 }
3722 }
3723
3724 return -1; // not found
3725}
3726
3727
3728llvm::BasicBlock* MethodCompiler::GetLandingPadBasicBlock(uint32_t dex_pc) {
3729 // Find the try item for this address in this method
3730 int32_t ti_offset = GetTryItemOffset(dex_pc);
3731
3732 if (ti_offset == -1) {
3733 return NULL; // No landing pad is available for this address.
3734 }
3735
3736 // Check for the existing landing pad basic block
3737 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
3738 llvm::BasicBlock* block_lpad = basic_block_landing_pads_[ti_offset];
3739
3740 if (block_lpad) {
3741 // We have generated landing pad for this try item already. Return the
3742 // same basic block.
3743 return block_lpad;
3744 }
3745
3746 // Get try item from code item
3747 DexFile::TryItem const* ti = DexFile::GetTryItems(*code_item_, ti_offset);
3748
3749 // Create landing pad basic block
3750 block_lpad = llvm::BasicBlock::Create(*context_,
3751 StringPrintf("lpad%d", ti_offset),
3752 func_);
3753
3754 // Change IRBuilder insert point
3755 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3756 irb_.SetInsertPoint(block_lpad);
3757
3758 // Find catch block with matching type
3759 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
3760
Logan Chien736df022012-04-27 16:25:57 +08003761 llvm::Value* ti_offset_value = irb_.getInt32(ti_offset);
Logan Chien5bcc04e2012-01-30 14:15:12 +08003762
3763 llvm::Value* catch_handler_index_value =
3764 irb_.CreateCall2(irb_.GetRuntime(FindCatchBlock),
Logan Chien736df022012-04-27 16:25:57 +08003765 method_object_addr, ti_offset_value);
Logan Chien5bcc04e2012-01-30 14:15:12 +08003766
3767 // Switch instruction (Go to unwind basic block by default)
3768 llvm::SwitchInst* sw =
3769 irb_.CreateSwitch(catch_handler_index_value, GetUnwindBasicBlock());
3770
3771 // Cases with matched catch block
3772 CatchHandlerIterator iter(*code_item_, ti->start_addr_);
3773
3774 for (uint32_t c = 0; iter.HasNext(); iter.Next(), ++c) {
3775 sw->addCase(irb_.getInt32(c), GetBasicBlock(iter.GetHandlerAddress()));
3776 }
3777
3778 // Restore the orignal insert point for IRBuilder
3779 irb_.restoreIP(irb_ip_original);
3780
3781 // Cache this landing pad
3782 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
3783 basic_block_landing_pads_[ti_offset] = block_lpad;
3784
3785 return block_lpad;
3786}
3787
3788
3789llvm::BasicBlock* MethodCompiler::GetUnwindBasicBlock() {
3790 // Check the existing unwinding baisc block block
3791 if (basic_block_unwind_ != NULL) {
3792 return basic_block_unwind_;
3793 }
3794
3795 // Create new basic block for unwinding
3796 basic_block_unwind_ =
3797 llvm::BasicBlock::Create(*context_, "exception_unwind", func_);
3798
3799 // Change IRBuilder insert point
3800 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3801 irb_.SetInsertPoint(basic_block_unwind_);
3802
Logan Chien8dfcbea2012-02-17 18:50:32 +08003803 // Pop the shadow frame
3804 EmitPopShadowFrame();
3805
Logan Chien5bcc04e2012-01-30 14:15:12 +08003806 // Emit the code to return default value (zero) for the given return type.
Logan Chiendd361c92012-04-10 23:40:37 +08003807 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
Logan Chien5bcc04e2012-01-30 14:15:12 +08003808 if (ret_shorty == 'V') {
3809 irb_.CreateRetVoid();
3810 } else {
3811 irb_.CreateRet(irb_.getJZero(ret_shorty));
3812 }
3813
3814 // Restore the orignal insert point for IRBuilder
3815 irb_.restoreIP(irb_ip_original);
3816
3817 return basic_block_unwind_;
3818}
3819
3820
Logan Chienc670a8d2011-12-20 21:25:56 +08003821llvm::Value* MethodCompiler::AllocDalvikLocalVarReg(RegCategory cat,
3822 uint32_t reg_idx) {
3823
3824 // Save current IR builder insert point
3825 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3826
3827 // Alloca
3828 llvm::Value* reg_addr = NULL;
3829
3830 switch (cat) {
3831 case kRegCat1nr:
3832 irb_.SetInsertPoint(basic_block_reg_alloca_);
3833 reg_addr = irb_.CreateAlloca(irb_.getJIntTy(), 0,
3834 StringPrintf("r%u", reg_idx));
Logan Chienc670a8d2011-12-20 21:25:56 +08003835 break;
3836
3837 case kRegCat2:
3838 irb_.SetInsertPoint(basic_block_reg_alloca_);
3839 reg_addr = irb_.CreateAlloca(irb_.getJLongTy(), 0,
3840 StringPrintf("w%u", reg_idx));
Logan Chienc670a8d2011-12-20 21:25:56 +08003841 break;
3842
3843 case kRegObject:
TDYa1277f5b9be2012-04-29 01:31:49 -07003844 irb_.SetInsertPoint(basic_block_reg_alloca_);
3845 reg_addr = irb_.CreateAlloca(irb_.getJObjectTy(), 0,
3846 StringPrintf("o%u", reg_idx));
Logan Chienc670a8d2011-12-20 21:25:56 +08003847 break;
3848
3849 default:
3850 LOG(FATAL) << "Unknown register category for allocation: " << cat;
3851 }
3852
3853 // Restore IRBuilder insert point
3854 irb_.restoreIP(irb_ip_original);
3855
3856 DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL));
3857 return reg_addr;
3858}
3859
3860
TDYa1277f5b9be2012-04-29 01:31:49 -07003861llvm::Value* MethodCompiler::AllocShadowFrameEntry(uint32_t reg_idx) {
3862 // Save current IR builder insert point
3863 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3864
3865 irb_.SetInsertPoint(basic_block_shadow_frame_alloca_);
3866
3867 llvm::Value* gep_index[] = {
3868 irb_.getInt32(0), // No pointer displacement
3869 irb_.getInt32(1), // SIRT
3870 irb_.getInt32(reg_idx) // Pointer field
3871 };
3872
3873 llvm::Value* reg_addr =
3874 irb_.CreateGEP(shadow_frame_, gep_index, StringPrintf("p%u", reg_idx));
3875
3876 // Restore IRBuilder insert point
3877 irb_.restoreIP(irb_ip_original);
3878
3879 DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL));
3880 return reg_addr;
3881}
3882
3883
Logan Chienc670a8d2011-12-20 21:25:56 +08003884llvm::Value* MethodCompiler::AllocDalvikRetValReg(RegCategory cat) {
3885 // Save current IR builder insert point
3886 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3887
3888 // Alloca
3889 llvm::Value* reg_addr = NULL;
3890
3891 switch (cat) {
3892 case kRegCat1nr:
3893 irb_.SetInsertPoint(basic_block_reg_alloca_);
3894 reg_addr = irb_.CreateAlloca(irb_.getJIntTy(), 0, "r_res");
3895 break;
3896
3897 case kRegCat2:
3898 irb_.SetInsertPoint(basic_block_reg_alloca_);
3899 reg_addr = irb_.CreateAlloca(irb_.getJLongTy(), 0, "w_res");
3900 break;
3901
3902 case kRegObject:
3903 irb_.SetInsertPoint(basic_block_reg_alloca_);
3904 reg_addr = irb_.CreateAlloca(irb_.getJObjectTy(), 0, "p_res");
3905 break;
3906
3907 default:
3908 LOG(FATAL) << "Unknown register category for allocation: " << cat;
3909 }
3910
3911 // Restore IRBuilder insert point
3912 irb_.restoreIP(irb_ip_original);
3913
3914 DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL));
3915 return reg_addr;
3916}
3917
3918
Logan Chien8dfcbea2012-02-17 18:50:32 +08003919void MethodCompiler::EmitPopShadowFrame() {
3920 irb_.CreateCall(irb_.GetRuntime(PopShadowFrame));
3921}
3922
3923
TDYa127c8dc1012012-04-19 07:03:33 -07003924void MethodCompiler::EmitUpdateDexPC(uint32_t dex_pc) {
3925 irb_.StoreToObjectOffset(shadow_frame_,
3926 ShadowFrame::DexPCOffset(),
3927 irb_.getInt32(dex_pc));
Logan Chien8dfcbea2012-02-17 18:50:32 +08003928}
3929
3930
Logan Chien83426162011-12-09 09:29:50 +08003931} // namespace compiler_llvm
3932} // namespace art