blob: e44481182f932338052d2ef3624e003cbb167e9f [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
TDYa1274165a832012-04-03 17:47:16 -0700142 basic_block_stack_overflow_ =
143 llvm::BasicBlock::Create(*context_, "prologue.stack_overflow_check", func_);
144
Logan Chienc670a8d2011-12-20 21:25:56 +0800145 basic_block_reg_alloca_ =
146 llvm::BasicBlock::Create(*context_, "prologue.alloca", func_);
147
Logan Chien8dfcbea2012-02-17 18:50:32 +0800148 basic_block_shadow_frame_alloca_ =
149 llvm::BasicBlock::Create(*context_, "prologue.shadowframe", func_);
150
Logan Chiend6ececa2011-12-27 16:20:15 +0800151 basic_block_reg_arg_init_ =
152 llvm::BasicBlock::Create(*context_, "prologue.arginit", func_);
153
TDYa1274165a832012-04-03 17:47:16 -0700154 // Before alloca, check stack overflow.
155 EmitStackOverflowCheck();
156
Logan Chienc670a8d2011-12-20 21:25:56 +0800157 // Create register array
158 for (uint16_t r = 0; r < code_item_->registers_size_; ++r) {
159 regs_.push_back(DalvikReg::CreateLocalVarReg(*this, r));
160 }
161
162 retval_reg_.reset(DalvikReg::CreateRetValReg(*this));
Logan Chiend6ececa2011-12-27 16:20:15 +0800163
Logan Chien8dfcbea2012-02-17 18:50:32 +0800164 // Create Shadow Frame
165 EmitPrologueAllocShadowFrame();
166
Logan Chiend6ececa2011-12-27 16:20:15 +0800167 // Store argument to dalvik register
168 irb_.SetInsertPoint(basic_block_reg_arg_init_);
169 EmitPrologueAssignArgRegister();
170
171 // Branch to start address
172 irb_.CreateBr(GetBasicBlock(0));
Logan Chienc670a8d2011-12-20 21:25:56 +0800173}
174
175
TDYa1274165a832012-04-03 17:47:16 -0700176void MethodCompiler::EmitStackOverflowCheck() {
177 irb_.SetInsertPoint(basic_block_stack_overflow_);
178
179 // Call llvm intrinsic function to get frame address.
180 llvm::Function* frameaddress =
181 llvm::Intrinsic::getDeclaration(module_, llvm::Intrinsic::frameaddress);
182
183 // The type of llvm::frameaddress is: i8* @llvm.frameaddress(i32)
184 llvm::Value* frame_address = irb_.CreateCall(frameaddress, irb_.getInt32(0));
185
186 // Cast i8* to int
187 frame_address = irb_.CreatePtrToInt(frame_address, irb_.getPtrEquivIntTy());
188
189 // Get thread.stack_end_
190 llvm::Value* thread_object_addr =
191 irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
192
193 llvm::Value* stack_end_addr =
194 irb_.CreatePtrDisp(thread_object_addr,
195 irb_.getPtrEquivInt(Thread::StackEndOffset().Int32Value()),
196 irb_.getPtrEquivIntTy()->getPointerTo());
197
198 llvm::Value* stack_end = irb_.CreateLoad(stack_end_addr);
199
200 // Check the frame address < thread.stack_end_ ?
201 llvm::Value* is_stack_overflow = irb_.CreateICmpULT(frame_address, stack_end);
202
203 llvm::BasicBlock* block_exception =
204 llvm::BasicBlock::Create(*context_, "stack_overflow", func_);
205
206 llvm::BasicBlock* block_continue =
207 llvm::BasicBlock::Create(*context_, "stack_overflow_cont", func_);
208
209 irb_.CreateCondBr(is_stack_overflow, block_exception, block_continue);
210
211 // If stack overflow, throw exception.
212 irb_.SetInsertPoint(block_exception);
213 irb_.CreateCall(irb_.GetRuntime(ThrowStackOverflowException));
214
215 // Unwind.
Logan Chiendd361c92012-04-10 23:40:37 +0800216 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
TDYa1274165a832012-04-03 17:47:16 -0700217 if (ret_shorty == 'V') {
218 irb_.CreateRetVoid();
219 } else {
220 irb_.CreateRet(irb_.getJZero(ret_shorty));
221 }
222
223 basic_block_stack_overflow_ = block_continue;
224}
225
226
Logan Chienc670a8d2011-12-20 21:25:56 +0800227void MethodCompiler::EmitPrologueLastBranch() {
TDYa1274165a832012-04-03 17:47:16 -0700228 irb_.SetInsertPoint(basic_block_stack_overflow_);
229 irb_.CreateBr(basic_block_reg_alloca_);
230
Logan Chienc670a8d2011-12-20 21:25:56 +0800231 irb_.SetInsertPoint(basic_block_reg_alloca_);
Logan Chien8dfcbea2012-02-17 18:50:32 +0800232 irb_.CreateBr(basic_block_shadow_frame_alloca_);
233
234 irb_.SetInsertPoint(basic_block_shadow_frame_alloca_);
Logan Chiend6ececa2011-12-27 16:20:15 +0800235 irb_.CreateBr(basic_block_reg_arg_init_);
236}
237
238
Logan Chien8dfcbea2012-02-17 18:50:32 +0800239void MethodCompiler::EmitPrologueAllocShadowFrame() {
240 irb_.SetInsertPoint(basic_block_shadow_frame_alloca_);
241
242 // Allocate the shadow frame now!
243 uint32_t sirt_size = code_item_->registers_size_;
244 // TODO: registers_size_ is a bad approximation. Compute a
245 // tighter approximation at Dex verifier while performing data-flow
246 // analysis.
247
248 llvm::StructType* shadow_frame_type = irb_.getShadowFrameTy(sirt_size);
249 shadow_frame_ = irb_.CreateAlloca(shadow_frame_type);
250
251 // Zero-initialization of the shadow frame
252 llvm::ConstantAggregateZero* zero_initializer =
253 llvm::ConstantAggregateZero::get(shadow_frame_type);
254
255 irb_.CreateStore(zero_initializer, shadow_frame_);
256
Logan Chien8dfcbea2012-02-17 18:50:32 +0800257 // Store the method pointer
Logan Chien1b0a1b72012-03-15 06:20:17 +0800258 llvm::Value* method_field_addr =
259 irb_.CreatePtrDisp(shadow_frame_,
260 irb_.getPtrEquivInt(ShadowFrame::MethodOffset()),
261 irb_.getJObjectTy()->getPointerTo());
262
Logan Chien8dfcbea2012-02-17 18:50:32 +0800263 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
264 irb_.CreateStore(method_object_addr, method_field_addr);
265
266 // Store the number of the pointer slots
Logan Chien1b0a1b72012-03-15 06:20:17 +0800267 llvm::ConstantInt* num_of_refs_offset =
268 irb_.getPtrEquivInt(ShadowFrame::NumberOfReferencesOffset());
269
270 llvm::Value* num_of_refs_field_addr =
271 irb_.CreatePtrDisp(shadow_frame_, num_of_refs_offset,
272 irb_.getJIntTy()->getPointerTo());
273
274 llvm::ConstantInt* num_of_refs_value = irb_.getJInt(sirt_size);
275 irb_.CreateStore(num_of_refs_value, num_of_refs_field_addr);
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
1231 // Get thread-local exception field address
1232 llvm::Constant* exception_field_offset =
1233 irb_.getPtrEquivInt(Thread::ExceptionOffset().Int32Value());
1234
1235 llvm::Value* thread_object_addr =
1236 irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1237
1238 llvm::Value* exception_field_addr =
1239 irb_.CreatePtrDisp(thread_object_addr, exception_field_offset,
1240 irb_.getJObjectTy()->getPointerTo());
1241
1242 // Get exception object address
1243 llvm::Value* exception_object_addr = irb_.CreateLoad(exception_field_addr);
1244
1245 // Set thread-local exception field address to NULL
1246 irb_.CreateStore(irb_.getJNull(), exception_field_addr);
1247
1248 // Keep the exception object in the Dalvik register
Elliott Hughesadb8c672012-03-06 16:49:32 -08001249 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, exception_object_addr);
Logan Chien3354cec2012-01-13 14:29:03 +08001250
Logan Chien70f94b42011-12-27 17:49:11 +08001251 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1252}
1253
1254
1255void MethodCompiler::EmitInsn_ThrowException(uint32_t dex_pc,
1256 Instruction const* insn) {
Logan Chien6c6f12d2012-01-13 19:26:27 +08001257
Elliott Hughesadb8c672012-03-06 16:49:32 -08001258 DecodedInstruction dec_insn(insn);
Logan Chien6c6f12d2012-01-13 19:26:27 +08001259
1260 llvm::Value* exception_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001261 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chien6c6f12d2012-01-13 19:26:27 +08001262
TDYa127c8dc1012012-04-19 07:03:33 -07001263 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001264
Logan Chien6c6f12d2012-01-13 19:26:27 +08001265 irb_.CreateCall(irb_.GetRuntime(ThrowException), exception_addr);
1266
1267 EmitBranchExceptionLandingPad(dex_pc);
Logan Chien70f94b42011-12-27 17:49:11 +08001268}
1269
1270
Logan Chien9e5f5c12012-04-10 13:51:45 +08001271void MethodCompiler::EmitInsn_ThrowVerificationError(uint32_t dex_pc,
1272 Instruction const* insn) {
1273
1274 DecodedInstruction dec_insn(insn);
1275
TDYa127c8dc1012012-04-19 07:03:33 -07001276 EmitUpdateDexPC(dex_pc);
Logan Chien9e5f5c12012-04-10 13:51:45 +08001277
1278 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1279 llvm::Value* kind_value = irb_.getInt32(dec_insn.vA);
1280 llvm::Value* ref_value = irb_.getInt32(dec_insn.vB);
1281
1282 irb_.CreateCall3(irb_.GetRuntime(ThrowVerificationError),
1283 method_object_addr, kind_value, ref_value);
1284
1285 EmitBranchExceptionLandingPad(dex_pc);
1286}
1287
1288
Logan Chien70f94b42011-12-27 17:49:11 +08001289void MethodCompiler::EmitInsn_ReturnVoid(uint32_t dex_pc,
1290 Instruction const* insn) {
Logan Chien8898a272011-12-27 17:51:56 +08001291 // Garbage collection safe-point
1292 EmitGuard_GarbageCollectionSuspend(dex_pc);
1293
Logan Chien8dfcbea2012-02-17 18:50:32 +08001294 // Pop the shadow frame
1295 EmitPopShadowFrame();
1296
Logan Chien8898a272011-12-27 17:51:56 +08001297 // Return!
1298 irb_.CreateRetVoid();
Logan Chien70f94b42011-12-27 17:49:11 +08001299}
1300
1301
1302void MethodCompiler::EmitInsn_Return(uint32_t dex_pc,
1303 Instruction const* insn) {
Logan Chien8898a272011-12-27 17:51:56 +08001304
Elliott Hughesadb8c672012-03-06 16:49:32 -08001305 DecodedInstruction dec_insn(insn);
Logan Chien8898a272011-12-27 17:51:56 +08001306
1307 // Garbage collection safe-point
1308 EmitGuard_GarbageCollectionSuspend(dex_pc);
1309
Logan Chien8dfcbea2012-02-17 18:50:32 +08001310 // Pop the shadow frame
1311 EmitPopShadowFrame();
1312 // NOTE: It is important to keep this AFTER the GC safe-point. Otherwise,
1313 // the return value might be collected since the shadow stack is popped.
1314
Logan Chien8898a272011-12-27 17:51:56 +08001315 // Return!
Logan Chiendd361c92012-04-10 23:40:37 +08001316 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
Elliott Hughesadb8c672012-03-06 16:49:32 -08001317 llvm::Value* retval = EmitLoadDalvikReg(dec_insn.vA, ret_shorty, kAccurate);
Logan Chien8898a272011-12-27 17:51:56 +08001318
1319 irb_.CreateRet(retval);
Logan Chien70f94b42011-12-27 17:49:11 +08001320}
1321
1322
1323void MethodCompiler::EmitInsn_LoadConstant(uint32_t dex_pc,
1324 Instruction const* insn,
1325 JType imm_jty) {
Shih-wei Liao798366e2012-02-16 09:25:33 -08001326
Elliott Hughesadb8c672012-03-06 16:49:32 -08001327 DecodedInstruction dec_insn(insn);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001328
1329 DCHECK(imm_jty == kInt || imm_jty == kLong) << imm_jty;
1330
1331 int64_t imm = 0;
1332
1333 switch (insn->Opcode()) {
1334 // 32-bit Immediate
1335 case Instruction::CONST_4:
1336 case Instruction::CONST_16:
1337 case Instruction::CONST:
1338 case Instruction::CONST_WIDE_16:
1339 case Instruction::CONST_WIDE_32:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001340 imm = static_cast<int64_t>(static_cast<int32_t>(dec_insn.vB));
Shih-wei Liao798366e2012-02-16 09:25:33 -08001341 break;
1342
1343 case Instruction::CONST_HIGH16:
1344 imm = static_cast<int64_t>(static_cast<int32_t>(
Elliott Hughesadb8c672012-03-06 16:49:32 -08001345 static_cast<uint32_t>(static_cast<uint16_t>(dec_insn.vB)) << 16));
Shih-wei Liao798366e2012-02-16 09:25:33 -08001346 break;
1347
1348 // 64-bit Immediate
1349 case Instruction::CONST_WIDE:
Elliott Hughesadb8c672012-03-06 16:49:32 -08001350 imm = static_cast<int64_t>(dec_insn.vB_wide);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001351 break;
1352
1353 case Instruction::CONST_WIDE_HIGH16:
1354 imm = static_cast<int64_t>(
Elliott Hughesadb8c672012-03-06 16:49:32 -08001355 static_cast<uint64_t>(static_cast<uint16_t>(dec_insn.vB)) << 48);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001356 break;
1357
1358 // Unknown opcode for load constant (unreachable)
1359 default:
1360 LOG(FATAL) << "Unknown opcode for load constant: " << insn->Opcode();
1361 break;
1362 }
1363
1364 // Store the non-object register
1365 llvm::Type* imm_type = irb_.getJType(imm_jty, kAccurate);
1366 llvm::Constant* imm_value = llvm::ConstantInt::getSigned(imm_type, imm);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001367 EmitStoreDalvikReg(dec_insn.vA, imm_jty, kAccurate, imm_value);
Shih-wei Liao798366e2012-02-16 09:25:33 -08001368
1369 // Store the object register if it is possible to be null.
1370 if (imm_jty == kInt && imm == 0) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001371 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, irb_.getJNull());
Shih-wei Liao798366e2012-02-16 09:25:33 -08001372 }
1373
Logan Chien70f94b42011-12-27 17:49:11 +08001374 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1375}
1376
1377
1378void MethodCompiler::EmitInsn_LoadConstantString(uint32_t dex_pc,
1379 Instruction const* insn) {
Logan Chienc3b4ba12012-01-16 19:52:53 +08001380
Elliott Hughesadb8c672012-03-06 16:49:32 -08001381 DecodedInstruction dec_insn(insn);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001382
Elliott Hughesadb8c672012-03-06 16:49:32 -08001383 uint32_t string_idx = dec_insn.vB;
Logan Chienc3b4ba12012-01-16 19:52:53 +08001384
1385 llvm::Value* string_field_addr = EmitLoadDexCacheStringFieldAddr(string_idx);
1386
1387 llvm::Value* string_addr = irb_.CreateLoad(string_field_addr);
1388
1389 if (!compiler_->CanAssumeStringIsPresentInDexCache(dex_cache_, string_idx)) {
1390 llvm::BasicBlock* block_str_exist =
1391 CreateBasicBlockWithDexPC(dex_pc, "str_exist");
1392
1393 llvm::BasicBlock* block_str_resolve =
1394 CreateBasicBlockWithDexPC(dex_pc, "str_resolve");
1395
1396 // Test: Is the string resolved and in the dex cache?
1397 llvm::Value* equal_null = irb_.CreateICmpEQ(string_addr, irb_.getJNull());
1398
TDYa127a849cb62012-04-01 05:59:34 -07001399 irb_.CreateCondBr(equal_null, block_str_resolve, block_str_exist);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001400
1401 // String is resolved, go to next basic block.
1402 irb_.SetInsertPoint(block_str_exist);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001403 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, string_addr);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001404 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1405
1406 // String is not resolved yet, resolve it now.
1407 irb_.SetInsertPoint(block_str_resolve);
1408
1409 llvm::Function* runtime_func = irb_.GetRuntime(ResolveString);
1410
1411 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1412
1413 llvm::Value* string_idx_value = irb_.getInt32(string_idx);
1414
TDYa127c8dc1012012-04-19 07:03:33 -07001415 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001416
1417 string_addr = irb_.CreateCall2(runtime_func, method_object_addr,
1418 string_idx_value);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001419
1420 EmitGuard_ExceptionLandingPad(dex_pc);
1421 }
1422
1423 // Store the string object to the Dalvik register
Elliott Hughesadb8c672012-03-06 16:49:32 -08001424 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, string_addr);
Logan Chienc3b4ba12012-01-16 19:52:53 +08001425
Logan Chien70f94b42011-12-27 17:49:11 +08001426 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1427}
1428
1429
Logan Chien27b30252012-01-14 03:43:35 +08001430llvm::Value* MethodCompiler::EmitLoadConstantClass(uint32_t dex_pc,
1431 uint32_t type_idx) {
1432 if (!compiler_->CanAccessTypeWithoutChecks(method_idx_, dex_cache_,
1433 *dex_file_, type_idx)) {
1434 llvm::Value* type_idx_value = irb_.getInt32(type_idx);
1435
1436 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1437
TDYa127706e9b62012-04-19 12:24:26 -07001438 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1439
Logan Chien27b30252012-01-14 03:43:35 +08001440 llvm::Function* runtime_func =
1441 irb_.GetRuntime(InitializeTypeAndVerifyAccess);
1442
TDYa127c8dc1012012-04-19 07:03:33 -07001443 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001444
Logan Chien27b30252012-01-14 03:43:35 +08001445 llvm::Value* type_object_addr =
TDYa127706e9b62012-04-19 12:24:26 -07001446 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
Logan Chien27b30252012-01-14 03:43:35 +08001447
1448 EmitGuard_ExceptionLandingPad(dex_pc);
1449
1450 return type_object_addr;
1451
1452 } else {
1453 // Try to load the class (type) object from the test cache.
1454 llvm::Value* type_field_addr =
1455 EmitLoadDexCacheResolvedTypeFieldAddr(type_idx);
1456
1457 llvm::Value* type_object_addr = irb_.CreateLoad(type_field_addr);
1458
1459 if (compiler_->CanAssumeTypeIsPresentInDexCache(dex_cache_, type_idx)) {
1460 return type_object_addr;
1461 }
1462
1463 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
1464
1465 // Test whether class (type) object is in the dex cache or not
1466 llvm::Value* equal_null =
1467 irb_.CreateICmpEQ(type_object_addr, irb_.getJNull());
1468
1469 llvm::BasicBlock* block_cont =
1470 CreateBasicBlockWithDexPC(dex_pc, "cont");
1471
1472 llvm::BasicBlock* block_load_class =
1473 CreateBasicBlockWithDexPC(dex_pc, "load_class");
1474
1475 irb_.CreateCondBr(equal_null, block_load_class, block_cont);
1476
1477 // Failback routine to load the class object
1478 irb_.SetInsertPoint(block_load_class);
1479
1480 llvm::Function* runtime_func = irb_.GetRuntime(InitializeType);
1481
1482 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
1483
1484 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1485
TDYa127706e9b62012-04-19 12:24:26 -07001486 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1487
TDYa127c8dc1012012-04-19 07:03:33 -07001488 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001489
Logan Chien27b30252012-01-14 03:43:35 +08001490 llvm::Value* loaded_type_object_addr =
TDYa127706e9b62012-04-19 12:24:26 -07001491 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
Logan Chien27b30252012-01-14 03:43:35 +08001492
1493 EmitGuard_ExceptionLandingPad(dex_pc);
1494
1495 llvm::BasicBlock* block_after_load_class = irb_.GetInsertBlock();
1496
1497 irb_.CreateBr(block_cont);
1498
1499 // Now the class object must be loaded
1500 irb_.SetInsertPoint(block_cont);
1501
1502 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1503
1504 phi->addIncoming(type_object_addr, block_original);
1505 phi->addIncoming(loaded_type_object_addr, block_after_load_class);
1506
1507 return phi;
1508 }
1509}
1510
1511
Logan Chien70f94b42011-12-27 17:49:11 +08001512void MethodCompiler::EmitInsn_LoadConstantClass(uint32_t dex_pc,
1513 Instruction const* insn) {
Logan Chien27b30252012-01-14 03:43:35 +08001514
Elliott Hughesadb8c672012-03-06 16:49:32 -08001515 DecodedInstruction dec_insn(insn);
Logan Chien27b30252012-01-14 03:43:35 +08001516
Elliott Hughesadb8c672012-03-06 16:49:32 -08001517 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vB);
1518 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, type_object_addr);
Logan Chien27b30252012-01-14 03:43:35 +08001519
Logan Chien70f94b42011-12-27 17:49:11 +08001520 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1521}
1522
1523
1524void MethodCompiler::EmitInsn_MonitorEnter(uint32_t dex_pc,
1525 Instruction const* insn) {
Logan Chien9e0dbe42012-01-13 12:11:37 +08001526
Elliott Hughesadb8c672012-03-06 16:49:32 -08001527 DecodedInstruction dec_insn(insn);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001528
1529 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001530 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001531
1532 // TODO: Slow path always. May not need NullPointerException check.
1533 EmitGuard_NullPointerException(dex_pc, object_addr);
1534
TDYa127c8dc1012012-04-19 07:03:33 -07001535 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001536
TDYa127706e9b62012-04-19 12:24:26 -07001537 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1538
1539 irb_.CreateCall2(irb_.GetRuntime(LockObject), object_addr, thread_object_addr);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001540 EmitGuard_ExceptionLandingPad(dex_pc);
1541
Logan Chien70f94b42011-12-27 17:49:11 +08001542 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1543}
1544
1545
1546void MethodCompiler::EmitInsn_MonitorExit(uint32_t dex_pc,
1547 Instruction const* insn) {
Logan Chien9e0dbe42012-01-13 12:11:37 +08001548
Elliott Hughesadb8c672012-03-06 16:49:32 -08001549 DecodedInstruction dec_insn(insn);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001550
1551 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001552 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001553
1554 EmitGuard_NullPointerException(dex_pc, object_addr);
1555
TDYa127c8dc1012012-04-19 07:03:33 -07001556 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001557
TDYa127706e9b62012-04-19 12:24:26 -07001558 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1559
1560 irb_.CreateCall2(irb_.GetRuntime(UnlockObject), object_addr, thread_object_addr);
Logan Chien9e0dbe42012-01-13 12:11:37 +08001561 EmitGuard_ExceptionLandingPad(dex_pc);
1562
Logan Chien70f94b42011-12-27 17:49:11 +08001563 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1564}
1565
1566
1567void MethodCompiler::EmitInsn_CheckCast(uint32_t dex_pc,
1568 Instruction const* insn) {
Logan Chienfc880952012-01-15 23:53:10 +08001569
Elliott Hughesadb8c672012-03-06 16:49:32 -08001570 DecodedInstruction dec_insn(insn);
Logan Chienfc880952012-01-15 23:53:10 +08001571
1572 llvm::BasicBlock* block_test_class =
1573 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1574
1575 llvm::BasicBlock* block_test_sub_class =
1576 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
1577
1578 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001579 EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chienfc880952012-01-15 23:53:10 +08001580
1581 // Test: Is the reference equal to null? Act as no-op when it is null.
1582 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
1583
1584 irb_.CreateCondBr(equal_null,
1585 GetNextBasicBlock(dex_pc),
1586 block_test_class);
1587
1588 // Test: Is the object instantiated from the given class?
1589 irb_.SetInsertPoint(block_test_class);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001590 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vB);
Logan Chienfc880952012-01-15 23:53:10 +08001591 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1592
1593 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
1594
1595 llvm::Value* object_type_field_addr =
1596 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
1597
1598 llvm::Value* object_type_object_addr =
1599 irb_.CreateLoad(object_type_field_addr);
1600
1601 llvm::Value* equal_class =
1602 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
1603
1604 irb_.CreateCondBr(equal_class,
1605 GetNextBasicBlock(dex_pc),
1606 block_test_sub_class);
1607
1608 // Test: Is the object instantiated from the subclass of the given class?
1609 irb_.SetInsertPoint(block_test_sub_class);
1610
TDYa127c8dc1012012-04-19 07:03:33 -07001611 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001612
Logan Chienfc880952012-01-15 23:53:10 +08001613 irb_.CreateCall2(irb_.GetRuntime(CheckCast),
1614 type_object_addr, object_type_object_addr);
1615
1616 EmitGuard_ExceptionLandingPad(dex_pc);
1617
Logan Chien70f94b42011-12-27 17:49:11 +08001618 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1619}
1620
1621
1622void MethodCompiler::EmitInsn_InstanceOf(uint32_t dex_pc,
1623 Instruction const* insn) {
Logan Chien68725e22012-01-15 22:25:34 +08001624
Elliott Hughesadb8c672012-03-06 16:49:32 -08001625 DecodedInstruction dec_insn(insn);
Logan Chien68725e22012-01-15 22:25:34 +08001626
1627 llvm::Constant* zero = irb_.getJInt(0);
1628 llvm::Constant* one = irb_.getJInt(1);
1629
1630 llvm::BasicBlock* block_nullp = CreateBasicBlockWithDexPC(dex_pc, "nullp");
1631
1632 llvm::BasicBlock* block_test_class =
1633 CreateBasicBlockWithDexPC(dex_pc, "test_class");
1634
1635 llvm::BasicBlock* block_class_equals =
1636 CreateBasicBlockWithDexPC(dex_pc, "class_eq");
1637
1638 llvm::BasicBlock* block_test_sub_class =
1639 CreateBasicBlockWithDexPC(dex_pc, "test_sub_class");
1640
1641 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001642 EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
Logan Chien68725e22012-01-15 22:25:34 +08001643
1644 // Overview of the following code :
1645 // We check for null, if so, then false, otherwise check for class == . If so
1646 // then true, otherwise do callout slowpath.
1647 //
1648 // Test: Is the reference equal to null? Set 0 when it is null.
1649 llvm::Value* equal_null = irb_.CreateICmpEQ(object_addr, irb_.getJNull());
1650
1651 irb_.CreateCondBr(equal_null, block_nullp, block_test_class);
1652
1653 irb_.SetInsertPoint(block_nullp);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001654 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, zero);
Logan Chien68725e22012-01-15 22:25:34 +08001655 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1656
1657 // Test: Is the object instantiated from the given class?
1658 irb_.SetInsertPoint(block_test_class);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001659 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vC);
Logan Chien68725e22012-01-15 22:25:34 +08001660 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
1661
1662 llvm::PointerType* jobject_ptr_ty = irb_.getJObjectTy();
1663
1664 llvm::Value* object_type_field_addr =
1665 irb_.CreateBitCast(object_addr, jobject_ptr_ty->getPointerTo());
1666
1667 llvm::Value* object_type_object_addr =
1668 irb_.CreateLoad(object_type_field_addr);
1669
1670 llvm::Value* equal_class =
1671 irb_.CreateICmpEQ(type_object_addr, object_type_object_addr);
1672
1673 irb_.CreateCondBr(equal_class, block_class_equals, block_test_sub_class);
1674
1675 irb_.SetInsertPoint(block_class_equals);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001676 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, one);
Logan Chien68725e22012-01-15 22:25:34 +08001677 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1678
1679 // Test: Is the object instantiated from the subclass of the given class?
1680 irb_.SetInsertPoint(block_test_sub_class);
1681
1682 llvm::Value* result =
1683 irb_.CreateCall2(irb_.GetRuntime(IsAssignable),
1684 type_object_addr, object_type_object_addr);
1685
Elliott Hughesadb8c672012-03-06 16:49:32 -08001686 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result);
Logan Chien68725e22012-01-15 22:25:34 +08001687
Logan Chien70f94b42011-12-27 17:49:11 +08001688 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1689}
1690
1691
Logan Chien61bb6142012-02-03 15:34:53 +08001692llvm::Value* MethodCompiler::EmitLoadArrayLength(llvm::Value* array) {
1693 // Load array length field address
1694 llvm::Constant* array_len_field_offset =
1695 irb_.getPtrEquivInt(Array::LengthOffset().Int32Value());
1696
1697 llvm::Value* array_len_field_addr =
1698 irb_.CreatePtrDisp(array, array_len_field_offset,
1699 irb_.getJIntTy()->getPointerTo());
1700
1701 // Load array length
1702 return irb_.CreateLoad(array_len_field_addr);
1703}
1704
1705
Logan Chien70f94b42011-12-27 17:49:11 +08001706void MethodCompiler::EmitInsn_ArrayLength(uint32_t dex_pc,
1707 Instruction const* insn) {
Logan Chien61bb6142012-02-03 15:34:53 +08001708
Elliott Hughesadb8c672012-03-06 16:49:32 -08001709 DecodedInstruction dec_insn(insn);
Logan Chien61bb6142012-02-03 15:34:53 +08001710
1711 // Get the array object address
Elliott Hughesadb8c672012-03-06 16:49:32 -08001712 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
Logan Chien61bb6142012-02-03 15:34:53 +08001713 EmitGuard_NullPointerException(dex_pc, array_addr);
1714
1715 // Get the array length and store it to the register
1716 llvm::Value* array_len = EmitLoadArrayLength(array_addr);
Elliott Hughesadb8c672012-03-06 16:49:32 -08001717 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, array_len);
Logan Chien61bb6142012-02-03 15:34:53 +08001718
Logan Chien70f94b42011-12-27 17:49:11 +08001719 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1720}
1721
1722
1723void MethodCompiler::EmitInsn_NewInstance(uint32_t dex_pc,
1724 Instruction const* insn) {
Logan Chien032bdad2012-01-16 09:59:23 +08001725
Elliott Hughesadb8c672012-03-06 16:49:32 -08001726 DecodedInstruction dec_insn(insn);
Logan Chien032bdad2012-01-16 09:59:23 +08001727
1728 llvm::Function* runtime_func;
Logan Chien1a032b12012-04-11 11:43:04 +08001729 if (compiler_->CanAccessInstantiableTypeWithoutChecks(
1730 method_idx_, dex_cache_, *dex_file_, dec_insn.vB)) {
Logan Chien032bdad2012-01-16 09:59:23 +08001731 runtime_func = irb_.GetRuntime(AllocObject);
1732 } else {
1733 runtime_func = irb_.GetRuntime(AllocObjectWithAccessCheck);
1734 }
1735
Elliott Hughesadb8c672012-03-06 16:49:32 -08001736 llvm::Constant* type_index_value = irb_.getInt32(dec_insn.vB);
Logan Chien032bdad2012-01-16 09:59:23 +08001737
1738 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1739
TDYa127da83d972012-04-18 00:21:49 -07001740 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1741
TDYa127c8dc1012012-04-19 07:03:33 -07001742 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001743
Logan Chien032bdad2012-01-16 09:59:23 +08001744 llvm::Value* object_addr =
TDYa127da83d972012-04-18 00:21:49 -07001745 irb_.CreateCall3(runtime_func, type_index_value, method_object_addr, thread_object_addr);
Logan Chien032bdad2012-01-16 09:59:23 +08001746
1747 EmitGuard_ExceptionLandingPad(dex_pc);
1748
Elliott Hughesadb8c672012-03-06 16:49:32 -08001749 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, object_addr);
Logan Chien032bdad2012-01-16 09:59:23 +08001750
Logan Chien70f94b42011-12-27 17:49:11 +08001751 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1752}
1753
1754
Logan Chiena2cc6a32012-01-16 10:38:41 +08001755llvm::Value* MethodCompiler::EmitAllocNewArray(uint32_t dex_pc,
1756 int32_t length,
1757 uint32_t type_idx,
1758 bool is_filled_new_array) {
1759 llvm::Function* runtime_func;
1760
1761 bool skip_access_check =
1762 compiler_->CanAccessTypeWithoutChecks(method_idx_, dex_cache_,
1763 *dex_file_, type_idx);
1764
TDYa127a849cb62012-04-01 05:59:34 -07001765 llvm::Value* array_length_value;
1766
Logan Chiena2cc6a32012-01-16 10:38:41 +08001767 if (is_filled_new_array) {
1768 runtime_func = skip_access_check ?
1769 irb_.GetRuntime(CheckAndAllocArray) :
1770 irb_.GetRuntime(CheckAndAllocArrayWithAccessCheck);
TDYa127a849cb62012-04-01 05:59:34 -07001771 array_length_value = irb_.getInt32(length);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001772 } else {
1773 runtime_func = skip_access_check ?
1774 irb_.GetRuntime(AllocArray) :
1775 irb_.GetRuntime(AllocArrayWithAccessCheck);
TDYa127a849cb62012-04-01 05:59:34 -07001776 array_length_value = EmitLoadDalvikReg(length, kInt, kAccurate);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001777 }
1778
1779 llvm::Constant* type_index_value = irb_.getInt32(type_idx);
1780
1781 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1782
TDYa127da83d972012-04-18 00:21:49 -07001783 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1784
TDYa127c8dc1012012-04-19 07:03:33 -07001785 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08001786
Logan Chiena2cc6a32012-01-16 10:38:41 +08001787 llvm::Value* object_addr =
TDYa127da83d972012-04-18 00:21:49 -07001788 irb_.CreateCall4(runtime_func, type_index_value, method_object_addr,
1789 array_length_value, thread_object_addr);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001790
1791 EmitGuard_ExceptionLandingPad(dex_pc);
1792
1793 return object_addr;
1794}
1795
1796
Logan Chien70f94b42011-12-27 17:49:11 +08001797void MethodCompiler::EmitInsn_NewArray(uint32_t dex_pc,
1798 Instruction const* insn) {
Logan Chiena2cc6a32012-01-16 10:38:41 +08001799
Elliott Hughesadb8c672012-03-06 16:49:32 -08001800 DecodedInstruction dec_insn(insn);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001801
1802 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001803 EmitAllocNewArray(dex_pc, dec_insn.vB, dec_insn.vC, false);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001804
Elliott Hughesadb8c672012-03-06 16:49:32 -08001805 EmitStoreDalvikReg(dec_insn.vA, kObject, kAccurate, object_addr);
Logan Chiena2cc6a32012-01-16 10:38:41 +08001806
Logan Chien70f94b42011-12-27 17:49:11 +08001807 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1808}
1809
1810
1811void MethodCompiler::EmitInsn_FilledNewArray(uint32_t dex_pc,
1812 Instruction const* insn,
1813 bool is_range) {
Logan Chiena85fb2f2012-01-16 12:52:56 +08001814
Elliott Hughesadb8c672012-03-06 16:49:32 -08001815 DecodedInstruction dec_insn(insn);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001816
1817 llvm::Value* object_addr =
Elliott Hughesadb8c672012-03-06 16:49:32 -08001818 EmitAllocNewArray(dex_pc, dec_insn.vA, dec_insn.vB, true);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001819
Elliott Hughesadb8c672012-03-06 16:49:32 -08001820 if (dec_insn.vA > 0) {
Logan Chiendd361c92012-04-10 23:40:37 +08001821 // Resolve the element type
TDYa127183cf262012-04-11 07:53:21 -07001822 Class* klass = dex_cache_->GetResolvedType(dec_insn.vB)->GetComponentType();
Logan Chiendd361c92012-04-10 23:40:37 +08001823 // TODO: Avoid the usage of the dex_cache_. Try to figure out a better
1824 // way to distinguish [I and [L.
Logan Chiena85fb2f2012-01-16 12:52:56 +08001825 CHECK_NE(klass, static_cast<Class*>(NULL));
Logan Chiena85fb2f2012-01-16 12:52:56 +08001826
Logan Chiendd361c92012-04-10 23:40:37 +08001827 uint32_t alignment;
1828 llvm::Constant* elem_size;
1829 llvm::PointerType* field_type;
Logan Chiena85fb2f2012-01-16 12:52:56 +08001830
Logan Chiendd361c92012-04-10 23:40:37 +08001831 // NOTE: Currently filled-new-array only supports 'L', '[', and 'I'
1832 // as the element, thus we are only checking 2 cases: primitive int and
1833 // non-primitive type.
Logan Chiena85fb2f2012-01-16 12:52:56 +08001834 if (klass->IsPrimitiveInt()) {
Logan Chiendd361c92012-04-10 23:40:37 +08001835 alignment = sizeof(int32_t);
1836 elem_size = irb_.getPtrEquivInt(sizeof(int32_t));
Logan Chiena85fb2f2012-01-16 12:52:56 +08001837 field_type = irb_.getJIntTy()->getPointerTo();
1838 } else {
1839 CHECK(!klass->IsPrimitive());
Logan Chiendd361c92012-04-10 23:40:37 +08001840 alignment = irb_.getSizeOfPtrEquivInt();
1841 elem_size = irb_.getSizeOfPtrEquivIntValue();
Logan Chiena85fb2f2012-01-16 12:52:56 +08001842 field_type = irb_.getJObjectTy()->getPointerTo();
1843 }
1844
Logan Chiendd361c92012-04-10 23:40:37 +08001845 llvm::Value* data_field_offset =
1846 irb_.getPtrEquivInt(Array::DataOffset(alignment).Int32Value());
1847
1848 llvm::Value* data_field_addr =
1849 irb_.CreatePtrDisp(object_addr, data_field_offset, field_type);
1850
Logan Chiena85fb2f2012-01-16 12:52:56 +08001851 // TODO: Tune this code. Currently we are generating one instruction for
1852 // one element which may be very space consuming. Maybe changing to use
1853 // memcpy may help; however, since we can't guarantee that the alloca of
1854 // dalvik register are continuous, we can't perform such optimization yet.
Elliott Hughesadb8c672012-03-06 16:49:32 -08001855 for (uint32_t i = 0; i < dec_insn.vA; ++i) {
Logan Chiena85fb2f2012-01-16 12:52:56 +08001856 int reg_index;
1857 if (is_range) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001858 reg_index = dec_insn.vC + i;
Logan Chiena85fb2f2012-01-16 12:52:56 +08001859 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08001860 reg_index = dec_insn.arg[i];
Logan Chiena85fb2f2012-01-16 12:52:56 +08001861 }
1862
1863 llvm::Value* reg_value;
1864 if (klass->IsPrimitiveInt()) {
1865 reg_value = EmitLoadDalvikReg(reg_index, kInt, kAccurate);
1866 } else {
1867 reg_value = EmitLoadDalvikReg(reg_index, kObject, kAccurate);
1868 }
1869
1870 irb_.CreateStore(reg_value, data_field_addr);
1871
Logan Chiendd361c92012-04-10 23:40:37 +08001872 data_field_addr =
1873 irb_.CreatePtrDisp(data_field_addr, elem_size, field_type);
Logan Chiena85fb2f2012-01-16 12:52:56 +08001874 }
1875 }
1876
1877 EmitStoreDalvikRetValReg(kObject, kAccurate, object_addr);
1878
Logan Chien70f94b42011-12-27 17:49:11 +08001879 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1880}
1881
1882
1883void MethodCompiler::EmitInsn_FillArrayData(uint32_t dex_pc,
1884 Instruction const* insn) {
Logan Chiene58b6582012-01-16 17:13:13 +08001885
Elliott Hughesadb8c672012-03-06 16:49:32 -08001886 DecodedInstruction dec_insn(insn);
Logan Chiene58b6582012-01-16 17:13:13 +08001887
1888 // Read the payload
Logan Chiene58b6582012-01-16 17:13:13 +08001889 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
Elliott Hughesadb8c672012-03-06 16:49:32 -08001890 static_cast<int32_t>(dec_insn.vB);
Logan Chiene58b6582012-01-16 17:13:13 +08001891
Logan Chien19c350a2012-05-01 19:21:32 +08001892 const Instruction::ArrayDataPayload* payload =
1893 reinterpret_cast<const Instruction::ArrayDataPayload*>(
1894 code_item_->insns_ + payload_offset);
Logan Chiene58b6582012-01-16 17:13:13 +08001895
Logan Chien86f50672012-04-24 13:08:45 +08001896 // Load array object
Elliott Hughesadb8c672012-03-06 16:49:32 -08001897 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiene58b6582012-01-16 17:13:13 +08001898
Logan Chien86f50672012-04-24 13:08:45 +08001899 if (payload->element_count == 0) {
1900 // When the number of the elements in the payload is zero, we don't have
1901 // to copy any numbers. However, we should check whether the array object
1902 // address is equal to null or not.
1903 EmitGuard_NullPointerException(dex_pc, array_addr);
1904 } else {
1905 // To save the code size, we are going to call the runtime function to
1906 // copy the content from DexFile.
Logan Chiene58b6582012-01-16 17:13:13 +08001907
Logan Chien86f50672012-04-24 13:08:45 +08001908 // NOTE: We will check for the NullPointerException in the runtime.
Logan Chiene58b6582012-01-16 17:13:13 +08001909
Logan Chien86f50672012-04-24 13:08:45 +08001910 llvm::Function* runtime_func = irb_.GetRuntime(FillArrayData);
Logan Chiene58b6582012-01-16 17:13:13 +08001911
Logan Chien86f50672012-04-24 13:08:45 +08001912 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
Logan Chiene58b6582012-01-16 17:13:13 +08001913
Logan Chien86f50672012-04-24 13:08:45 +08001914 EmitUpdateDexPC(dex_pc);
Logan Chiene58b6582012-01-16 17:13:13 +08001915
Logan Chien86f50672012-04-24 13:08:45 +08001916 irb_.CreateCall4(runtime_func,
1917 method_object_addr, irb_.getInt32(dex_pc),
1918 array_addr, irb_.getInt32(payload_offset));
Logan Chiene58b6582012-01-16 17:13:13 +08001919
Logan Chien86f50672012-04-24 13:08:45 +08001920 EmitGuard_ExceptionLandingPad(dex_pc);
Logan Chiene58b6582012-01-16 17:13:13 +08001921 }
1922
Logan Chien70f94b42011-12-27 17:49:11 +08001923 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1924}
1925
1926
1927void MethodCompiler::EmitInsn_UnconditionalBranch(uint32_t dex_pc,
1928 Instruction const* insn) {
Logan Chiena466c162011-12-27 17:55:46 +08001929
Elliott Hughesadb8c672012-03-06 16:49:32 -08001930 DecodedInstruction dec_insn(insn);
Logan Chiena466c162011-12-27 17:55:46 +08001931
Elliott Hughesadb8c672012-03-06 16:49:32 -08001932 int32_t branch_offset = dec_insn.vA;
Logan Chiena466c162011-12-27 17:55:46 +08001933
1934 if (branch_offset <= 0) {
1935 // Garbage collection safe-point on backward branch
1936 EmitGuard_GarbageCollectionSuspend(dex_pc);
1937 }
1938
1939 irb_.CreateBr(GetBasicBlock(dex_pc + branch_offset));
Logan Chien70f94b42011-12-27 17:49:11 +08001940}
1941
1942
1943void MethodCompiler::EmitInsn_PackedSwitch(uint32_t dex_pc,
1944 Instruction const* insn) {
Logan Chien7a89b6d2011-12-27 17:56:56 +08001945
Elliott Hughesadb8c672012-03-06 16:49:32 -08001946 DecodedInstruction dec_insn(insn);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001947
Logan Chien7a89b6d2011-12-27 17:56:56 +08001948 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
Elliott Hughesadb8c672012-03-06 16:49:32 -08001949 static_cast<int32_t>(dec_insn.vB);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001950
Logan Chien19c350a2012-05-01 19:21:32 +08001951 const Instruction::PackedSwitchPayload* payload =
1952 reinterpret_cast<const Instruction::PackedSwitchPayload*>(
1953 code_item_->insns_ + payload_offset);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001954
Elliott Hughesadb8c672012-03-06 16:49:32 -08001955 llvm::Value* value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001956
1957 llvm::SwitchInst* sw =
Logan Chien19c350a2012-05-01 19:21:32 +08001958 irb_.CreateSwitch(value, GetNextBasicBlock(dex_pc), payload->case_count);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001959
Logan Chien19c350a2012-05-01 19:21:32 +08001960 for (uint16_t i = 0; i < payload->case_count; ++i) {
1961 sw->addCase(irb_.getInt32(payload->first_key + i),
1962 GetBasicBlock(dex_pc + payload->targets[i]));
Logan Chien7a89b6d2011-12-27 17:56:56 +08001963 }
Logan Chien70f94b42011-12-27 17:49:11 +08001964}
1965
1966
1967void MethodCompiler::EmitInsn_SparseSwitch(uint32_t dex_pc,
1968 Instruction const* insn) {
Logan Chien7a89b6d2011-12-27 17:56:56 +08001969
Elliott Hughesadb8c672012-03-06 16:49:32 -08001970 DecodedInstruction dec_insn(insn);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001971
Logan Chien7a89b6d2011-12-27 17:56:56 +08001972 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
Elliott Hughesadb8c672012-03-06 16:49:32 -08001973 static_cast<int32_t>(dec_insn.vB);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001974
Logan Chien19c350a2012-05-01 19:21:32 +08001975 const Instruction::SparseSwitchPayload* payload =
1976 reinterpret_cast<const Instruction::SparseSwitchPayload*>(
1977 code_item_->insns_ + payload_offset);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001978
Logan Chien19c350a2012-05-01 19:21:32 +08001979 const int32_t* keys = payload->GetKeys();
1980 const int32_t* targets = payload->GetTargets();
Logan Chien7a89b6d2011-12-27 17:56:56 +08001981
Elliott Hughesadb8c672012-03-06 16:49:32 -08001982 llvm::Value* value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001983
1984 llvm::SwitchInst* sw =
Logan Chien19c350a2012-05-01 19:21:32 +08001985 irb_.CreateSwitch(value, GetNextBasicBlock(dex_pc), payload->case_count);
Logan Chien7a89b6d2011-12-27 17:56:56 +08001986
Logan Chien19c350a2012-05-01 19:21:32 +08001987 for (size_t i = 0; i < payload->case_count; ++i) {
Logan Chien7a89b6d2011-12-27 17:56:56 +08001988 sw->addCase(irb_.getInt32(keys[i]), GetBasicBlock(dex_pc + targets[i]));
1989 }
Logan Chien70f94b42011-12-27 17:49:11 +08001990}
1991
1992
1993void MethodCompiler::EmitInsn_FPCompare(uint32_t dex_pc,
1994 Instruction const* insn,
1995 JType fp_jty,
1996 bool gt_bias) {
Logan Chien2c37e8e2011-12-27 17:58:46 +08001997
Elliott Hughesadb8c672012-03-06 16:49:32 -08001998 DecodedInstruction dec_insn(insn);
Logan Chien2c37e8e2011-12-27 17:58:46 +08001999
2000 DCHECK(fp_jty == kFloat || fp_jty == kDouble) << "JType: " << fp_jty;
2001
Elliott Hughesadb8c672012-03-06 16:49:32 -08002002 llvm::Value* src1_value = EmitLoadDalvikReg(dec_insn.vB, fp_jty, kAccurate);
2003 llvm::Value* src2_value = EmitLoadDalvikReg(dec_insn.vC, fp_jty, kAccurate);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002004
2005 llvm::Value* cmp_eq = irb_.CreateFCmpOEQ(src1_value, src2_value);
2006 llvm::Value* cmp_lt;
2007
2008 if (gt_bias) {
2009 cmp_lt = irb_.CreateFCmpOLT(src1_value, src2_value);
2010 } else {
2011 cmp_lt = irb_.CreateFCmpULT(src1_value, src2_value);
2012 }
2013
2014 llvm::Value* result = EmitCompareResultSelection(cmp_eq, cmp_lt);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002015 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002016
Logan Chien70f94b42011-12-27 17:49:11 +08002017 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2018}
2019
2020
2021void MethodCompiler::EmitInsn_LongCompare(uint32_t dex_pc,
2022 Instruction const* insn) {
Logan Chien2c37e8e2011-12-27 17:58:46 +08002023
Elliott Hughesadb8c672012-03-06 16:49:32 -08002024 DecodedInstruction dec_insn(insn);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002025
Elliott Hughesadb8c672012-03-06 16:49:32 -08002026 llvm::Value* src1_value = EmitLoadDalvikReg(dec_insn.vB, kLong, kAccurate);
2027 llvm::Value* src2_value = EmitLoadDalvikReg(dec_insn.vC, kLong, kAccurate);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002028
2029 llvm::Value* cmp_eq = irb_.CreateICmpEQ(src1_value, src2_value);
2030 llvm::Value* cmp_lt = irb_.CreateICmpSLT(src1_value, src2_value);
2031
2032 llvm::Value* result = EmitCompareResultSelection(cmp_eq, cmp_lt);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002033 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result);
Logan Chien2c37e8e2011-12-27 17:58:46 +08002034
Logan Chien70f94b42011-12-27 17:49:11 +08002035 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2036}
2037
2038
Logan Chien2c37e8e2011-12-27 17:58:46 +08002039llvm::Value* MethodCompiler::EmitCompareResultSelection(llvm::Value* cmp_eq,
2040 llvm::Value* cmp_lt) {
2041
2042 llvm::Constant* zero = irb_.getJInt(0);
2043 llvm::Constant* pos1 = irb_.getJInt(1);
2044 llvm::Constant* neg1 = irb_.getJInt(-1);
2045
2046 llvm::Value* result_lt = irb_.CreateSelect(cmp_lt, neg1, pos1);
2047 llvm::Value* result_eq = irb_.CreateSelect(cmp_eq, zero, result_lt);
2048
2049 return result_eq;
2050}
2051
2052
Logan Chien70f94b42011-12-27 17:49:11 +08002053void MethodCompiler::EmitInsn_BinaryConditionalBranch(uint32_t dex_pc,
2054 Instruction const* insn,
2055 CondBranchKind cond) {
Logan Chiena78e3c82011-12-27 17:59:35 +08002056
Elliott Hughesadb8c672012-03-06 16:49:32 -08002057 DecodedInstruction dec_insn(insn);
Logan Chiena78e3c82011-12-27 17:59:35 +08002058
Elliott Hughesadb8c672012-03-06 16:49:32 -08002059 int8_t src1_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vA);
2060 int8_t src2_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vB);
Logan Chiena78e3c82011-12-27 17:59:35 +08002061
2062 DCHECK_NE(kRegUnknown, src1_reg_cat);
2063 DCHECK_NE(kRegUnknown, src2_reg_cat);
2064 DCHECK_NE(kRegCat2, src1_reg_cat);
2065 DCHECK_NE(kRegCat2, src2_reg_cat);
2066
Elliott Hughesadb8c672012-03-06 16:49:32 -08002067 int32_t branch_offset = dec_insn.vC;
Logan Chiena78e3c82011-12-27 17:59:35 +08002068
2069 if (branch_offset <= 0) {
2070 // Garbage collection safe-point on backward branch
2071 EmitGuard_GarbageCollectionSuspend(dex_pc);
2072 }
2073
Logan Chiena78e3c82011-12-27 17:59:35 +08002074 llvm::Value* src1_value;
2075 llvm::Value* src2_value;
2076
TDYa1278e9b4492012-04-24 15:50:27 -07002077 if (src1_reg_cat == kRegZero && src2_reg_cat == kRegZero) {
2078 src1_value = irb_.getInt32(0);
2079 src2_value = irb_.getInt32(0);
2080 } else if (src1_reg_cat != kRegZero && src2_reg_cat != kRegZero) {
Logan Chiena78e3c82011-12-27 17:59:35 +08002081 CHECK_EQ(src1_reg_cat, src2_reg_cat);
2082
2083 if (src1_reg_cat == kRegCat1nr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002084 src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
2085 src2_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002086 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002087 src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
2088 src2_value = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002089 }
2090 } else {
2091 DCHECK(src1_reg_cat == kRegZero ||
2092 src2_reg_cat == kRegZero);
2093
2094 if (src1_reg_cat == kRegZero) {
2095 if (src2_reg_cat == kRegCat1nr) {
2096 src1_value = irb_.getJInt(0);
Elliott Hughesadb8c672012-03-06 16:49:32 -08002097 src2_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002098 } else {
2099 src1_value = irb_.getJNull();
Elliott Hughesadb8c672012-03-06 16:49:32 -08002100 src2_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002101 }
2102 } else { // src2_reg_cat == kRegZero
2103 if (src2_reg_cat == kRegCat1nr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002104 src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002105 src2_value = irb_.getJInt(0);
2106 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002107 src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002108 src2_value = irb_.getJNull();
2109 }
2110 }
2111 }
2112
2113 llvm::Value* cond_value =
2114 EmitConditionResult(src1_value, src2_value, cond);
2115
2116 irb_.CreateCondBr(cond_value,
2117 GetBasicBlock(dex_pc + branch_offset),
2118 GetNextBasicBlock(dex_pc));
Logan Chien70f94b42011-12-27 17:49:11 +08002119}
2120
2121
2122void MethodCompiler::EmitInsn_UnaryConditionalBranch(uint32_t dex_pc,
2123 Instruction const* insn,
2124 CondBranchKind cond) {
Logan Chiena78e3c82011-12-27 17:59:35 +08002125
Elliott Hughesadb8c672012-03-06 16:49:32 -08002126 DecodedInstruction dec_insn(insn);
Logan Chiena78e3c82011-12-27 17:59:35 +08002127
Elliott Hughesadb8c672012-03-06 16:49:32 -08002128 int8_t src_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vA);
Logan Chiena78e3c82011-12-27 17:59:35 +08002129
2130 DCHECK_NE(kRegUnknown, src_reg_cat);
2131 DCHECK_NE(kRegCat2, src_reg_cat);
2132
Elliott Hughesadb8c672012-03-06 16:49:32 -08002133 int32_t branch_offset = dec_insn.vB;
Logan Chiena78e3c82011-12-27 17:59:35 +08002134
2135 if (branch_offset <= 0) {
2136 // Garbage collection safe-point on backward branch
2137 EmitGuard_GarbageCollectionSuspend(dex_pc);
2138 }
2139
Logan Chiena78e3c82011-12-27 17:59:35 +08002140 llvm::Value* src1_value;
2141 llvm::Value* src2_value;
2142
TDYa1278e9b4492012-04-24 15:50:27 -07002143 if (src_reg_cat == kRegZero) {
2144 src1_value = irb_.getInt32(0);
2145 src2_value = irb_.getInt32(0);
2146 } else if (src_reg_cat == kRegCat1nr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002147 src1_value = EmitLoadDalvikReg(dec_insn.vA, kInt, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002148 src2_value = irb_.getInt32(0);
2149 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002150 src1_value = EmitLoadDalvikReg(dec_insn.vA, kObject, kAccurate);
Logan Chiena78e3c82011-12-27 17:59:35 +08002151 src2_value = irb_.getJNull();
2152 }
2153
2154 llvm::Value* cond_value =
2155 EmitConditionResult(src1_value, src2_value, cond);
2156
2157 irb_.CreateCondBr(cond_value,
2158 GetBasicBlock(dex_pc + branch_offset),
2159 GetNextBasicBlock(dex_pc));
2160}
2161
2162
2163RegCategory MethodCompiler::GetInferredRegCategory(uint32_t dex_pc,
2164 uint16_t reg_idx) {
Logan Chiendd361c92012-04-10 23:40:37 +08002165
2166 Compiler::MethodReference mref(dex_file_, method_idx_);
2167
2168 InferredRegCategoryMap const* map =
Ian Rogers776ac1f2012-04-13 23:36:36 -07002169 verifier::MethodVerifier::GetInferredRegCategoryMap(mref);
Logan Chiendd361c92012-04-10 23:40:37 +08002170
Logan Chiena78e3c82011-12-27 17:59:35 +08002171 CHECK_NE(map, static_cast<InferredRegCategoryMap*>(NULL));
2172
2173 return map->GetRegCategory(dex_pc, reg_idx);
2174}
2175
2176
2177llvm::Value* MethodCompiler::EmitConditionResult(llvm::Value* lhs,
2178 llvm::Value* rhs,
2179 CondBranchKind cond) {
2180 switch (cond) {
2181 case kCondBranch_EQ:
2182 return irb_.CreateICmpEQ(lhs, rhs);
2183
2184 case kCondBranch_NE:
2185 return irb_.CreateICmpNE(lhs, rhs);
2186
2187 case kCondBranch_LT:
2188 return irb_.CreateICmpSLT(lhs, rhs);
2189
2190 case kCondBranch_GE:
2191 return irb_.CreateICmpSGE(lhs, rhs);
2192
2193 case kCondBranch_GT:
2194 return irb_.CreateICmpSGT(lhs, rhs);
2195
2196 case kCondBranch_LE:
2197 return irb_.CreateICmpSLE(lhs, rhs);
2198
2199 default: // Unreachable
2200 LOG(FATAL) << "Unknown conditional branch kind: " << cond;
2201 return NULL;
2202 }
Logan Chien70f94b42011-12-27 17:49:11 +08002203}
2204
TDYa12783bb6622012-04-17 02:20:34 -07002205void MethodCompiler::EmitMarkGCCard(llvm::Value* value, llvm::Value* target_addr) {
2206 // Using runtime support, let the target can override by InlineAssembly.
2207 llvm::Function* runtime_func = irb_.GetRuntime(MarkGCCard);
2208
2209 irb_.CreateCall2(runtime_func, value, target_addr);
2210}
Logan Chien70f94b42011-12-27 17:49:11 +08002211
Logan Chiene27fdbb2012-01-02 23:27:26 +08002212void
2213MethodCompiler::EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc,
2214 llvm::Value* array,
2215 llvm::Value* index) {
2216 llvm::Value* array_len = EmitLoadArrayLength(array);
2217
2218 llvm::Value* cmp = irb_.CreateICmpUGE(index, array_len);
2219
2220 llvm::BasicBlock* block_exception =
2221 CreateBasicBlockWithDexPC(dex_pc, "overflow");
2222
2223 llvm::BasicBlock* block_continue =
2224 CreateBasicBlockWithDexPC(dex_pc, "cont");
2225
2226 irb_.CreateCondBr(cmp, block_exception, block_continue);
2227
2228 irb_.SetInsertPoint(block_exception);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002229
TDYa127c8dc1012012-04-19 07:03:33 -07002230 EmitUpdateDexPC(dex_pc);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002231 irb_.CreateCall2(irb_.GetRuntime(ThrowIndexOutOfBounds), index, array_len);
2232 EmitBranchExceptionLandingPad(dex_pc);
2233
2234 irb_.SetInsertPoint(block_continue);
2235}
2236
2237
2238void MethodCompiler::EmitGuard_ArrayException(uint32_t dex_pc,
2239 llvm::Value* array,
2240 llvm::Value* index) {
2241 EmitGuard_NullPointerException(dex_pc, array);
2242 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array, index);
2243}
2244
2245
2246// Emit Array GetElementPtr
2247llvm::Value* MethodCompiler::EmitArrayGEP(llvm::Value* array_addr,
2248 llvm::Value* index_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08002249 llvm::Type* elem_type,
2250 JType elem_jty) {
2251
2252 int data_offset;
2253 if (elem_jty == kLong || elem_jty == kDouble ||
2254 (elem_jty == kObject && sizeof(uint64_t) == sizeof(Object*))) {
2255 data_offset = Array::DataOffset(sizeof(int64_t)).Int32Value();
2256 } else {
2257 data_offset = Array::DataOffset(sizeof(int32_t)).Int32Value();
2258 }
Logan Chiene27fdbb2012-01-02 23:27:26 +08002259
2260 llvm::Constant* data_offset_value =
Ian Rogers04ec04e2012-02-28 16:15:33 -08002261 irb_.getPtrEquivInt(data_offset);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002262
2263 llvm::Value* array_data_addr =
2264 irb_.CreatePtrDisp(array_addr, data_offset_value,
2265 elem_type->getPointerTo());
2266
2267 return irb_.CreateGEP(array_data_addr, index_value);
2268}
2269
2270
Logan Chien70f94b42011-12-27 17:49:11 +08002271void MethodCompiler::EmitInsn_AGet(uint32_t dex_pc,
2272 Instruction const* insn,
2273 JType elem_jty) {
Logan Chiene27fdbb2012-01-02 23:27:26 +08002274
Elliott Hughesadb8c672012-03-06 16:49:32 -08002275 DecodedInstruction dec_insn(insn);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002276
Elliott Hughesadb8c672012-03-06 16:49:32 -08002277 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
2278 llvm::Value* index_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002279
2280 EmitGuard_ArrayException(dex_pc, array_addr, index_value);
2281
2282 llvm::Type* elem_type = irb_.getJType(elem_jty, kArray);
2283
2284 llvm::Value* array_elem_addr =
Ian Rogers04ec04e2012-02-28 16:15:33 -08002285 EmitArrayGEP(array_addr, index_value, elem_type, elem_jty);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002286
2287 llvm::Value* array_elem_value = irb_.CreateLoad(array_elem_addr);
2288
Elliott Hughesadb8c672012-03-06 16:49:32 -08002289 EmitStoreDalvikReg(dec_insn.vA, elem_jty, kArray, array_elem_value);
Logan Chiene27fdbb2012-01-02 23:27:26 +08002290
Logan Chien70f94b42011-12-27 17:49:11 +08002291 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2292}
2293
2294
2295void MethodCompiler::EmitInsn_APut(uint32_t dex_pc,
2296 Instruction const* insn,
2297 JType elem_jty) {
Logan Chien8dabb432012-01-02 23:29:32 +08002298
Elliott Hughesadb8c672012-03-06 16:49:32 -08002299 DecodedInstruction dec_insn(insn);
Logan Chien8dabb432012-01-02 23:29:32 +08002300
Elliott Hughesadb8c672012-03-06 16:49:32 -08002301 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB, kObject, kAccurate);
2302 llvm::Value* index_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate);
Logan Chien8dabb432012-01-02 23:29:32 +08002303
2304 EmitGuard_ArrayException(dex_pc, array_addr, index_value);
2305
2306 llvm::Type* elem_type = irb_.getJType(elem_jty, kArray);
2307
2308 llvm::Value* array_elem_addr =
Ian Rogers04ec04e2012-02-28 16:15:33 -08002309 EmitArrayGEP(array_addr, index_value, elem_type, elem_jty);
Logan Chien8dabb432012-01-02 23:29:32 +08002310
Elliott Hughesadb8c672012-03-06 16:49:32 -08002311 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, elem_jty, kArray);
Logan Chien8dabb432012-01-02 23:29:32 +08002312
TDYa12783bb6622012-04-17 02:20:34 -07002313 if (elem_jty == kObject) { // If put an object, check the type, and mark GC card table.
TDYa1271b86d072012-04-05 17:38:56 -07002314 llvm::Function* runtime_func = irb_.GetRuntime(CheckPutArrayElement);
2315
2316 irb_.CreateCall2(runtime_func, new_value, array_addr);
2317
2318 EmitGuard_ExceptionLandingPad(dex_pc);
TDYa12783bb6622012-04-17 02:20:34 -07002319
2320 EmitMarkGCCard(new_value, array_addr);
TDYa1271b86d072012-04-05 17:38:56 -07002321 }
2322
Logan Chien8dabb432012-01-02 23:29:32 +08002323 irb_.CreateStore(new_value, array_elem_addr);
2324
Logan Chien70f94b42011-12-27 17:49:11 +08002325 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2326}
2327
2328
2329void MethodCompiler::EmitInsn_IGet(uint32_t dex_pc,
2330 Instruction const* insn,
2331 JType field_jty) {
Logan Chien48f1d2a2012-01-02 22:49:53 +08002332
Elliott Hughesadb8c672012-03-06 16:49:32 -08002333 DecodedInstruction dec_insn(insn);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002334
Elliott Hughesadb8c672012-03-06 16:49:32 -08002335 uint32_t reg_idx = dec_insn.vB;
2336 uint32_t field_idx = dec_insn.vC;
Logan Chien48f1d2a2012-01-02 22:49:53 +08002337
Logan Chien48f1d2a2012-01-02 22:49:53 +08002338 llvm::Value* object_addr = EmitLoadDalvikReg(reg_idx, kObject, kAccurate);
2339
2340 EmitGuard_NullPointerException(dex_pc, object_addr);
2341
2342 llvm::Value* field_value;
2343
Logan Chien933abf82012-04-11 12:24:31 +08002344 int field_offset;
2345 bool is_volatile;
2346 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
2347 field_idx, oat_compilation_unit_, field_offset, is_volatile, false);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002348
Logan Chien933abf82012-04-11 12:24:31 +08002349 if (!is_fast_path) {
Logan Chien48f1d2a2012-01-02 22:49:53 +08002350 llvm::Function* runtime_func;
2351
2352 if (field_jty == kObject) {
Logan Chien3b2b2e72012-03-06 16:11:45 +08002353 runtime_func = irb_.GetRuntime(GetObjectInstance);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002354 } else if (field_jty == kLong || field_jty == kDouble) {
Logan Chien3b2b2e72012-03-06 16:11:45 +08002355 runtime_func = irb_.GetRuntime(Get64Instance);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002356 } else {
Logan Chien3b2b2e72012-03-06 16:11:45 +08002357 runtime_func = irb_.GetRuntime(Get32Instance);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002358 }
2359
2360 llvm::ConstantInt* field_idx_value = irb_.getInt32(field_idx);
2361
2362 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2363
TDYa127c8dc1012012-04-19 07:03:33 -07002364 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002365
Logan Chien3b2b2e72012-03-06 16:11:45 +08002366 field_value = irb_.CreateCall3(runtime_func, field_idx_value,
2367 method_object_addr, object_addr);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002368
2369 EmitGuard_ExceptionLandingPad(dex_pc);
2370
2371 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002372 DCHECK_GE(field_offset, 0);
2373
Logan Chien48f1d2a2012-01-02 22:49:53 +08002374 llvm::PointerType* field_type =
2375 irb_.getJType(field_jty, kField)->getPointerTo();
2376
Logan Chien933abf82012-04-11 12:24:31 +08002377 llvm::ConstantInt* field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002378
2379 llvm::Value* field_addr =
Logan Chien933abf82012-04-11 12:24:31 +08002380 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002381
Logan Chien933abf82012-04-11 12:24:31 +08002382 // TODO: Check is_volatile. We need to generate atomic load instruction
2383 // when is_volatile is true.
Logan Chien48f1d2a2012-01-02 22:49:53 +08002384 field_value = irb_.CreateLoad(field_addr);
2385 }
2386
Elliott Hughesadb8c672012-03-06 16:49:32 -08002387 EmitStoreDalvikReg(dec_insn.vA, field_jty, kField, field_value);
Logan Chien48f1d2a2012-01-02 22:49:53 +08002388
Logan Chien70f94b42011-12-27 17:49:11 +08002389 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2390}
2391
2392
2393void MethodCompiler::EmitInsn_IPut(uint32_t dex_pc,
2394 Instruction const* insn,
2395 JType field_jty) {
Logan Chiendd6aa872012-01-03 16:06:32 +08002396
Elliott Hughesadb8c672012-03-06 16:49:32 -08002397 DecodedInstruction dec_insn(insn);
Logan Chiendd6aa872012-01-03 16:06:32 +08002398
Elliott Hughesadb8c672012-03-06 16:49:32 -08002399 uint32_t reg_idx = dec_insn.vB;
2400 uint32_t field_idx = dec_insn.vC;
Logan Chiendd6aa872012-01-03 16:06:32 +08002401
Logan Chiendd6aa872012-01-03 16:06:32 +08002402 llvm::Value* object_addr = EmitLoadDalvikReg(reg_idx, kObject, kAccurate);
2403
2404 EmitGuard_NullPointerException(dex_pc, object_addr);
2405
Elliott Hughesadb8c672012-03-06 16:49:32 -08002406 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, field_jty, kField);
Logan Chiendd6aa872012-01-03 16:06:32 +08002407
Logan Chien933abf82012-04-11 12:24:31 +08002408 int field_offset;
2409 bool is_volatile;
2410 bool is_fast_path = compiler_->ComputeInstanceFieldInfo(
2411 field_idx, oat_compilation_unit_, field_offset, is_volatile, true);
Logan Chiendd6aa872012-01-03 16:06:32 +08002412
Logan Chien933abf82012-04-11 12:24:31 +08002413 if (!is_fast_path) {
Logan Chiendd6aa872012-01-03 16:06:32 +08002414 llvm::Function* runtime_func;
2415
2416 if (field_jty == kObject) {
2417 runtime_func = irb_.GetRuntime(SetObjectInstance);
2418 } else if (field_jty == kLong || field_jty == kDouble) {
2419 runtime_func = irb_.GetRuntime(Set64Instance);
2420 } else {
2421 runtime_func = irb_.GetRuntime(Set32Instance);
2422 }
2423
2424 llvm::Value* field_idx_value = irb_.getInt32(field_idx);
2425
2426 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2427
TDYa127c8dc1012012-04-19 07:03:33 -07002428 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002429
Logan Chien3b2b2e72012-03-06 16:11:45 +08002430 irb_.CreateCall4(runtime_func, field_idx_value,
2431 method_object_addr, object_addr, new_value);
Logan Chiendd6aa872012-01-03 16:06:32 +08002432
2433 EmitGuard_ExceptionLandingPad(dex_pc);
2434
2435 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002436 DCHECK_GE(field_offset, 0);
2437
Logan Chiendd6aa872012-01-03 16:06:32 +08002438 llvm::PointerType* field_type =
2439 irb_.getJType(field_jty, kField)->getPointerTo();
2440
Logan Chien933abf82012-04-11 12:24:31 +08002441 llvm::Value* field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chiendd6aa872012-01-03 16:06:32 +08002442
2443 llvm::Value* field_addr =
Logan Chien933abf82012-04-11 12:24:31 +08002444 irb_.CreatePtrDisp(object_addr, field_offset_value, field_type);
Logan Chiendd6aa872012-01-03 16:06:32 +08002445
Logan Chien933abf82012-04-11 12:24:31 +08002446 // TODO: Check is_volatile. We need to generate atomic store instruction
2447 // when is_volatile is true.
Logan Chiendd6aa872012-01-03 16:06:32 +08002448 irb_.CreateStore(new_value, field_addr);
TDYa12783bb6622012-04-17 02:20:34 -07002449
2450 if (field_jty == kObject) { // If put an object, mark the GC card table.
2451 EmitMarkGCCard(new_value, object_addr);
2452 }
Logan Chiendd6aa872012-01-03 16:06:32 +08002453 }
2454
Logan Chien70f94b42011-12-27 17:49:11 +08002455 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2456}
2457
2458
Logan Chien438c4b62012-01-17 16:06:00 +08002459llvm::Value* MethodCompiler::EmitLoadStaticStorage(uint32_t dex_pc,
2460 uint32_t type_idx) {
2461 llvm::BasicBlock* block_load_static =
2462 CreateBasicBlockWithDexPC(dex_pc, "load_static");
2463
2464 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
2465
2466 // Load static storage from dex cache
2467 llvm::Value* storage_field_addr =
2468 EmitLoadDexCacheStaticStorageFieldAddr(type_idx);
2469
2470 llvm::Value* storage_object_addr = irb_.CreateLoad(storage_field_addr);
2471
2472 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
2473
2474 // Test: Is the static storage of this class initialized?
2475 llvm::Value* equal_null =
2476 irb_.CreateICmpEQ(storage_object_addr, irb_.getJNull());
2477
2478 irb_.CreateCondBr(equal_null, block_load_static, block_cont);
2479
2480 // Failback routine to load the class object
2481 irb_.SetInsertPoint(block_load_static);
2482
2483 llvm::Function* runtime_func =
2484 irb_.GetRuntime(InitializeStaticStorage);
2485
2486 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
2487
2488 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2489
TDYa127706e9b62012-04-19 12:24:26 -07002490 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
2491
TDYa127c8dc1012012-04-19 07:03:33 -07002492 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002493
Logan Chien438c4b62012-01-17 16:06:00 +08002494 llvm::Value* loaded_storage_object_addr =
TDYa127706e9b62012-04-19 12:24:26 -07002495 irb_.CreateCall3(runtime_func, type_idx_value, method_object_addr, thread_object_addr);
Logan Chien438c4b62012-01-17 16:06:00 +08002496
2497 EmitGuard_ExceptionLandingPad(dex_pc);
2498
2499 llvm::BasicBlock* block_after_load_static = irb_.GetInsertBlock();
2500
2501 irb_.CreateBr(block_cont);
2502
2503 // Now the class object must be loaded
2504 irb_.SetInsertPoint(block_cont);
2505
2506 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
2507
2508 phi->addIncoming(storage_object_addr, block_original);
2509 phi->addIncoming(loaded_storage_object_addr, block_after_load_static);
2510
2511 return phi;
2512}
2513
2514
Logan Chien70f94b42011-12-27 17:49:11 +08002515void MethodCompiler::EmitInsn_SGet(uint32_t dex_pc,
2516 Instruction const* insn,
2517 JType field_jty) {
Logan Chien438c4b62012-01-17 16:06:00 +08002518
Elliott Hughesadb8c672012-03-06 16:49:32 -08002519 DecodedInstruction dec_insn(insn);
Logan Chien438c4b62012-01-17 16:06:00 +08002520
Logan Chien933abf82012-04-11 12:24:31 +08002521 uint32_t field_idx = dec_insn.vB;
Logan Chien438c4b62012-01-17 16:06:00 +08002522
Logan Chien933abf82012-04-11 12:24:31 +08002523 int field_offset;
2524 int ssb_index;
2525 bool is_referrers_class;
2526 bool is_volatile;
2527
2528 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
2529 field_idx, oat_compilation_unit_, field_offset, ssb_index,
2530 is_referrers_class, is_volatile, false);
Logan Chien438c4b62012-01-17 16:06:00 +08002531
2532 llvm::Value* static_field_value;
2533
Logan Chien933abf82012-04-11 12:24:31 +08002534 if (!is_fast_path) {
Logan Chien438c4b62012-01-17 16:06:00 +08002535 llvm::Function* runtime_func;
2536
2537 if (field_jty == kObject) {
2538 runtime_func = irb_.GetRuntime(GetObjectStatic);
2539 } else if (field_jty == kLong || field_jty == kDouble) {
2540 runtime_func = irb_.GetRuntime(Get64Static);
2541 } else {
2542 runtime_func = irb_.GetRuntime(Get32Static);
2543 }
2544
Elliott Hughesadb8c672012-03-06 16:49:32 -08002545 llvm::Constant* field_idx_value = irb_.getInt32(dec_insn.vB);
Logan Chien438c4b62012-01-17 16:06:00 +08002546
2547 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2548
TDYa127c8dc1012012-04-19 07:03:33 -07002549 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002550
Logan Chien438c4b62012-01-17 16:06:00 +08002551 static_field_value =
2552 irb_.CreateCall2(runtime_func, field_idx_value, method_object_addr);
2553
2554 EmitGuard_ExceptionLandingPad(dex_pc);
2555
2556 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002557 DCHECK_GE(field_offset, 0);
Logan Chien438c4b62012-01-17 16:06:00 +08002558
Logan Chien933abf82012-04-11 12:24:31 +08002559 llvm::Value* static_storage_addr = NULL;
2560
2561 if (is_referrers_class) {
2562 // Fast path, static storage base is this method's class
2563 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2564
2565 llvm::Constant* declaring_class_offset_value =
2566 irb_.getPtrEquivInt(Method::DeclaringClassOffset().Int32Value());
2567
2568 llvm::Value* static_storage_field_addr =
2569 irb_.CreatePtrDisp(method_object_addr, declaring_class_offset_value,
2570 irb_.getJObjectTy()->getPointerTo());
2571
2572 static_storage_addr = irb_.CreateLoad(static_storage_field_addr);
2573 } else {
2574 // Medium path, static storage base in a different class which
2575 // requires checks that the other class is initialized
2576 DCHECK_GE(ssb_index, 0);
2577 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
2578 }
2579
2580 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chien438c4b62012-01-17 16:06:00 +08002581
2582 llvm::Value* static_field_addr =
2583 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
2584 irb_.getJType(field_jty, kField)->getPointerTo());
2585
Logan Chien933abf82012-04-11 12:24:31 +08002586 // TODO: Check is_volatile. We need to generate atomic load instruction
2587 // when is_volatile is true.
Logan Chien438c4b62012-01-17 16:06:00 +08002588 static_field_value = irb_.CreateLoad(static_field_addr);
2589 }
2590
Elliott Hughesadb8c672012-03-06 16:49:32 -08002591 EmitStoreDalvikReg(dec_insn.vA, field_jty, kField, static_field_value);
Logan Chien438c4b62012-01-17 16:06:00 +08002592
Logan Chien70f94b42011-12-27 17:49:11 +08002593 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2594}
2595
2596
2597void MethodCompiler::EmitInsn_SPut(uint32_t dex_pc,
2598 Instruction const* insn,
2599 JType field_jty) {
Logan Chien14179c82012-01-17 17:06:34 +08002600
Elliott Hughesadb8c672012-03-06 16:49:32 -08002601 DecodedInstruction dec_insn(insn);
Logan Chien14179c82012-01-17 17:06:34 +08002602
Logan Chien933abf82012-04-11 12:24:31 +08002603 uint32_t field_idx = dec_insn.vB;
Logan Chien14179c82012-01-17 17:06:34 +08002604
Elliott Hughesadb8c672012-03-06 16:49:32 -08002605 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA, field_jty, kField);
Logan Chien14179c82012-01-17 17:06:34 +08002606
Logan Chien933abf82012-04-11 12:24:31 +08002607 int field_offset;
2608 int ssb_index;
2609 bool is_referrers_class;
2610 bool is_volatile;
2611
2612 bool is_fast_path = compiler_->ComputeStaticFieldInfo(
2613 field_idx, oat_compilation_unit_, field_offset, ssb_index,
2614 is_referrers_class, is_volatile, true);
2615
2616 if (!is_fast_path) {
Logan Chien14179c82012-01-17 17:06:34 +08002617 llvm::Function* runtime_func;
2618
2619 if (field_jty == kObject) {
2620 runtime_func = irb_.GetRuntime(SetObjectStatic);
2621 } else if (field_jty == kLong || field_jty == kDouble) {
2622 runtime_func = irb_.GetRuntime(Set64Static);
2623 } else {
2624 runtime_func = irb_.GetRuntime(Set32Static);
2625 }
2626
Elliott Hughesadb8c672012-03-06 16:49:32 -08002627 llvm::Constant* field_idx_value = irb_.getInt32(dec_insn.vB);
Logan Chien14179c82012-01-17 17:06:34 +08002628
2629 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2630
TDYa127c8dc1012012-04-19 07:03:33 -07002631 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08002632
Logan Chien14179c82012-01-17 17:06:34 +08002633 irb_.CreateCall3(runtime_func, field_idx_value,
2634 method_object_addr, new_value);
2635
2636 EmitGuard_ExceptionLandingPad(dex_pc);
2637
2638 } else {
Logan Chien933abf82012-04-11 12:24:31 +08002639 DCHECK_GE(field_offset, 0);
Logan Chien14179c82012-01-17 17:06:34 +08002640
Logan Chien933abf82012-04-11 12:24:31 +08002641 llvm::Value* static_storage_addr = NULL;
2642
2643 if (is_referrers_class) {
2644 // Fast path, static storage base is this method's class
2645 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2646
2647 llvm::Constant* declaring_class_offset_value =
2648 irb_.getPtrEquivInt(Method::DeclaringClassOffset().Int32Value());
2649
2650 llvm::Value* static_storage_field_addr =
2651 irb_.CreatePtrDisp(method_object_addr, declaring_class_offset_value,
2652 irb_.getJObjectTy()->getPointerTo());
2653
2654 static_storage_addr = irb_.CreateLoad(static_storage_field_addr);
2655 } else {
2656 // Medium path, static storage base in a different class which
2657 // requires checks that the other class is initialized
2658 DCHECK_GE(ssb_index, 0);
2659 static_storage_addr = EmitLoadStaticStorage(dex_pc, ssb_index);
2660 }
2661
2662 llvm::Value* static_field_offset_value = irb_.getPtrEquivInt(field_offset);
Logan Chien14179c82012-01-17 17:06:34 +08002663
2664 llvm::Value* static_field_addr =
2665 irb_.CreatePtrDisp(static_storage_addr, static_field_offset_value,
2666 irb_.getJType(field_jty, kField)->getPointerTo());
2667
Logan Chien933abf82012-04-11 12:24:31 +08002668 // TODO: Check is_volatile. We need to generate atomic store instruction
2669 // when is_volatile is true.
Logan Chien14179c82012-01-17 17:06:34 +08002670 irb_.CreateStore(new_value, static_field_addr);
TDYa12783bb6622012-04-17 02:20:34 -07002671
2672 if (field_jty == kObject) { // If put an object, mark the GC card table.
2673 EmitMarkGCCard(new_value, static_storage_addr);
2674 }
Logan Chien14179c82012-01-17 17:06:34 +08002675 }
2676
Logan Chien70f94b42011-12-27 17:49:11 +08002677 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2678}
2679
2680
Logan Chien1a121b92012-02-15 22:23:42 +08002681void MethodCompiler::
2682EmitLoadActualParameters(std::vector<llvm::Value*>& args,
2683 uint32_t callee_method_idx,
Elliott Hughesadb8c672012-03-06 16:49:32 -08002684 DecodedInstruction const& dec_insn,
Logan Chien7e7fabc2012-04-10 18:59:11 +08002685 InvokeArgFmt arg_fmt,
Logan Chien1a121b92012-02-15 22:23:42 +08002686 bool is_static) {
2687
2688 // Get method signature
2689 DexFile::MethodId const& method_id =
2690 dex_file_->GetMethodId(callee_method_idx);
2691
Logan Chien8faf8022012-02-24 12:25:29 +08002692 uint32_t shorty_size;
Logan Chien1a121b92012-02-15 22:23:42 +08002693 char const* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size);
Logan Chien8faf8022012-02-24 12:25:29 +08002694 CHECK_GE(shorty_size, 1u);
Logan Chien1a121b92012-02-15 22:23:42 +08002695
2696 // Load argument values according to the shorty (without "this")
2697 uint16_t reg_count = 0;
2698
2699 if (!is_static) {
2700 ++reg_count; // skip the "this" pointer
2701 }
2702
Logan Chien7e7fabc2012-04-10 18:59:11 +08002703 bool is_range = (arg_fmt == kArgRange);
2704
Logan Chien8faf8022012-02-24 12:25:29 +08002705 for (uint32_t i = 1; i < shorty_size; ++i) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002706 uint32_t reg_idx = (is_range) ? (dec_insn.vC + reg_count)
2707 : (dec_insn.arg[reg_count]);
Logan Chien1a121b92012-02-15 22:23:42 +08002708
2709 args.push_back(EmitLoadDalvikReg(reg_idx, shorty[i], kAccurate));
2710
2711 ++reg_count;
2712 if (shorty[i] == 'J' || shorty[i] == 'D') {
2713 // Wide types, such as long and double, are using a pair of registers
2714 // to store the value, so we have to increase arg_reg again.
2715 ++reg_count;
2716 }
2717 }
2718
Elliott Hughesadb8c672012-03-06 16:49:32 -08002719 DCHECK_EQ(reg_count, dec_insn.vA)
Logan Chien1a121b92012-02-15 22:23:42 +08002720 << "Actual argument mismatch for callee: "
2721 << PrettyMethod(callee_method_idx, *dex_file_);
2722}
2723
TDYa1270b686e52012-04-09 22:43:35 -07002724llvm::Value* MethodCompiler::EmitFixStub(llvm::Value* callee_method_object_addr,
2725 uint32_t method_idx,
2726 bool is_static) {
2727 // TODO: Remove this after we solve the link and trampoline related problems.
2728 llvm::Value* code_addr = irb_.CreateCall(irb_.GetRuntime(FixStub), callee_method_object_addr);
2729
2730 llvm::FunctionType* method_type = GetFunctionType(method_idx, is_static);
2731
2732 return irb_.CreatePointerCast(code_addr, method_type->getPointerTo());
TDYa12785321912012-04-01 15:24:56 -07002733}
Logan Chien1a121b92012-02-15 22:23:42 +08002734
Shih-wei Liao399ed3f2012-03-08 01:27:04 -08002735
Logan Chien7e7fabc2012-04-10 18:59:11 +08002736void MethodCompiler::EmitInsn_Invoke(uint32_t dex_pc,
2737 Instruction const* insn,
2738 InvokeType invoke_type,
2739 InvokeArgFmt arg_fmt) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08002740 DecodedInstruction dec_insn(insn);
Logan Chien46fbb412012-02-15 22:29:08 +08002741
Logan Chien61c65dc2012-02-29 03:22:30 +08002742 bool is_static = (invoke_type == kStatic);
Logan Chien7e7fabc2012-04-10 18:59:11 +08002743 uint32_t callee_method_idx = dec_insn.vB;
Logan Chien61c65dc2012-02-29 03:22:30 +08002744
Logan Chien7e7fabc2012-04-10 18:59:11 +08002745 // Compute invoke related information for compiler decision
2746 int vtable_idx = -1;
Logan Chien92ad16d2012-03-18 05:48:55 +08002747 uintptr_t direct_code = 0; // Currently unused
Logan Chienfca64372012-04-23 14:57:01 +08002748 uintptr_t direct_method = 0;
Logan Chien61c65dc2012-02-29 03:22:30 +08002749 bool is_fast_path = compiler_->
Logan Chien7e7fabc2012-04-10 18:59:11 +08002750 ComputeInvokeInfo(callee_method_idx, oat_compilation_unit_,
Logan Chien92ad16d2012-03-18 05:48:55 +08002751 invoke_type, vtable_idx, direct_code, direct_method);
Logan Chien61c65dc2012-02-29 03:22:30 +08002752
Logan Chien7e7fabc2012-04-10 18:59:11 +08002753 // Load *this* actual parameter
Logan Chien1a121b92012-02-15 22:23:42 +08002754 llvm::Value* this_addr = NULL;
2755
2756 if (!is_static) {
2757 // Test: Is *this* parameter equal to null?
Logan Chien7e7fabc2012-04-10 18:59:11 +08002758 this_addr = (arg_fmt == kArgReg) ?
2759 EmitLoadDalvikReg(dec_insn.arg[0], kObject, kAccurate):
2760 EmitLoadDalvikReg(dec_insn.vC + 0, kObject, kAccurate);
2761
Logan Chien1a121b92012-02-15 22:23:42 +08002762 EmitGuard_NullPointerException(dex_pc, this_addr);
2763 }
2764
Logan Chien7e7fabc2012-04-10 18:59:11 +08002765 // Load the method object
TDYa1274e42a592012-04-10 20:13:54 -07002766 llvm::Value* callee_method_object_addr = NULL;
Logan Chien7e7fabc2012-04-10 18:59:11 +08002767
2768 if (!is_fast_path) {
TDYa1274e42a592012-04-10 20:13:54 -07002769 callee_method_object_addr =
Logan Chien7e7fabc2012-04-10 18:59:11 +08002770 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx, invoke_type,
2771 this_addr, dex_pc, is_fast_path);
2772 } else {
2773 switch (invoke_type) {
2774 case kStatic:
2775 case kDirect:
Logan Chienfca64372012-04-23 14:57:01 +08002776 if (direct_method != 0u &&
2777 direct_method != static_cast<uintptr_t>(-1)) {
2778 callee_method_object_addr =
TDYa12717826bf2012-04-24 01:15:10 -07002779 irb_.CreateIntToPtr(irb_.getPtrEquivInt(direct_method),
2780 irb_.getJObjectTy());
Logan Chienfca64372012-04-23 14:57:01 +08002781 } else {
2782 callee_method_object_addr =
2783 EmitLoadSDCalleeMethodObjectAddr(callee_method_idx);
2784 }
Logan Chien7e7fabc2012-04-10 18:59:11 +08002785 break;
2786
2787 case kVirtual:
2788 DCHECK(vtable_idx != -1);
TDYa1274e42a592012-04-10 20:13:54 -07002789 callee_method_object_addr =
Logan Chien7e7fabc2012-04-10 18:59:11 +08002790 EmitLoadVirtualCalleeMethodObjectAddr(vtable_idx, this_addr);
2791 break;
2792
2793 case kSuper:
2794 LOG(FATAL) << "invoke-super should be promoted to invoke-direct in "
2795 "the fast path.";
2796 break;
2797
2798 case kInterface:
TDYa1274e42a592012-04-10 20:13:54 -07002799 callee_method_object_addr =
Logan Chien7e7fabc2012-04-10 18:59:11 +08002800 EmitCallRuntimeForCalleeMethodObjectAddr(callee_method_idx,
2801 invoke_type, this_addr,
2802 dex_pc, is_fast_path);
2803 break;
2804 }
2805 }
2806
Logan Chien7e7fabc2012-04-10 18:59:11 +08002807 llvm::Value* code_addr =
TDYa127ce154722012-04-21 16:43:29 -07002808 irb_.LoadFromObjectOffset(callee_method_object_addr,
2809 Method::GetCodeOffset().Int32Value(),
2810 GetFunctionType(callee_method_idx, is_static)->getPointerTo());
TDYa12785321912012-04-01 15:24:56 -07002811
Logan Chien1a121b92012-02-15 22:23:42 +08002812 // Load the actual parameter
2813 std::vector<llvm::Value*> args;
2814
2815 args.push_back(callee_method_object_addr); // method object for callee
2816
2817 if (!is_static) {
Logan Chien7e7fabc2012-04-10 18:59:11 +08002818 DCHECK(this_addr != NULL);
Logan Chien1a121b92012-02-15 22:23:42 +08002819 args.push_back(this_addr); // "this" object for callee
2820 }
2821
2822 EmitLoadActualParameters(args, callee_method_idx, dec_insn,
Logan Chien7e7fabc2012-04-10 18:59:11 +08002823 arg_fmt, is_static);
Logan Chien1a121b92012-02-15 22:23:42 +08002824
TDYa1275bb86012012-04-11 05:57:28 -07002825#if 0
Logan Chien8dfcbea2012-02-17 18:50:32 +08002826 // Invoke callee
TDYa127c8dc1012012-04-19 07:03:33 -07002827 EmitUpdateDexPC(dex_pc);
Logan Chien1a121b92012-02-15 22:23:42 +08002828 llvm::Value* retval = irb_.CreateCall(code_addr, args);
2829 EmitGuard_ExceptionLandingPad(dex_pc);
2830
Logan Chien7e7fabc2012-04-10 18:59:11 +08002831 uint32_t callee_access_flags = is_static ? kAccStatic : 0;
2832 UniquePtr<OatCompilationUnit> callee_oat_compilation_unit(
2833 oat_compilation_unit_->GetCallee(callee_method_idx, callee_access_flags));
2834
2835 char ret_shorty = callee_oat_compilation_unit->GetShorty()[0];
Shih-wei Liao90d50992012-02-19 03:32:05 -08002836 if (ret_shorty != 'V') {
Logan Chien1a121b92012-02-15 22:23:42 +08002837 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
2838 }
TDYa1275bb86012012-04-11 05:57:28 -07002839#else
2840 uint32_t callee_access_flags = is_static ? kAccStatic : 0;
2841 UniquePtr<OatCompilationUnit> callee_oat_compilation_unit(
2842 oat_compilation_unit_->GetCallee(callee_method_idx, callee_access_flags));
2843
2844 char ret_shorty = callee_oat_compilation_unit->GetShorty()[0];
2845
2846
TDYa127c8dc1012012-04-19 07:03:33 -07002847 EmitUpdateDexPC(dex_pc);
TDYa1275bb86012012-04-11 05:57:28 -07002848
2849
2850 llvm::BasicBlock* block_normal = CreateBasicBlockWithDexPC(dex_pc, "normal");
TDYa127ce154722012-04-21 16:43:29 -07002851 llvm::BasicBlock* block_stub = CreateBasicBlockWithDexPC(dex_pc, "stub");
TDYa1275bb86012012-04-11 05:57:28 -07002852 llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont");
2853
2854 llvm::Type* accurate_ret_type = irb_.getJType(ret_shorty, kAccurate);
2855 llvm::Value* retval_addr = NULL;
2856 if (ret_shorty != 'V') {
2857 retval_addr = irb_.CreateAlloca(accurate_ret_type);
2858 }
2859
2860
TDYa1275bb86012012-04-11 05:57:28 -07002861 llvm::Value* code_addr_int = irb_.CreatePtrToInt(code_addr, irb_.getPtrEquivIntTy());
TDYa127ce154722012-04-21 16:43:29 -07002862 llvm::Value* max_stub_int = irb_.getPtrEquivInt(special_stub::kMaxSpecialStub);
2863 llvm::Value* is_stub = irb_.CreateICmpULT(code_addr_int, max_stub_int);
2864 irb_.CreateCondBr(is_stub, block_stub, block_normal);
TDYa1275bb86012012-04-11 05:57:28 -07002865
2866
2867 irb_.SetInsertPoint(block_normal);
2868 {
2869 // Invoke callee
TDYa127ce154722012-04-21 16:43:29 -07002870 llvm::Value* retval = irb_.CreateCall(code_addr, args);
TDYa1275bb86012012-04-11 05:57:28 -07002871 if (ret_shorty != 'V') {
TDYa127ce154722012-04-21 16:43:29 -07002872 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
TDYa1275bb86012012-04-11 05:57:28 -07002873 }
2874 }
2875 irb_.CreateBr(block_continue);
2876
2877
TDYa127ce154722012-04-21 16:43:29 -07002878 irb_.SetInsertPoint(block_stub);
TDYa1275bb86012012-04-11 05:57:28 -07002879 {
TDYa127ce154722012-04-21 16:43:29 -07002880 { // lazy link
2881 // TODO: Remove this after we solve the link problem.
2882 llvm::BasicBlock* block_proxy_stub = CreateBasicBlockWithDexPC(dex_pc, "proxy");
2883 llvm::BasicBlock* block_link = CreateBasicBlockWithDexPC(dex_pc, "link");
2884
2885 irb_.CreateCondBr(irb_.CreateIsNull(code_addr), block_link, block_proxy_stub);
2886
2887
2888 irb_.SetInsertPoint(block_link);
2889 code_addr = EmitFixStub(callee_method_object_addr, callee_method_idx, is_static);
2890
2891 EmitGuard_ExceptionLandingPad(dex_pc);
2892
2893 llvm::Value* retval = irb_.CreateCall(code_addr, args);
2894 if (ret_shorty != 'V') {
2895 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
2896 }
2897 irb_.CreateBr(block_continue);
2898
2899
2900 irb_.SetInsertPoint(block_proxy_stub);
TDYa1275bb86012012-04-11 05:57:28 -07002901 }
TDYa127ce154722012-04-21 16:43:29 -07002902 { // proxy stub
2903 llvm::Value* temp_space_addr;
2904 if (ret_shorty != 'V') {
2905 temp_space_addr = irb_.CreateAlloca(irb_.getJValueTy());
2906 args.push_back(temp_space_addr);
2907 }
2908 // TODO: Remove this after we solve the proxy trampoline calling convention problem.
2909 irb_.CreateCall(irb_.GetRuntime(ProxyInvokeHandler), args);
2910 if (ret_shorty != 'V') {
2911 llvm::Value* result_addr =
2912 irb_.CreateBitCast(temp_space_addr, accurate_ret_type->getPointerTo());
2913 llvm::Value* retval = irb_.CreateLoad(result_addr);
2914 EmitStoreDalvikRetValReg(ret_shorty, kAccurate, retval);
2915 }
TDYa1275bb86012012-04-11 05:57:28 -07002916 }
2917 }
2918 irb_.CreateBr(block_continue);
2919
2920
2921 irb_.SetInsertPoint(block_continue);
2922
TDYa1275bb86012012-04-11 05:57:28 -07002923 EmitGuard_ExceptionLandingPad(dex_pc);
2924#endif
Logan Chien1a121b92012-02-15 22:23:42 +08002925
Logan Chien70f94b42011-12-27 17:49:11 +08002926 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2927}
2928
2929
Logan Chien7e7fabc2012-04-10 18:59:11 +08002930llvm::Value* MethodCompiler::
2931EmitLoadSDCalleeMethodObjectAddr(uint32_t callee_method_idx) {
2932 llvm::Value* callee_method_object_field_addr =
2933 EmitLoadDexCacheResolvedMethodFieldAddr(callee_method_idx);
Logan Chien7caf37e2012-02-03 22:56:04 +08002934
Logan Chien7e7fabc2012-04-10 18:59:11 +08002935 return irb_.CreateLoad(callee_method_object_field_addr);
2936}
Logan Chien7caf37e2012-02-03 22:56:04 +08002937
Logan Chien7caf37e2012-02-03 22:56:04 +08002938
Logan Chien7e7fabc2012-04-10 18:59:11 +08002939llvm::Value* MethodCompiler::
2940EmitLoadVirtualCalleeMethodObjectAddr(int vtable_idx,
2941 llvm::Value* this_addr) {
2942 // Load class object of *this* pointer
2943 llvm::Constant* class_field_offset_value =
2944 irb_.getPtrEquivInt(Object::ClassOffset().Int32Value());
Logan Chien7caf37e2012-02-03 22:56:04 +08002945
Logan Chien7e7fabc2012-04-10 18:59:11 +08002946 llvm::Value* class_field_addr =
2947 irb_.CreatePtrDisp(this_addr, class_field_offset_value,
2948 irb_.getJObjectTy()->getPointerTo());
Logan Chien7caf37e2012-02-03 22:56:04 +08002949
Logan Chien7e7fabc2012-04-10 18:59:11 +08002950 llvm::Value* class_object_addr = irb_.CreateLoad(class_field_addr);
Logan Chien7caf37e2012-02-03 22:56:04 +08002951
Logan Chien7e7fabc2012-04-10 18:59:11 +08002952 // Load vtable address
2953 llvm::Constant* vtable_offset_value =
2954 irb_.getPtrEquivInt(Class::VTableOffset().Int32Value());
2955
2956 llvm::Value* vtable_field_addr =
2957 irb_.CreatePtrDisp(class_object_addr, vtable_offset_value,
2958 irb_.getJObjectTy()->getPointerTo());
2959
2960 llvm::Value* vtable_addr = irb_.CreateLoad(vtable_field_addr);
2961
2962 // Load callee method object
2963 llvm::Value* vtable_idx_value =
2964 irb_.getPtrEquivInt(static_cast<uint64_t>(vtable_idx));
2965
2966 llvm::Value* method_field_addr =
2967 EmitArrayGEP(vtable_addr, vtable_idx_value, irb_.getJObjectTy(), kObject);
2968
2969 return irb_.CreateLoad(method_field_addr);
2970}
2971
2972
2973llvm::Value* MethodCompiler::
2974EmitCallRuntimeForCalleeMethodObjectAddr(uint32_t callee_method_idx,
2975 InvokeType invoke_type,
2976 llvm::Value* this_addr,
2977 uint32_t dex_pc,
2978 bool is_fast_path) {
2979
2980 llvm::Function* runtime_func = NULL;
2981
2982 switch (invoke_type) {
2983 case kStatic:
2984 runtime_func = irb_.GetRuntime(FindStaticMethodWithAccessCheck);
2985 break;
2986
2987 case kDirect:
2988 runtime_func = irb_.GetRuntime(FindDirectMethodWithAccessCheck);
2989 break;
2990
2991 case kVirtual:
2992 runtime_func = irb_.GetRuntime(FindVirtualMethodWithAccessCheck);
2993 break;
2994
2995 case kSuper:
2996 runtime_func = irb_.GetRuntime(FindSuperMethodWithAccessCheck);
2997 break;
2998
2999 case kInterface:
3000 if (is_fast_path) {
3001 runtime_func = irb_.GetRuntime(FindInterfaceMethod);
3002 } else {
3003 runtime_func = irb_.GetRuntime(FindInterfaceMethodWithAccessCheck);
3004 }
3005 break;
3006 }
Logan Chien7caf37e2012-02-03 22:56:04 +08003007
3008 llvm::Value* callee_method_idx_value = irb_.getInt32(callee_method_idx);
3009
Logan Chien7e7fabc2012-04-10 18:59:11 +08003010 if (this_addr == NULL) {
3011 DCHECK_EQ(invoke_type, kStatic);
3012 this_addr = irb_.getJNull();
3013 }
3014
3015 llvm::Value* caller_method_object_addr = EmitLoadMethodObjectAddr();
3016
TDYa127da83d972012-04-18 00:21:49 -07003017 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
3018
TDYa127c8dc1012012-04-19 07:03:33 -07003019 EmitUpdateDexPC(dex_pc);
Logan Chien8dfcbea2012-02-17 18:50:32 +08003020
TDYa1270b686e52012-04-09 22:43:35 -07003021 llvm::Value* callee_method_object_addr =
TDYa127da83d972012-04-18 00:21:49 -07003022 irb_.CreateCall4(runtime_func,
Logan Chien7e7fabc2012-04-10 18:59:11 +08003023 callee_method_idx_value,
3024 this_addr,
TDYa127da83d972012-04-18 00:21:49 -07003025 caller_method_object_addr,
3026 thread_object_addr);
Logan Chien7caf37e2012-02-03 22:56:04 +08003027
3028 EmitGuard_ExceptionLandingPad(dex_pc);
3029
Logan Chien7e7fabc2012-04-10 18:59:11 +08003030 return callee_method_object_addr;
Logan Chien70f94b42011-12-27 17:49:11 +08003031}
3032
3033
3034void MethodCompiler::EmitInsn_Neg(uint32_t dex_pc,
3035 Instruction const* insn,
3036 JType op_jty) {
Logan Chien1b5685f2011-12-27 18:01:14 +08003037
Elliott Hughesadb8c672012-03-06 16:49:32 -08003038 DecodedInstruction dec_insn(insn);
Logan Chien1b5685f2011-12-27 18:01:14 +08003039
3040 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3041
Elliott Hughesadb8c672012-03-06 16:49:32 -08003042 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chien1b5685f2011-12-27 18:01:14 +08003043 llvm::Value* result_value = irb_.CreateNeg(src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003044 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chien1b5685f2011-12-27 18:01:14 +08003045
Logan Chien70f94b42011-12-27 17:49:11 +08003046 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3047}
3048
3049
3050void MethodCompiler::EmitInsn_Not(uint32_t dex_pc,
3051 Instruction const* insn,
3052 JType op_jty) {
Logan Chiene53750d2011-12-27 18:02:27 +08003053
Elliott Hughesadb8c672012-03-06 16:49:32 -08003054 DecodedInstruction dec_insn(insn);
Logan Chiene53750d2011-12-27 18:02:27 +08003055
3056 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3057
Elliott Hughesadb8c672012-03-06 16:49:32 -08003058 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chiene53750d2011-12-27 18:02:27 +08003059 llvm::Value* result_value =
3060 irb_.CreateXor(src_value, static_cast<uint64_t>(-1));
3061
Elliott Hughesadb8c672012-03-06 16:49:32 -08003062 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chiene53750d2011-12-27 18:02:27 +08003063
Logan Chien70f94b42011-12-27 17:49:11 +08003064 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3065}
3066
3067
3068void MethodCompiler::EmitInsn_SExt(uint32_t dex_pc,
3069 Instruction const* insn) {
Logan Chien61752ad2011-12-27 18:03:51 +08003070
Elliott Hughesadb8c672012-03-06 16:49:32 -08003071 DecodedInstruction dec_insn(insn);
Logan Chien61752ad2011-12-27 18:03:51 +08003072
Elliott Hughesadb8c672012-03-06 16:49:32 -08003073 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chien61752ad2011-12-27 18:03:51 +08003074 llvm::Value* result_value = irb_.CreateSExt(src_value, irb_.getJLongTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003075 EmitStoreDalvikReg(dec_insn.vA, kLong, kAccurate, result_value);
Logan Chien61752ad2011-12-27 18:03:51 +08003076
Logan Chien70f94b42011-12-27 17:49:11 +08003077 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3078}
3079
3080
3081void MethodCompiler::EmitInsn_Trunc(uint32_t dex_pc,
3082 Instruction const* insn) {
Logan Chien17a57662011-12-27 18:05:14 +08003083
Elliott Hughesadb8c672012-03-06 16:49:32 -08003084 DecodedInstruction dec_insn(insn);
Logan Chien17a57662011-12-27 18:05:14 +08003085
Elliott Hughesadb8c672012-03-06 16:49:32 -08003086 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kLong, kAccurate);
Logan Chien17a57662011-12-27 18:05:14 +08003087 llvm::Value* result_value = irb_.CreateTrunc(src_value, irb_.getJIntTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003088 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chien17a57662011-12-27 18:05:14 +08003089
Logan Chien70f94b42011-12-27 17:49:11 +08003090 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3091}
3092
3093
3094void MethodCompiler::EmitInsn_TruncAndSExt(uint32_t dex_pc,
3095 Instruction const* insn,
3096 unsigned N) {
Logan Chienb6744c52011-12-27 18:06:26 +08003097
Elliott Hughesadb8c672012-03-06 16:49:32 -08003098 DecodedInstruction dec_insn(insn);
Logan Chienb6744c52011-12-27 18:06:26 +08003099
Elliott Hughesadb8c672012-03-06 16:49:32 -08003100 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chienb6744c52011-12-27 18:06:26 +08003101
3102 llvm::Value* trunc_value =
3103 irb_.CreateTrunc(src_value, llvm::Type::getIntNTy(*context_, N));
3104
3105 llvm::Value* result_value = irb_.CreateSExt(trunc_value, irb_.getJIntTy());
3106
Elliott Hughesadb8c672012-03-06 16:49:32 -08003107 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chienb6744c52011-12-27 18:06:26 +08003108
Logan Chien70f94b42011-12-27 17:49:11 +08003109 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3110}
3111
3112
3113void MethodCompiler::EmitInsn_TruncAndZExt(uint32_t dex_pc,
3114 Instruction const* insn,
3115 unsigned N) {
Logan Chienb6744c52011-12-27 18:06:26 +08003116
Elliott Hughesadb8c672012-03-06 16:49:32 -08003117 DecodedInstruction dec_insn(insn);
Logan Chienb6744c52011-12-27 18:06:26 +08003118
Elliott Hughesadb8c672012-03-06 16:49:32 -08003119 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chienb6744c52011-12-27 18:06:26 +08003120
3121 llvm::Value* trunc_value =
3122 irb_.CreateTrunc(src_value, llvm::Type::getIntNTy(*context_, N));
3123
3124 llvm::Value* result_value = irb_.CreateZExt(trunc_value, irb_.getJIntTy());
3125
Elliott Hughesadb8c672012-03-06 16:49:32 -08003126 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chienb6744c52011-12-27 18:06:26 +08003127
Logan Chien70f94b42011-12-27 17:49:11 +08003128 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3129}
3130
3131
3132void MethodCompiler::EmitInsn_FNeg(uint32_t dex_pc,
3133 Instruction const* insn,
3134 JType op_jty) {
Logan Chien7a48b092011-12-27 18:07:45 +08003135
Elliott Hughesadb8c672012-03-06 16:49:32 -08003136 DecodedInstruction dec_insn(insn);
Logan Chien7a48b092011-12-27 18:07:45 +08003137
3138 DCHECK(op_jty == kFloat || op_jty == kDouble) << op_jty;
3139
Elliott Hughesadb8c672012-03-06 16:49:32 -08003140 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chien7a48b092011-12-27 18:07:45 +08003141 llvm::Value* result_value = irb_.CreateFNeg(src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003142 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chien7a48b092011-12-27 18:07:45 +08003143
Logan Chien70f94b42011-12-27 17:49:11 +08003144 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3145}
3146
3147
3148void MethodCompiler::EmitInsn_IntToFP(uint32_t dex_pc,
3149 Instruction const* insn,
3150 JType src_jty,
3151 JType dest_jty) {
Logan Chien62dd4532011-12-27 18:09:00 +08003152
Elliott Hughesadb8c672012-03-06 16:49:32 -08003153 DecodedInstruction dec_insn(insn);
Logan Chien62dd4532011-12-27 18:09:00 +08003154
3155 DCHECK(src_jty == kInt || src_jty == kLong) << src_jty;
3156 DCHECK(dest_jty == kFloat || dest_jty == kDouble) << dest_jty;
3157
Elliott Hughesadb8c672012-03-06 16:49:32 -08003158 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, src_jty, kAccurate);
Logan Chien62dd4532011-12-27 18:09:00 +08003159 llvm::Type* dest_type = irb_.getJType(dest_jty, kAccurate);
3160 llvm::Value* dest_value = irb_.CreateSIToFP(src_value, dest_type);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003161 EmitStoreDalvikReg(dec_insn.vA, dest_jty, kAccurate, dest_value);
Logan Chien62dd4532011-12-27 18:09:00 +08003162
Logan Chien70f94b42011-12-27 17:49:11 +08003163 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3164}
3165
3166
3167void MethodCompiler::EmitInsn_FPToInt(uint32_t dex_pc,
3168 Instruction const* insn,
3169 JType src_jty,
TDYa127a4746872012-04-11 23:48:55 -07003170 JType dest_jty,
3171 runtime_support::RuntimeId runtime_func_id) {
Logan Chien12dc1752011-12-27 18:10:15 +08003172
Elliott Hughesadb8c672012-03-06 16:49:32 -08003173 DecodedInstruction dec_insn(insn);
Logan Chien12dc1752011-12-27 18:10:15 +08003174
3175 DCHECK(src_jty == kFloat || src_jty == kDouble) << src_jty;
3176 DCHECK(dest_jty == kInt || dest_jty == kLong) << dest_jty;
3177
Elliott Hughesadb8c672012-03-06 16:49:32 -08003178 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, src_jty, kAccurate);
TDYa127a4746872012-04-11 23:48:55 -07003179 llvm::Value* dest_value = irb_.CreateCall(irb_.GetRuntime(runtime_func_id), src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003180 EmitStoreDalvikReg(dec_insn.vA, dest_jty, kAccurate, dest_value);
Logan Chien12dc1752011-12-27 18:10:15 +08003181
Logan Chien70f94b42011-12-27 17:49:11 +08003182 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3183}
3184
3185
3186void MethodCompiler::EmitInsn_FExt(uint32_t dex_pc,
3187 Instruction const* insn) {
Logan Chienc56ded92011-12-27 18:10:57 +08003188
Elliott Hughesadb8c672012-03-06 16:49:32 -08003189 DecodedInstruction dec_insn(insn);
Logan Chienc56ded92011-12-27 18:10:57 +08003190
Elliott Hughesadb8c672012-03-06 16:49:32 -08003191 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kFloat, kAccurate);
Logan Chienc56ded92011-12-27 18:10:57 +08003192 llvm::Value* result_value = irb_.CreateFPExt(src_value, irb_.getJDoubleTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003193 EmitStoreDalvikReg(dec_insn.vA, kDouble, kAccurate, result_value);
Logan Chienc56ded92011-12-27 18:10:57 +08003194
Logan Chien70f94b42011-12-27 17:49:11 +08003195 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3196}
3197
3198
3199void MethodCompiler::EmitInsn_FTrunc(uint32_t dex_pc,
3200 Instruction const* insn) {
Logan Chien927744f2011-12-27 18:11:52 +08003201
Elliott Hughesadb8c672012-03-06 16:49:32 -08003202 DecodedInstruction dec_insn(insn);
Logan Chien927744f2011-12-27 18:11:52 +08003203
Elliott Hughesadb8c672012-03-06 16:49:32 -08003204 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kDouble, kAccurate);
Logan Chien927744f2011-12-27 18:11:52 +08003205 llvm::Value* result_value = irb_.CreateFPTrunc(src_value, irb_.getJFloatTy());
Elliott Hughesadb8c672012-03-06 16:49:32 -08003206 EmitStoreDalvikReg(dec_insn.vA, kFloat, kAccurate, result_value);
Logan Chien927744f2011-12-27 18:11:52 +08003207
Logan Chien70f94b42011-12-27 17:49:11 +08003208 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3209}
3210
3211
3212void MethodCompiler::EmitInsn_IntArithm(uint32_t dex_pc,
3213 Instruction const* insn,
3214 IntArithmKind arithm,
3215 JType op_jty,
3216 bool is_2addr) {
Logan Chienc3f7d962011-12-27 18:13:18 +08003217
Elliott Hughesadb8c672012-03-06 16:49:32 -08003218 DecodedInstruction dec_insn(insn);
Logan Chienc3f7d962011-12-27 18:13:18 +08003219
3220 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3221
3222 llvm::Value* src1_value;
3223 llvm::Value* src2_value;
3224
3225 if (is_2addr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003226 src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate);
3227 src2_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chienc3f7d962011-12-27 18:13:18 +08003228 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003229 src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
3230 src2_value = EmitLoadDalvikReg(dec_insn.vC, op_jty, kAccurate);
Logan Chienc3f7d962011-12-27 18:13:18 +08003231 }
3232
3233 llvm::Value* result_value =
3234 EmitIntArithmResultComputation(dex_pc, src1_value, src2_value,
3235 arithm, op_jty);
3236
Elliott Hughesadb8c672012-03-06 16:49:32 -08003237 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chienc3f7d962011-12-27 18:13:18 +08003238
Logan Chien70f94b42011-12-27 17:49:11 +08003239 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3240}
3241
3242
3243void MethodCompiler::EmitInsn_IntArithmImmediate(uint32_t dex_pc,
3244 Instruction const* insn,
3245 IntArithmKind arithm) {
Logan Chienc3f7d962011-12-27 18:13:18 +08003246
Elliott Hughesadb8c672012-03-06 16:49:32 -08003247 DecodedInstruction dec_insn(insn);
Logan Chienc3f7d962011-12-27 18:13:18 +08003248
Elliott Hughesadb8c672012-03-06 16:49:32 -08003249 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
Logan Chienc3f7d962011-12-27 18:13:18 +08003250
Elliott Hughesadb8c672012-03-06 16:49:32 -08003251 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC);
Logan Chienc3f7d962011-12-27 18:13:18 +08003252
3253 llvm::Value* result_value =
3254 EmitIntArithmResultComputation(dex_pc, src_value, imm_value, arithm, kInt);
3255
Elliott Hughesadb8c672012-03-06 16:49:32 -08003256 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chienc3f7d962011-12-27 18:13:18 +08003257
Logan Chien70f94b42011-12-27 17:49:11 +08003258 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3259}
3260
3261
Logan Chienc3f7d962011-12-27 18:13:18 +08003262llvm::Value*
3263MethodCompiler::EmitIntArithmResultComputation(uint32_t dex_pc,
3264 llvm::Value* lhs,
3265 llvm::Value* rhs,
3266 IntArithmKind arithm,
3267 JType op_jty) {
3268 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3269
3270 switch (arithm) {
3271 case kIntArithm_Add:
3272 return irb_.CreateAdd(lhs, rhs);
3273
3274 case kIntArithm_Sub:
3275 return irb_.CreateSub(lhs, rhs);
3276
3277 case kIntArithm_Mul:
3278 return irb_.CreateMul(lhs, rhs);
3279
3280 case kIntArithm_Div:
Logan Chienc3f7d962011-12-27 18:13:18 +08003281 case kIntArithm_Rem:
TDYa127f8641ce2012-04-02 06:40:40 -07003282 return EmitIntDivRemResultComputation(dex_pc, lhs, rhs, arithm, op_jty);
Logan Chienc3f7d962011-12-27 18:13:18 +08003283
3284 case kIntArithm_And:
3285 return irb_.CreateAnd(lhs, rhs);
3286
3287 case kIntArithm_Or:
3288 return irb_.CreateOr(lhs, rhs);
3289
3290 case kIntArithm_Xor:
3291 return irb_.CreateXor(lhs, rhs);
3292
Logan Chienc3f7d962011-12-27 18:13:18 +08003293 default:
3294 LOG(FATAL) << "Unknown integer arithmetic kind: " << arithm;
3295 return NULL;
3296 }
3297}
3298
3299
TDYa127f8641ce2012-04-02 06:40:40 -07003300llvm::Value*
3301MethodCompiler::EmitIntDivRemResultComputation(uint32_t dex_pc,
3302 llvm::Value* dividend,
3303 llvm::Value* divisor,
3304 IntArithmKind arithm,
3305 JType op_jty) {
3306 // Throw exception if the divisor is 0.
3307 EmitGuard_DivZeroException(dex_pc, divisor, op_jty);
3308
3309 // Check the special case: MININT / -1 = MININT
3310 // That case will cause overflow, which is undefined behavior in llvm.
3311 // So we check the divisor is -1 or not, if the divisor is -1, we do
3312 // the special path to avoid undefined behavior.
3313 llvm::Type* op_type = irb_.getJType(op_jty, kAccurate);
3314 llvm::Value* zero = irb_.getJZero(op_jty);
3315 llvm::Value* neg_one = llvm::ConstantInt::getSigned(op_type, -1);
3316 llvm::Value* result = irb_.CreateAlloca(op_type);
3317
3318 llvm::BasicBlock* eq_neg_one = CreateBasicBlockWithDexPC(dex_pc, "eq_neg_one");
3319 llvm::BasicBlock* ne_neg_one = CreateBasicBlockWithDexPC(dex_pc, "ne_neg_one");
3320 llvm::BasicBlock* neg_one_cont = CreateBasicBlockWithDexPC(dex_pc, "neg_one_cont");
3321
3322 llvm::Value* is_equal_neg_one = EmitConditionResult(divisor, neg_one, kCondBranch_EQ);
3323 irb_.CreateCondBr(is_equal_neg_one, eq_neg_one, ne_neg_one);
3324
3325 // If divisor == -1
3326 irb_.SetInsertPoint(eq_neg_one);
3327 llvm::Value* eq_result;
3328 if (arithm == kIntArithm_Div) {
3329 // We can just change from "dividend div -1" to "neg dividend".
3330 // The sub don't care the sign/unsigned because of two's complement representation.
3331 // And the behavior is what we want:
3332 // -(2^n) (2^n)-1
3333 // MININT < k <= MAXINT -> mul k -1 = -k
3334 // MININT == k -> mul k -1 = k
3335 //
3336 // LLVM use sub to represent 'neg'
3337 eq_result = irb_.CreateSub(zero, dividend);
3338 } else {
3339 // Everything modulo -1 will be 0.
3340 eq_result = zero;
3341 }
3342 irb_.CreateStore(eq_result, result);
3343 irb_.CreateBr(neg_one_cont);
3344
3345 // If divisor != -1, just do the division.
3346 irb_.SetInsertPoint(ne_neg_one);
3347 llvm::Value* ne_result;
3348 if (arithm == kIntArithm_Div) {
3349 ne_result = irb_.CreateSDiv(dividend, divisor);
3350 } else {
3351 ne_result = irb_.CreateSRem(dividend, divisor);
3352 }
3353 irb_.CreateStore(ne_result, result);
3354 irb_.CreateBr(neg_one_cont);
3355
3356 irb_.SetInsertPoint(neg_one_cont);
3357 return irb_.CreateLoad(result);
3358}
3359
3360
Logan Chien5539ad02012-04-02 14:36:55 +08003361void MethodCompiler::EmitInsn_IntShiftArithm(uint32_t dex_pc,
3362 Instruction const* insn,
3363 IntShiftArithmKind arithm,
3364 JType op_jty,
3365 bool is_2addr) {
3366
3367 DecodedInstruction dec_insn(insn);
3368
3369 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3370
3371 llvm::Value* src1_value;
3372 llvm::Value* src2_value;
3373
3374 // NOTE: The 2nd operand of the shift arithmetic instruction is
3375 // 32-bit integer regardless of the 1st operand.
3376 if (is_2addr) {
3377 src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate);
3378 src2_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
3379 } else {
3380 src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
3381 src2_value = EmitLoadDalvikReg(dec_insn.vC, kInt, kAccurate);
3382 }
3383
3384 llvm::Value* result_value =
3385 EmitIntShiftArithmResultComputation(dex_pc, src1_value, src2_value,
3386 arithm, op_jty);
3387
3388 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
3389
3390 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3391}
3392
3393
3394void MethodCompiler::
3395EmitInsn_IntShiftArithmImmediate(uint32_t dex_pc,
3396 Instruction const* insn,
3397 IntShiftArithmKind arithm) {
3398
3399 DecodedInstruction dec_insn(insn);
3400
3401 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
3402
3403 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC);
3404
3405 llvm::Value* result_value =
3406 EmitIntShiftArithmResultComputation(dex_pc, src_value, imm_value,
3407 arithm, kInt);
3408
3409 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
3410
3411 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3412}
3413
3414
3415llvm::Value*
3416MethodCompiler::EmitIntShiftArithmResultComputation(uint32_t dex_pc,
3417 llvm::Value* lhs,
3418 llvm::Value* rhs,
3419 IntShiftArithmKind arithm,
3420 JType op_jty) {
3421 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3422
3423 if (op_jty == kInt) {
3424 rhs = irb_.CreateAnd(rhs, 0x1f);
3425 } else {
3426 llvm::Value* masked_rhs = irb_.CreateAnd(rhs, 0x3f);
3427 rhs = irb_.CreateZExt(masked_rhs, irb_.getJLongTy());
3428 }
3429
3430 switch (arithm) {
3431 case kIntArithm_Shl:
3432 return irb_.CreateShl(lhs, rhs);
3433
3434 case kIntArithm_Shr:
3435 return irb_.CreateAShr(lhs, rhs);
3436
3437 case kIntArithm_UShr:
3438 return irb_.CreateLShr(lhs, rhs);
3439
3440 default:
3441 LOG(FATAL) << "Unknown integer shift arithmetic kind: " << arithm;
3442 return NULL;
3443 }
3444}
3445
3446
Logan Chien70f94b42011-12-27 17:49:11 +08003447void MethodCompiler::EmitInsn_RSubImmediate(uint32_t dex_pc,
3448 Instruction const* insn) {
Logan Chien65c62d42011-12-27 18:14:18 +08003449
Elliott Hughesadb8c672012-03-06 16:49:32 -08003450 DecodedInstruction dec_insn(insn);
Logan Chien65c62d42011-12-27 18:14:18 +08003451
Elliott Hughesadb8c672012-03-06 16:49:32 -08003452 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB, kInt, kAccurate);
3453 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC);
Logan Chien65c62d42011-12-27 18:14:18 +08003454 llvm::Value* result_value = irb_.CreateSub(imm_value, src_value);
Elliott Hughesadb8c672012-03-06 16:49:32 -08003455 EmitStoreDalvikReg(dec_insn.vA, kInt, kAccurate, result_value);
Logan Chien65c62d42011-12-27 18:14:18 +08003456
Logan Chien70f94b42011-12-27 17:49:11 +08003457 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3458}
3459
3460
3461void MethodCompiler::EmitInsn_FPArithm(uint32_t dex_pc,
3462 Instruction const* insn,
3463 FPArithmKind arithm,
3464 JType op_jty,
3465 bool is_2addr) {
Logan Chien76e1c792011-12-27 18:15:01 +08003466
Elliott Hughesadb8c672012-03-06 16:49:32 -08003467 DecodedInstruction dec_insn(insn);
Logan Chien76e1c792011-12-27 18:15:01 +08003468
3469 DCHECK(op_jty == kFloat || op_jty == kDouble) << op_jty;
3470
3471 llvm::Value* src1_value;
3472 llvm::Value* src2_value;
3473
3474 if (is_2addr) {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003475 src1_value = EmitLoadDalvikReg(dec_insn.vA, op_jty, kAccurate);
3476 src2_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
Logan Chien76e1c792011-12-27 18:15:01 +08003477 } else {
Elliott Hughesadb8c672012-03-06 16:49:32 -08003478 src1_value = EmitLoadDalvikReg(dec_insn.vB, op_jty, kAccurate);
3479 src2_value = EmitLoadDalvikReg(dec_insn.vC, op_jty, kAccurate);
Logan Chien76e1c792011-12-27 18:15:01 +08003480 }
3481
3482 llvm::Value* result_value =
3483 EmitFPArithmResultComputation(dex_pc, src1_value, src2_value, arithm);
3484
Elliott Hughesadb8c672012-03-06 16:49:32 -08003485 EmitStoreDalvikReg(dec_insn.vA, op_jty, kAccurate, result_value);
Logan Chien76e1c792011-12-27 18:15:01 +08003486
Logan Chien70f94b42011-12-27 17:49:11 +08003487 irb_.CreateBr(GetNextBasicBlock(dex_pc));
3488}
3489
3490
Logan Chien76e1c792011-12-27 18:15:01 +08003491llvm::Value*
3492MethodCompiler::EmitFPArithmResultComputation(uint32_t dex_pc,
3493 llvm::Value *lhs,
3494 llvm::Value *rhs,
3495 FPArithmKind arithm) {
3496 switch (arithm) {
3497 case kFPArithm_Add:
3498 return irb_.CreateFAdd(lhs, rhs);
3499
3500 case kFPArithm_Sub:
3501 return irb_.CreateFSub(lhs, rhs);
3502
3503 case kFPArithm_Mul:
3504 return irb_.CreateFMul(lhs, rhs);
3505
3506 case kFPArithm_Div:
3507 return irb_.CreateFDiv(lhs, rhs);
3508
3509 case kFPArithm_Rem:
3510 return irb_.CreateFRem(lhs, rhs);
3511
3512 default:
3513 LOG(FATAL) << "Unknown floating-point arithmetic kind: " << arithm;
3514 return NULL;
3515 }
3516}
3517
3518
Logan Chienc3f7d962011-12-27 18:13:18 +08003519void MethodCompiler::EmitGuard_DivZeroException(uint32_t dex_pc,
3520 llvm::Value* denominator,
3521 JType op_jty) {
3522 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
3523
3524 llvm::Constant* zero = irb_.getJZero(op_jty);
3525
3526 llvm::Value* equal_zero = irb_.CreateICmpEQ(denominator, zero);
3527
3528 llvm::BasicBlock* block_exception = CreateBasicBlockWithDexPC(dex_pc, "div0");
3529
3530 llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont");
3531
3532 irb_.CreateCondBr(equal_zero, block_exception, block_continue);
3533
3534 irb_.SetInsertPoint(block_exception);
TDYa127c8dc1012012-04-19 07:03:33 -07003535 EmitUpdateDexPC(dex_pc);
Logan Chienc3f7d962011-12-27 18:13:18 +08003536 irb_.CreateCall(irb_.GetRuntime(ThrowDivZeroException));
3537 EmitBranchExceptionLandingPad(dex_pc);
3538
3539 irb_.SetInsertPoint(block_continue);
3540}
3541
3542
Logan Chien61bb6142012-02-03 15:34:53 +08003543void MethodCompiler::EmitGuard_NullPointerException(uint32_t dex_pc,
3544 llvm::Value* object) {
3545 llvm::Value* equal_null = irb_.CreateICmpEQ(object, irb_.getJNull());
3546
3547 llvm::BasicBlock* block_exception =
3548 CreateBasicBlockWithDexPC(dex_pc, "nullp");
3549
3550 llvm::BasicBlock* block_continue =
3551 CreateBasicBlockWithDexPC(dex_pc, "cont");
3552
3553 irb_.CreateCondBr(equal_null, block_exception, block_continue);
3554
3555 irb_.SetInsertPoint(block_exception);
TDYa127c8dc1012012-04-19 07:03:33 -07003556 EmitUpdateDexPC(dex_pc);
TDYa1273f9137d2012-04-08 15:59:19 -07003557 irb_.CreateCall(irb_.GetRuntime(ThrowNullPointerException), irb_.getInt32(dex_pc));
Logan Chien61bb6142012-02-03 15:34:53 +08003558 EmitBranchExceptionLandingPad(dex_pc);
3559
3560 irb_.SetInsertPoint(block_continue);
3561}
3562
3563
Logan Chienbb4d12a2012-02-17 14:10:01 +08003564llvm::Value* MethodCompiler::EmitLoadDexCacheAddr(MemberOffset offset) {
3565 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
3566
3567 llvm::Value* dex_cache_offset_value =
3568 irb_.getPtrEquivInt(offset.Int32Value());
3569
3570 llvm::Value* dex_cache_field_addr =
3571 irb_.CreatePtrDisp(method_object_addr, dex_cache_offset_value,
3572 irb_.getJObjectTy()->getPointerTo());
3573
3574 return irb_.CreateLoad(dex_cache_field_addr);
3575}
3576
3577
Logan Chienbb4d12a2012-02-17 14:10:01 +08003578llvm::Value* MethodCompiler::
3579EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx) {
3580 llvm::Value* static_storage_dex_cache_addr =
3581 EmitLoadDexCacheAddr(Method::DexCacheInitializedStaticStorageOffset());
3582
3583 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
3584
3585 return EmitArrayGEP(static_storage_dex_cache_addr, type_idx_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08003586 irb_.getJObjectTy(), kObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003587}
3588
3589
3590llvm::Value* MethodCompiler::
3591EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx) {
3592 llvm::Value* resolved_type_dex_cache_addr =
3593 EmitLoadDexCacheAddr(Method::DexCacheResolvedTypesOffset());
3594
3595 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
3596
3597 return EmitArrayGEP(resolved_type_dex_cache_addr, type_idx_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08003598 irb_.getJObjectTy(), kObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003599}
3600
3601
3602llvm::Value* MethodCompiler::
Logan Chien61c65dc2012-02-29 03:22:30 +08003603EmitLoadDexCacheResolvedMethodFieldAddr(uint32_t method_idx) {
3604 llvm::Value* resolved_method_dex_cache_addr =
3605 EmitLoadDexCacheAddr(Method::DexCacheResolvedMethodsOffset());
3606
3607 llvm::Value* method_idx_value = irb_.getPtrEquivInt(method_idx);
3608
3609 return EmitArrayGEP(resolved_method_dex_cache_addr, method_idx_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08003610 irb_.getJObjectTy(), kObject);
Logan Chien61c65dc2012-02-29 03:22:30 +08003611}
3612
3613
3614llvm::Value* MethodCompiler::
Logan Chienbb4d12a2012-02-17 14:10:01 +08003615EmitLoadDexCacheStringFieldAddr(uint32_t string_idx) {
3616 llvm::Value* string_dex_cache_addr =
3617 EmitLoadDexCacheAddr(Method::DexCacheStringsOffset());
3618
3619 llvm::Value* string_idx_value = irb_.getPtrEquivInt(string_idx);
3620
3621 return EmitArrayGEP(string_dex_cache_addr, string_idx_value,
Ian Rogers04ec04e2012-02-28 16:15:33 -08003622 irb_.getJObjectTy(), kObject);
Logan Chienbb4d12a2012-02-17 14:10:01 +08003623}
3624
3625
Logan Chien83426162011-12-09 09:29:50 +08003626CompiledMethod *MethodCompiler::Compile() {
Logan Chien0b827102011-12-20 19:46:14 +08003627 // Code generation
3628 CreateFunction();
3629
3630 EmitPrologue();
3631 EmitInstructions();
Logan Chienc670a8d2011-12-20 21:25:56 +08003632 EmitPrologueLastBranch();
Logan Chien0b827102011-12-20 19:46:14 +08003633
Logan Chiend6c239a2011-12-23 15:11:45 +08003634 // Verify the generated bitcode
TDYa127853cd092012-04-21 22:15:31 -07003635 VERIFY_LLVM_FUNCTION(*func_);
Logan Chiend6c239a2011-12-23 15:11:45 +08003636
Logan Chien8b977d32012-02-21 19:14:55 +08003637 // Add the memory usage approximation of the compilation unit
3638 cunit_->AddMemUsageApproximation(code_item_->insns_size_in_code_units_ * 900);
3639 // NOTE: From statistic, the bitcode size is 4.5 times bigger than the
3640 // Dex file. Besides, we have to convert the code unit into bytes.
3641 // Thus, we got our magic number 9.
3642
Logan Chien110bcba2012-04-16 19:11:28 +08003643 CompiledMethod* compiled_method =
3644 new CompiledMethod(cunit_->GetInstructionSet(),
3645 cunit_->GetElfIndex(),
3646 elf_func_idx_);
3647
3648 cunit_->RegisterCompiledMethod(func_, compiled_method);
3649
3650 return compiled_method;
Logan Chien0b827102011-12-20 19:46:14 +08003651}
3652
3653
3654llvm::Value* MethodCompiler::EmitLoadMethodObjectAddr() {
3655 return func_->arg_begin();
Shih-wei Liaod1fec812012-02-13 09:51:10 -08003656}
Logan Chien83426162011-12-09 09:29:50 +08003657
3658
Logan Chien5bcc04e2012-01-30 14:15:12 +08003659void MethodCompiler::EmitBranchExceptionLandingPad(uint32_t dex_pc) {
3660 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
3661 irb_.CreateBr(lpad);
3662 } else {
3663 irb_.CreateBr(GetUnwindBasicBlock());
3664 }
3665}
3666
3667
3668void MethodCompiler::EmitGuard_ExceptionLandingPad(uint32_t dex_pc) {
3669 llvm::Value* exception_pending =
3670 irb_.CreateCall(irb_.GetRuntime(IsExceptionPending));
3671
3672 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
3673
3674 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
3675 irb_.CreateCondBr(exception_pending, lpad, block_cont);
3676 } else {
3677 irb_.CreateCondBr(exception_pending, GetUnwindBasicBlock(), block_cont);
3678 }
3679
3680 irb_.SetInsertPoint(block_cont);
3681}
3682
3683
Logan Chien924072f2012-01-30 15:07:24 +08003684void MethodCompiler::EmitGuard_GarbageCollectionSuspend(uint32_t dex_pc) {
3685 llvm::Value* runtime_func = irb_.GetRuntime(TestSuspend);
TDYa127853cd092012-04-21 22:15:31 -07003686
3687 llvm::Value* thread_object_addr = irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
3688
TDYa127c8dc1012012-04-19 07:03:33 -07003689 EmitUpdateDexPC(dex_pc);
TDYa127853cd092012-04-21 22:15:31 -07003690
3691 irb_.CreateCall(runtime_func, thread_object_addr);
Logan Chien924072f2012-01-30 15:07:24 +08003692
3693 EmitGuard_ExceptionLandingPad(dex_pc);
3694}
3695
3696
Logan Chiend6c239a2011-12-23 15:11:45 +08003697llvm::BasicBlock* MethodCompiler::
3698CreateBasicBlockWithDexPC(uint32_t dex_pc, char const* postfix) {
3699 std::string name;
3700
3701 if (postfix) {
3702 StringAppendF(&name, "B%u.%s", dex_pc, postfix);
3703 } else {
3704 StringAppendF(&name, "B%u", dex_pc);
3705 }
3706
3707 return llvm::BasicBlock::Create(*context_, name, func_);
3708}
3709
3710
3711llvm::BasicBlock* MethodCompiler::GetBasicBlock(uint32_t dex_pc) {
3712 DCHECK(dex_pc < code_item_->insns_size_in_code_units_);
3713
3714 llvm::BasicBlock* basic_block = basic_blocks_[dex_pc];
3715
3716 if (!basic_block) {
3717 basic_block = CreateBasicBlockWithDexPC(dex_pc);
3718 basic_blocks_[dex_pc] = basic_block;
3719 }
3720
3721 return basic_block;
3722}
3723
3724
3725llvm::BasicBlock*
3726MethodCompiler::GetNextBasicBlock(uint32_t dex_pc) {
3727 Instruction const* insn = Instruction::At(code_item_->insns_ + dex_pc);
3728 return GetBasicBlock(dex_pc + insn->SizeInCodeUnits());
3729}
3730
3731
Logan Chien5bcc04e2012-01-30 14:15:12 +08003732int32_t MethodCompiler::GetTryItemOffset(uint32_t dex_pc) {
3733 // TODO: Since we are emitting the dex instructions in ascending order
3734 // w.r.t. address, we can cache the lastest try item offset so that we
3735 // don't have to do binary search for every query.
3736
3737 int32_t min = 0;
3738 int32_t max = code_item_->tries_size_ - 1;
3739
3740 while (min <= max) {
3741 int32_t mid = min + (max - min) / 2;
3742
3743 DexFile::TryItem const* ti = DexFile::GetTryItems(*code_item_, mid);
3744 uint32_t start = ti->start_addr_;
3745 uint32_t end = start + ti->insn_count_;
3746
3747 if (dex_pc < start) {
3748 max = mid - 1;
3749 } else if (dex_pc >= end) {
3750 min = mid + 1;
3751 } else {
3752 return mid; // found
3753 }
3754 }
3755
3756 return -1; // not found
3757}
3758
3759
3760llvm::BasicBlock* MethodCompiler::GetLandingPadBasicBlock(uint32_t dex_pc) {
3761 // Find the try item for this address in this method
3762 int32_t ti_offset = GetTryItemOffset(dex_pc);
3763
3764 if (ti_offset == -1) {
3765 return NULL; // No landing pad is available for this address.
3766 }
3767
3768 // Check for the existing landing pad basic block
3769 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
3770 llvm::BasicBlock* block_lpad = basic_block_landing_pads_[ti_offset];
3771
3772 if (block_lpad) {
3773 // We have generated landing pad for this try item already. Return the
3774 // same basic block.
3775 return block_lpad;
3776 }
3777
3778 // Get try item from code item
3779 DexFile::TryItem const* ti = DexFile::GetTryItems(*code_item_, ti_offset);
3780
3781 // Create landing pad basic block
3782 block_lpad = llvm::BasicBlock::Create(*context_,
3783 StringPrintf("lpad%d", ti_offset),
3784 func_);
3785
3786 // Change IRBuilder insert point
3787 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3788 irb_.SetInsertPoint(block_lpad);
3789
3790 // Find catch block with matching type
3791 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
3792
Logan Chien736df022012-04-27 16:25:57 +08003793 llvm::Value* ti_offset_value = irb_.getInt32(ti_offset);
Logan Chien5bcc04e2012-01-30 14:15:12 +08003794
3795 llvm::Value* catch_handler_index_value =
3796 irb_.CreateCall2(irb_.GetRuntime(FindCatchBlock),
Logan Chien736df022012-04-27 16:25:57 +08003797 method_object_addr, ti_offset_value);
Logan Chien5bcc04e2012-01-30 14:15:12 +08003798
3799 // Switch instruction (Go to unwind basic block by default)
3800 llvm::SwitchInst* sw =
3801 irb_.CreateSwitch(catch_handler_index_value, GetUnwindBasicBlock());
3802
3803 // Cases with matched catch block
3804 CatchHandlerIterator iter(*code_item_, ti->start_addr_);
3805
3806 for (uint32_t c = 0; iter.HasNext(); iter.Next(), ++c) {
3807 sw->addCase(irb_.getInt32(c), GetBasicBlock(iter.GetHandlerAddress()));
3808 }
3809
3810 // Restore the orignal insert point for IRBuilder
3811 irb_.restoreIP(irb_ip_original);
3812
3813 // Cache this landing pad
3814 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
3815 basic_block_landing_pads_[ti_offset] = block_lpad;
3816
3817 return block_lpad;
3818}
3819
3820
3821llvm::BasicBlock* MethodCompiler::GetUnwindBasicBlock() {
3822 // Check the existing unwinding baisc block block
3823 if (basic_block_unwind_ != NULL) {
3824 return basic_block_unwind_;
3825 }
3826
3827 // Create new basic block for unwinding
3828 basic_block_unwind_ =
3829 llvm::BasicBlock::Create(*context_, "exception_unwind", func_);
3830
3831 // Change IRBuilder insert point
3832 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3833 irb_.SetInsertPoint(basic_block_unwind_);
3834
Logan Chien8dfcbea2012-02-17 18:50:32 +08003835 // Pop the shadow frame
3836 EmitPopShadowFrame();
3837
Logan Chien5bcc04e2012-01-30 14:15:12 +08003838 // Emit the code to return default value (zero) for the given return type.
Logan Chiendd361c92012-04-10 23:40:37 +08003839 char ret_shorty = oat_compilation_unit_->GetShorty()[0];
Logan Chien5bcc04e2012-01-30 14:15:12 +08003840 if (ret_shorty == 'V') {
3841 irb_.CreateRetVoid();
3842 } else {
3843 irb_.CreateRet(irb_.getJZero(ret_shorty));
3844 }
3845
3846 // Restore the orignal insert point for IRBuilder
3847 irb_.restoreIP(irb_ip_original);
3848
3849 return basic_block_unwind_;
3850}
3851
3852
Logan Chienc670a8d2011-12-20 21:25:56 +08003853llvm::Value* MethodCompiler::AllocDalvikLocalVarReg(RegCategory cat,
3854 uint32_t reg_idx) {
3855
3856 // Save current IR builder insert point
3857 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3858
3859 // Alloca
3860 llvm::Value* reg_addr = NULL;
3861
3862 switch (cat) {
3863 case kRegCat1nr:
3864 irb_.SetInsertPoint(basic_block_reg_alloca_);
3865 reg_addr = irb_.CreateAlloca(irb_.getJIntTy(), 0,
3866 StringPrintf("r%u", reg_idx));
Logan Chienc670a8d2011-12-20 21:25:56 +08003867 break;
3868
3869 case kRegCat2:
3870 irb_.SetInsertPoint(basic_block_reg_alloca_);
3871 reg_addr = irb_.CreateAlloca(irb_.getJLongTy(), 0,
3872 StringPrintf("w%u", reg_idx));
Logan Chienc670a8d2011-12-20 21:25:56 +08003873 break;
3874
3875 case kRegObject:
Logan Chien8dfcbea2012-02-17 18:50:32 +08003876 {
3877 irb_.SetInsertPoint(basic_block_shadow_frame_alloca_);
Logan Chienc670a8d2011-12-20 21:25:56 +08003878
Logan Chien8dfcbea2012-02-17 18:50:32 +08003879 llvm::Value* gep_index[] = {
3880 irb_.getInt32(0), // No pointer displacement
3881 irb_.getInt32(1), // SIRT
3882 irb_.getInt32(reg_idx) // Pointer field
3883 };
3884
3885 reg_addr = irb_.CreateGEP(shadow_frame_, gep_index,
3886 StringPrintf("p%u", reg_idx));
Logan Chien8dfcbea2012-02-17 18:50:32 +08003887 }
Logan Chienc670a8d2011-12-20 21:25:56 +08003888 break;
3889
3890 default:
3891 LOG(FATAL) << "Unknown register category for allocation: " << cat;
3892 }
3893
3894 // Restore IRBuilder insert point
3895 irb_.restoreIP(irb_ip_original);
3896
3897 DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL));
3898 return reg_addr;
3899}
3900
3901
3902llvm::Value* MethodCompiler::AllocDalvikRetValReg(RegCategory cat) {
3903 // Save current IR builder insert point
3904 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
3905
3906 // Alloca
3907 llvm::Value* reg_addr = NULL;
3908
3909 switch (cat) {
3910 case kRegCat1nr:
3911 irb_.SetInsertPoint(basic_block_reg_alloca_);
3912 reg_addr = irb_.CreateAlloca(irb_.getJIntTy(), 0, "r_res");
3913 break;
3914
3915 case kRegCat2:
3916 irb_.SetInsertPoint(basic_block_reg_alloca_);
3917 reg_addr = irb_.CreateAlloca(irb_.getJLongTy(), 0, "w_res");
3918 break;
3919
3920 case kRegObject:
3921 irb_.SetInsertPoint(basic_block_reg_alloca_);
3922 reg_addr = irb_.CreateAlloca(irb_.getJObjectTy(), 0, "p_res");
3923 break;
3924
3925 default:
3926 LOG(FATAL) << "Unknown register category for allocation: " << cat;
3927 }
3928
3929 // Restore IRBuilder insert point
3930 irb_.restoreIP(irb_ip_original);
3931
3932 DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL));
3933 return reg_addr;
3934}
3935
3936
Logan Chien8dfcbea2012-02-17 18:50:32 +08003937void MethodCompiler::EmitPopShadowFrame() {
3938 irb_.CreateCall(irb_.GetRuntime(PopShadowFrame));
3939}
3940
3941
TDYa127c8dc1012012-04-19 07:03:33 -07003942void MethodCompiler::EmitUpdateDexPC(uint32_t dex_pc) {
3943 irb_.StoreToObjectOffset(shadow_frame_,
3944 ShadowFrame::DexPCOffset(),
3945 irb_.getInt32(dex_pc));
Logan Chien8dfcbea2012-02-17 18:50:32 +08003946}
3947
3948
Logan Chien83426162011-12-09 09:29:50 +08003949} // namespace compiler_llvm
3950} // namespace art