blob: cdd237820e43905c9f84ae46efac399e810a366f [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"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080020#include "compiler.h"
Logan Chiena78e3c82011-12-27 17:59:35 +080021#include "inferred_reg_category_map.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080022#include "ir_builder.h"
23#include "logging.h"
24#include "object.h"
25#include "object_utils.h"
Logan Chien42e0e152012-01-13 15:42:36 +080026#include "runtime_support_func.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080027#include "stl_util.h"
Logan Chien0b827102011-12-20 19:46:14 +080028#include "stringprintf.h"
29#include "utils_llvm.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080030
31#include <iomanip>
32
33#include <llvm/Analysis/Verifier.h>
Logan Chienc670a8d2011-12-20 21:25:56 +080034#include <llvm/BasicBlock.h>
Shih-wei Liaod1fec812012-02-13 09:51:10 -080035#include <llvm/Function.h>
36
Logan Chien83426162011-12-09 09:29:50 +080037namespace art {
38namespace compiler_llvm {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080039
Logan Chien42e0e152012-01-13 15:42:36 +080040using namespace runtime_support;
41
Shih-wei Liaod1fec812012-02-13 09:51:10 -080042
Logan Chien83426162011-12-09 09:29:50 +080043MethodCompiler::MethodCompiler(InstructionSet insn_set,
44 Compiler* compiler,
45 ClassLinker* class_linker,
46 ClassLoader const* class_loader,
47 DexFile const* dex_file,
48 DexCache* dex_cache,
49 DexFile::CodeItem const* code_item,
Shih-wei Liaod1fec812012-02-13 09:51:10 -080050 uint32_t method_idx,
51 uint32_t access_flags)
52: insn_set_(insn_set),
53 compiler_(compiler), compiler_llvm_(compiler->GetCompilerLLVM()),
54 class_linker_(class_linker), class_loader_(class_loader),
55 dex_file_(dex_file), dex_cache_(dex_cache), code_item_(code_item),
56 method_(dex_cache->GetResolvedMethod(method_idx)),
57 method_helper_(method_), method_idx_(method_idx),
58 access_flags_(access_flags), module_(compiler_llvm_->GetModule()),
59 context_(compiler_llvm_->GetLLVMContext()),
Logan Chienc670a8d2011-12-20 21:25:56 +080060 irb_(*compiler_llvm_->GetIRBuilder()), func_(NULL), retval_reg_(NULL),
Logan Chiend6ececa2011-12-27 16:20:15 +080061 basic_block_reg_alloca_(NULL),
62 basic_block_reg_zero_init_(NULL), basic_block_reg_arg_init_(NULL),
Logan Chien5bcc04e2012-01-30 14:15:12 +080063 basic_blocks_(code_item->insns_size_in_code_units_),
64 basic_block_landing_pads_(code_item->tries_size_, NULL),
65 basic_block_unwind_(NULL), basic_block_unreachable_(NULL) {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080066}
67
68
69MethodCompiler::~MethodCompiler() {
Logan Chienc670a8d2011-12-20 21:25:56 +080070 STLDeleteElements(&regs_);
Shih-wei Liaod1fec812012-02-13 09:51:10 -080071}
72
73
Logan Chien0b827102011-12-20 19:46:14 +080074void MethodCompiler::CreateFunction() {
75 // LLVM function name
76 std::string func_name(LLVMLongName(method_));
77
78 // Get function type
79 llvm::FunctionType* func_type =
80 GetFunctionType(method_idx_, method_->IsStatic());
81
82 // Create function
83 func_ = llvm::Function::Create(func_type, llvm::Function::ExternalLinkage,
84 func_name, module_);
85
86 // Set argument name
87 llvm::Function::arg_iterator arg_iter(func_->arg_begin());
88 llvm::Function::arg_iterator arg_end(func_->arg_end());
89
90 DCHECK_NE(arg_iter, arg_end);
91 arg_iter->setName("method");
92 ++arg_iter;
93
94 if (!method_->IsStatic()) {
95 DCHECK_NE(arg_iter, arg_end);
96 arg_iter->setName("this");
97 ++arg_iter;
98 }
99
100 for (unsigned i = 0; arg_iter != arg_end; ++i, ++arg_iter) {
101 arg_iter->setName(StringPrintf("a%u", i));
102 }
103}
104
105
106llvm::FunctionType* MethodCompiler::GetFunctionType(uint32_t method_idx,
107 bool is_static) {
108 // Get method signature
109 DexFile::MethodId const& method_id = dex_file_->GetMethodId(method_idx);
110
111 int32_t shorty_size;
112 char const* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size);
113 CHECK_GE(shorty_size, 1);
114
115 // Get return type
116 llvm::Type* ret_type = irb_.getJType(shorty[0], kAccurate);
117
118 // Get argument type
119 std::vector<llvm::Type*> args_type;
120
121 args_type.push_back(irb_.getJObjectTy()); // method object pointer
122
123 if (!is_static) {
124 args_type.push_back(irb_.getJType('L', kAccurate)); // "this" object pointer
125 }
126
127 for (int32_t i = 1; i < shorty_size; ++i) {
128 args_type.push_back(irb_.getJType(shorty[i], kAccurate));
129 }
130
131 return llvm::FunctionType::get(ret_type, args_type, false);
132}
133
134
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800135void MethodCompiler::EmitPrologue() {
Logan Chienc670a8d2011-12-20 21:25:56 +0800136 // Create basic blocks for prologue
137 basic_block_reg_alloca_ =
138 llvm::BasicBlock::Create(*context_, "prologue.alloca", func_);
139
140 basic_block_reg_zero_init_ =
141 llvm::BasicBlock::Create(*context_, "prologue.zeroinit", func_);
142
Logan Chiend6ececa2011-12-27 16:20:15 +0800143 basic_block_reg_arg_init_ =
144 llvm::BasicBlock::Create(*context_, "prologue.arginit", func_);
145
Logan Chienc670a8d2011-12-20 21:25:56 +0800146 // Create register array
147 for (uint16_t r = 0; r < code_item_->registers_size_; ++r) {
148 regs_.push_back(DalvikReg::CreateLocalVarReg(*this, r));
149 }
150
151 retval_reg_.reset(DalvikReg::CreateRetValReg(*this));
Logan Chiend6ececa2011-12-27 16:20:15 +0800152
153 // Store argument to dalvik register
154 irb_.SetInsertPoint(basic_block_reg_arg_init_);
155 EmitPrologueAssignArgRegister();
156
157 // Branch to start address
158 irb_.CreateBr(GetBasicBlock(0));
Logan Chienc670a8d2011-12-20 21:25:56 +0800159}
160
161
162void MethodCompiler::EmitPrologueLastBranch() {
163 irb_.SetInsertPoint(basic_block_reg_alloca_);
164 irb_.CreateBr(basic_block_reg_zero_init_);
165
166 irb_.SetInsertPoint(basic_block_reg_zero_init_);
Logan Chiend6ececa2011-12-27 16:20:15 +0800167 irb_.CreateBr(basic_block_reg_arg_init_);
168}
169
170
171void MethodCompiler::EmitPrologueAssignArgRegister() {
172 uint16_t arg_reg = code_item_->registers_size_ - code_item_->ins_size_;
173
174 llvm::Function::arg_iterator arg_iter(func_->arg_begin());
175 llvm::Function::arg_iterator arg_end(func_->arg_end());
176
177 char const* shorty = method_helper_.GetShorty();
178 int32_t shorty_size = method_helper_.GetShortyLength();
179 CHECK_LE(1, shorty_size);
180
181 ++arg_iter; // skip method object
182
183 if (!method_->IsStatic()) {
184 EmitStoreDalvikReg(arg_reg, kObject, kAccurate, arg_iter);
185 ++arg_iter;
186 ++arg_reg;
187 }
188
189 for (int32_t i = 1; i < shorty_size; ++i, ++arg_iter) {
190 EmitStoreDalvikReg(arg_reg, shorty[i], kAccurate, arg_iter);
191
192 ++arg_reg;
193 if (shorty[i] == 'J' || shorty[i] == 'D') {
194 // Wide types, such as long and double, are using a pair of registers
195 // to store the value, so we have to increase arg_reg again.
196 ++arg_reg;
197 }
198 }
199
200 DCHECK_EQ(arg_end, arg_iter);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800201}
202
203
Logan Chien83426162011-12-09 09:29:50 +0800204void MethodCompiler::EmitInstructions() {
Logan Chiend6c239a2011-12-23 15:11:45 +0800205 uint32_t dex_pc = 0;
206 while (dex_pc < code_item_->insns_size_in_code_units_) {
207 Instruction const* insn = Instruction::At(code_item_->insns_ + dex_pc);
208 EmitInstruction(dex_pc, insn);
209 dex_pc += insn->SizeInCodeUnits();
210 }
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800211}
212
213
Logan Chien83426162011-12-09 09:29:50 +0800214void MethodCompiler::EmitInstruction(uint32_t dex_pc,
215 Instruction const* insn) {
Logan Chiend6c239a2011-12-23 15:11:45 +0800216
217 // Set the IRBuilder insertion point
218 irb_.SetInsertPoint(GetBasicBlock(dex_pc));
219
Logan Chien70f94b42011-12-27 17:49:11 +0800220#define ARGS dex_pc, insn
221
222 // Dispatch the instruction
223 switch (insn->Opcode()) {
224 case Instruction::NOP:
225 EmitInsn_Nop(ARGS);
226 break;
227
228 case Instruction::MOVE:
229 case Instruction::MOVE_FROM16:
230 case Instruction::MOVE_16:
231 EmitInsn_Move(ARGS, kInt);
232 break;
233
234 case Instruction::MOVE_WIDE:
235 case Instruction::MOVE_WIDE_FROM16:
236 case Instruction::MOVE_WIDE_16:
237 EmitInsn_Move(ARGS, kLong);
238 break;
239
240 case Instruction::MOVE_OBJECT:
241 case Instruction::MOVE_OBJECT_FROM16:
242 case Instruction::MOVE_OBJECT_16:
243 EmitInsn_Move(ARGS, kObject);
244 break;
245
246 case Instruction::MOVE_RESULT:
247 EmitInsn_MoveResult(ARGS, kInt);
248 break;
249
250 case Instruction::MOVE_RESULT_WIDE:
251 EmitInsn_MoveResult(ARGS, kLong);
252 break;
253
254 case Instruction::MOVE_RESULT_OBJECT:
255 EmitInsn_MoveResult(ARGS, kObject);
256 break;
257
258 case Instruction::MOVE_EXCEPTION:
259 EmitInsn_MoveException(ARGS);
260 break;
261
262 case Instruction::RETURN_VOID:
263 EmitInsn_ReturnVoid(ARGS);
264 break;
265
266 case Instruction::RETURN:
267 case Instruction::RETURN_WIDE:
268 case Instruction::RETURN_OBJECT:
269 EmitInsn_Return(ARGS);
270 break;
271
272 case Instruction::CONST_4:
273 case Instruction::CONST_16:
274 case Instruction::CONST:
275 case Instruction::CONST_HIGH16:
276 EmitInsn_LoadConstant(ARGS, kInt);
277 break;
278
279 case Instruction::CONST_WIDE_16:
280 case Instruction::CONST_WIDE_32:
281 case Instruction::CONST_WIDE:
282 case Instruction::CONST_WIDE_HIGH16:
283 EmitInsn_LoadConstant(ARGS, kLong);
284 break;
285
286 case Instruction::CONST_STRING:
287 case Instruction::CONST_STRING_JUMBO:
288 EmitInsn_LoadConstantString(ARGS);
289 break;
290
291 case Instruction::CONST_CLASS:
292 EmitInsn_LoadConstantClass(ARGS);
293 break;
294
295 case Instruction::MONITOR_ENTER:
296 EmitInsn_MonitorEnter(ARGS);
297 break;
298
299 case Instruction::MONITOR_EXIT:
300 EmitInsn_MonitorExit(ARGS);
301 break;
302
303 case Instruction::CHECK_CAST:
304 EmitInsn_CheckCast(ARGS);
305 break;
306
307 case Instruction::INSTANCE_OF:
308 EmitInsn_InstanceOf(ARGS);
309 break;
310
311 case Instruction::ARRAY_LENGTH:
312 EmitInsn_ArrayLength(ARGS);
313 break;
314
315 case Instruction::NEW_INSTANCE:
316 EmitInsn_NewInstance(ARGS);
317 break;
318
319 case Instruction::NEW_ARRAY:
320 EmitInsn_NewArray(ARGS);
321 break;
322
323 case Instruction::FILLED_NEW_ARRAY:
324 EmitInsn_FilledNewArray(ARGS, false);
325 break;
326
327 case Instruction::FILLED_NEW_ARRAY_RANGE:
328 EmitInsn_FilledNewArray(ARGS, true);
329 break;
330
331 case Instruction::FILL_ARRAY_DATA:
332 EmitInsn_FillArrayData(ARGS);
333 break;
334
335 case Instruction::THROW:
336 EmitInsn_ThrowException(ARGS);
337 break;
338
339 case Instruction::GOTO:
340 case Instruction::GOTO_16:
341 case Instruction::GOTO_32:
342 EmitInsn_UnconditionalBranch(ARGS);
343 break;
344
345 case Instruction::PACKED_SWITCH:
346 EmitInsn_PackedSwitch(ARGS);
347 break;
348
349 case Instruction::SPARSE_SWITCH:
350 EmitInsn_SparseSwitch(ARGS);
351 break;
352
353 case Instruction::CMPL_FLOAT:
354 EmitInsn_FPCompare(ARGS, kFloat, false);
355 break;
356
357 case Instruction::CMPG_FLOAT:
358 EmitInsn_FPCompare(ARGS, kFloat, true);
359 break;
360
361 case Instruction::CMPL_DOUBLE:
362 EmitInsn_FPCompare(ARGS, kDouble, false);
363 break;
364
365 case Instruction::CMPG_DOUBLE:
366 EmitInsn_FPCompare(ARGS, kDouble, true);
367 break;
368
369 case Instruction::CMP_LONG:
370 EmitInsn_LongCompare(ARGS);
371 break;
372
373 case Instruction::IF_EQ:
374 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_EQ);
375 break;
376
377 case Instruction::IF_NE:
378 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_NE);
379 break;
380
381 case Instruction::IF_LT:
382 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_LT);
383 break;
384
385 case Instruction::IF_GE:
386 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_GE);
387 break;
388
389 case Instruction::IF_GT:
390 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_GT);
391 break;
392
393 case Instruction::IF_LE:
394 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_LE);
395 break;
396
397 case Instruction::IF_EQZ:
398 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_EQ);
399 break;
400
401 case Instruction::IF_NEZ:
402 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_NE);
403 break;
404
405 case Instruction::IF_LTZ:
406 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_LT);
407 break;
408
409 case Instruction::IF_GEZ:
410 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_GE);
411 break;
412
413 case Instruction::IF_GTZ:
414 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_GT);
415 break;
416
417 case Instruction::IF_LEZ:
418 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_LE);
419 break;
420
421 case Instruction::AGET:
422 EmitInsn_AGet(ARGS, kInt);
423 break;
424
425 case Instruction::AGET_WIDE:
426 EmitInsn_AGet(ARGS, kLong);
427 break;
428
429 case Instruction::AGET_OBJECT:
430 EmitInsn_AGet(ARGS, kObject);
431 break;
432
433 case Instruction::AGET_BOOLEAN:
434 EmitInsn_AGet(ARGS, kBoolean);
435 break;
436
437 case Instruction::AGET_BYTE:
438 EmitInsn_AGet(ARGS, kByte);
439 break;
440
441 case Instruction::AGET_CHAR:
442 EmitInsn_AGet(ARGS, kChar);
443 break;
444
445 case Instruction::AGET_SHORT:
446 EmitInsn_AGet(ARGS, kShort);
447 break;
448
449 case Instruction::APUT:
450 EmitInsn_APut(ARGS, kInt);
451 break;
452
453 case Instruction::APUT_WIDE:
454 EmitInsn_APut(ARGS, kLong);
455 break;
456
457 case Instruction::APUT_OBJECT:
458 EmitInsn_APut(ARGS, kObject);
459 break;
460
461 case Instruction::APUT_BOOLEAN:
462 EmitInsn_APut(ARGS, kBoolean);
463 break;
464
465 case Instruction::APUT_BYTE:
466 EmitInsn_APut(ARGS, kByte);
467 break;
468
469 case Instruction::APUT_CHAR:
470 EmitInsn_APut(ARGS, kChar);
471 break;
472
473 case Instruction::APUT_SHORT:
474 EmitInsn_APut(ARGS, kShort);
475 break;
476
477 case Instruction::IGET:
478 EmitInsn_IGet(ARGS, kInt);
479 break;
480
481 case Instruction::IGET_WIDE:
482 EmitInsn_IGet(ARGS, kLong);
483 break;
484
485 case Instruction::IGET_OBJECT:
486 EmitInsn_IGet(ARGS, kObject);
487 break;
488
489 case Instruction::IGET_BOOLEAN:
490 EmitInsn_IGet(ARGS, kBoolean);
491 break;
492
493 case Instruction::IGET_BYTE:
494 EmitInsn_IGet(ARGS, kByte);
495 break;
496
497 case Instruction::IGET_CHAR:
498 EmitInsn_IGet(ARGS, kChar);
499 break;
500
501 case Instruction::IGET_SHORT:
502 EmitInsn_IGet(ARGS, kShort);
503 break;
504
505 case Instruction::IPUT:
506 EmitInsn_IPut(ARGS, kInt);
507 break;
508
509 case Instruction::IPUT_WIDE:
510 EmitInsn_IPut(ARGS, kLong);
511 break;
512
513 case Instruction::IPUT_OBJECT:
514 EmitInsn_IPut(ARGS, kObject);
515 break;
516
517 case Instruction::IPUT_BOOLEAN:
518 EmitInsn_IPut(ARGS, kBoolean);
519 break;
520
521 case Instruction::IPUT_BYTE:
522 EmitInsn_IPut(ARGS, kByte);
523 break;
524
525 case Instruction::IPUT_CHAR:
526 EmitInsn_IPut(ARGS, kChar);
527 break;
528
529 case Instruction::IPUT_SHORT:
530 EmitInsn_IPut(ARGS, kShort);
531 break;
532
533 case Instruction::SGET:
534 EmitInsn_SGet(ARGS, kInt);
535 break;
536
537 case Instruction::SGET_WIDE:
538 EmitInsn_SGet(ARGS, kLong);
539 break;
540
541 case Instruction::SGET_OBJECT:
542 EmitInsn_SGet(ARGS, kObject);
543 break;
544
545 case Instruction::SGET_BOOLEAN:
546 EmitInsn_SGet(ARGS, kBoolean);
547 break;
548
549 case Instruction::SGET_BYTE:
550 EmitInsn_SGet(ARGS, kByte);
551 break;
552
553 case Instruction::SGET_CHAR:
554 EmitInsn_SGet(ARGS, kChar);
555 break;
556
557 case Instruction::SGET_SHORT:
558 EmitInsn_SGet(ARGS, kShort);
559 break;
560
561 case Instruction::SPUT:
562 EmitInsn_SPut(ARGS, kInt);
563 break;
564
565 case Instruction::SPUT_WIDE:
566 EmitInsn_SPut(ARGS, kLong);
567 break;
568
569 case Instruction::SPUT_OBJECT:
570 EmitInsn_SPut(ARGS, kObject);
571 break;
572
573 case Instruction::SPUT_BOOLEAN:
574 EmitInsn_SPut(ARGS, kBoolean);
575 break;
576
577 case Instruction::SPUT_BYTE:
578 EmitInsn_SPut(ARGS, kByte);
579 break;
580
581 case Instruction::SPUT_CHAR:
582 EmitInsn_SPut(ARGS, kChar);
583 break;
584
585 case Instruction::SPUT_SHORT:
586 EmitInsn_SPut(ARGS, kShort);
587 break;
588
589
590 case Instruction::INVOKE_VIRTUAL:
591 EmitInsn_InvokeVirtual(ARGS, false);
592 break;
593
594 case Instruction::INVOKE_SUPER:
595 EmitInsn_InvokeSuper(ARGS, false);
596 break;
597
598 case Instruction::INVOKE_DIRECT:
599 EmitInsn_InvokeDirect(ARGS, false);
600 break;
601
602 case Instruction::INVOKE_STATIC:
603 EmitInsn_InvokeStatic(ARGS, false);
604 break;
605
606 case Instruction::INVOKE_INTERFACE:
607 EmitInsn_InvokeInterface(ARGS, false);
608 break;
609
610 case Instruction::INVOKE_VIRTUAL_RANGE:
611 EmitInsn_InvokeVirtual(ARGS, true);
612 break;
613
614 case Instruction::INVOKE_SUPER_RANGE:
615 EmitInsn_InvokeSuper(ARGS, true);
616 break;
617
618 case Instruction::INVOKE_DIRECT_RANGE:
619 EmitInsn_InvokeDirect(ARGS, true);
620 break;
621
622 case Instruction::INVOKE_STATIC_RANGE:
623 EmitInsn_InvokeStatic(ARGS, true);
624 break;
625
626 case Instruction::INVOKE_INTERFACE_RANGE:
627 EmitInsn_InvokeInterface(ARGS, true);
628 break;
629
630 case Instruction::NEG_INT:
631 EmitInsn_Neg(ARGS, kInt);
632 break;
633
634 case Instruction::NOT_INT:
635 EmitInsn_Not(ARGS, kInt);
636 break;
637
638 case Instruction::NEG_LONG:
639 EmitInsn_Neg(ARGS, kLong);
640 break;
641
642 case Instruction::NOT_LONG:
643 EmitInsn_Not(ARGS, kLong);
644 break;
645
646 case Instruction::NEG_FLOAT:
647 EmitInsn_FNeg(ARGS, kFloat);
648 break;
649
650 case Instruction::NEG_DOUBLE:
651 EmitInsn_FNeg(ARGS, kDouble);
652 break;
653
654 case Instruction::INT_TO_LONG:
655 EmitInsn_SExt(ARGS);
656 break;
657
658 case Instruction::INT_TO_FLOAT:
659 EmitInsn_IntToFP(ARGS, kInt, kFloat);
660 break;
661
662 case Instruction::INT_TO_DOUBLE:
663 EmitInsn_IntToFP(ARGS, kInt, kDouble);
664 break;
665
666 case Instruction::LONG_TO_INT:
667 EmitInsn_Trunc(ARGS);
668 break;
669
670 case Instruction::LONG_TO_FLOAT:
671 EmitInsn_IntToFP(ARGS, kLong, kFloat);
672 break;
673
674 case Instruction::LONG_TO_DOUBLE:
675 EmitInsn_IntToFP(ARGS, kLong, kDouble);
676 break;
677
678 case Instruction::FLOAT_TO_INT:
679 EmitInsn_FPToInt(ARGS, kFloat, kInt);
680 break;
681
682 case Instruction::FLOAT_TO_LONG:
683 EmitInsn_FPToInt(ARGS, kFloat, kLong);
684 break;
685
686 case Instruction::FLOAT_TO_DOUBLE:
687 EmitInsn_FExt(ARGS);
688 break;
689
690 case Instruction::DOUBLE_TO_INT:
691 EmitInsn_FPToInt(ARGS, kDouble, kInt);
692 break;
693
694 case Instruction::DOUBLE_TO_LONG:
695 EmitInsn_FPToInt(ARGS, kDouble, kLong);
696 break;
697
698 case Instruction::DOUBLE_TO_FLOAT:
699 EmitInsn_FTrunc(ARGS);
700 break;
701
702 case Instruction::INT_TO_BYTE:
703 EmitInsn_TruncAndSExt(ARGS, 8);
704 break;
705
706 case Instruction::INT_TO_CHAR:
707 EmitInsn_TruncAndZExt(ARGS, 16);
708 break;
709
710 case Instruction::INT_TO_SHORT:
711 EmitInsn_TruncAndSExt(ARGS, 16);
712 break;
713
714 case Instruction::ADD_INT:
715 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kInt, false);
716 break;
717
718 case Instruction::SUB_INT:
719 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kInt, false);
720 break;
721
722 case Instruction::MUL_INT:
723 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kInt, false);
724 break;
725
726 case Instruction::DIV_INT:
727 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kInt, false);
728 break;
729
730 case Instruction::REM_INT:
731 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kInt, false);
732 break;
733
734 case Instruction::AND_INT:
735 EmitInsn_IntArithm(ARGS, kIntArithm_And, kInt, false);
736 break;
737
738 case Instruction::OR_INT:
739 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kInt, false);
740 break;
741
742 case Instruction::XOR_INT:
743 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kInt, false);
744 break;
745
746 case Instruction::SHL_INT:
747 EmitInsn_IntArithm(ARGS, kIntArithm_Shl, kInt, false);
748 break;
749
750 case Instruction::SHR_INT:
751 EmitInsn_IntArithm(ARGS, kIntArithm_Shr, kInt, false);
752 break;
753
754 case Instruction::USHR_INT:
755 EmitInsn_IntArithm(ARGS, kIntArithm_UShr, kInt, false);
756 break;
757
758 case Instruction::ADD_LONG:
759 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kLong, false);
760 break;
761
762 case Instruction::SUB_LONG:
763 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kLong, false);
764 break;
765
766 case Instruction::MUL_LONG:
767 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kLong, false);
768 break;
769
770 case Instruction::DIV_LONG:
771 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kLong, false);
772 break;
773
774 case Instruction::REM_LONG:
775 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kLong, false);
776 break;
777
778 case Instruction::AND_LONG:
779 EmitInsn_IntArithm(ARGS, kIntArithm_And, kLong, false);
780 break;
781
782 case Instruction::OR_LONG:
783 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kLong, false);
784 break;
785
786 case Instruction::XOR_LONG:
787 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kLong, false);
788 break;
789
790 case Instruction::SHL_LONG:
791 EmitInsn_IntArithm(ARGS, kIntArithm_Shl, kLong, false);
792 break;
793
794 case Instruction::SHR_LONG:
795 EmitInsn_IntArithm(ARGS, kIntArithm_Shr, kLong, false);
796 break;
797
798 case Instruction::USHR_LONG:
799 EmitInsn_IntArithm(ARGS, kIntArithm_UShr, kLong, false);
800 break;
801
802 case Instruction::ADD_FLOAT:
803 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kFloat, false);
804 break;
805
806 case Instruction::SUB_FLOAT:
807 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kFloat, false);
808 break;
809
810 case Instruction::MUL_FLOAT:
811 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kFloat, false);
812 break;
813
814 case Instruction::DIV_FLOAT:
815 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kFloat, false);
816 break;
817
818 case Instruction::REM_FLOAT:
819 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kFloat, false);
820 break;
821
822 case Instruction::ADD_DOUBLE:
823 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kDouble, false);
824 break;
825
826 case Instruction::SUB_DOUBLE:
827 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kDouble, false);
828 break;
829
830 case Instruction::MUL_DOUBLE:
831 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kDouble, false);
832 break;
833
834 case Instruction::DIV_DOUBLE:
835 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kDouble, false);
836 break;
837
838 case Instruction::REM_DOUBLE:
839 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kDouble, false);
840 break;
841
842 case Instruction::ADD_INT_2ADDR:
843 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kInt, true);
844 break;
845
846 case Instruction::SUB_INT_2ADDR:
847 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kInt, true);
848 break;
849
850 case Instruction::MUL_INT_2ADDR:
851 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kInt, true);
852 break;
853
854 case Instruction::DIV_INT_2ADDR:
855 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kInt, true);
856 break;
857
858 case Instruction::REM_INT_2ADDR:
859 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kInt, true);
860 break;
861
862 case Instruction::AND_INT_2ADDR:
863 EmitInsn_IntArithm(ARGS, kIntArithm_And, kInt, true);
864 break;
865
866 case Instruction::OR_INT_2ADDR:
867 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kInt, true);
868 break;
869
870 case Instruction::XOR_INT_2ADDR:
871 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kInt, true);
872 break;
873
874 case Instruction::SHL_INT_2ADDR:
875 EmitInsn_IntArithm(ARGS, kIntArithm_Shl, kInt, true);
876 break;
877
878 case Instruction::SHR_INT_2ADDR:
879 EmitInsn_IntArithm(ARGS, kIntArithm_Shr, kInt, true);
880 break;
881
882 case Instruction::USHR_INT_2ADDR:
883 EmitInsn_IntArithm(ARGS, kIntArithm_UShr, kInt, true);
884 break;
885
886 case Instruction::ADD_LONG_2ADDR:
887 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kLong, true);
888 break;
889
890 case Instruction::SUB_LONG_2ADDR:
891 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kLong, true);
892 break;
893
894 case Instruction::MUL_LONG_2ADDR:
895 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kLong, true);
896 break;
897
898 case Instruction::DIV_LONG_2ADDR:
899 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kLong, true);
900 break;
901
902 case Instruction::REM_LONG_2ADDR:
903 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kLong, true);
904 break;
905
906 case Instruction::AND_LONG_2ADDR:
907 EmitInsn_IntArithm(ARGS, kIntArithm_And, kLong, true);
908 break;
909
910 case Instruction::OR_LONG_2ADDR:
911 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kLong, true);
912 break;
913
914 case Instruction::XOR_LONG_2ADDR:
915 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kLong, true);
916 break;
917
918 case Instruction::SHL_LONG_2ADDR:
919 EmitInsn_IntArithm(ARGS, kIntArithm_Shl, kLong, true);
920 break;
921
922 case Instruction::SHR_LONG_2ADDR:
923 EmitInsn_IntArithm(ARGS, kIntArithm_Shr, kLong, true);
924 break;
925
926 case Instruction::USHR_LONG_2ADDR:
927 EmitInsn_IntArithm(ARGS, kIntArithm_UShr, kLong, true);
928 break;
929
930 case Instruction::ADD_FLOAT_2ADDR:
931 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kFloat, true);
932 break;
933
934 case Instruction::SUB_FLOAT_2ADDR:
935 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kFloat, true);
936 break;
937
938 case Instruction::MUL_FLOAT_2ADDR:
939 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kFloat, true);
940 break;
941
942 case Instruction::DIV_FLOAT_2ADDR:
943 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kFloat, true);
944 break;
945
946 case Instruction::REM_FLOAT_2ADDR:
947 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kFloat, true);
948 break;
949
950 case Instruction::ADD_DOUBLE_2ADDR:
951 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kDouble, true);
952 break;
953
954 case Instruction::SUB_DOUBLE_2ADDR:
955 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kDouble, true);
956 break;
957
958 case Instruction::MUL_DOUBLE_2ADDR:
959 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kDouble, true);
960 break;
961
962 case Instruction::DIV_DOUBLE_2ADDR:
963 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kDouble, true);
964 break;
965
966 case Instruction::REM_DOUBLE_2ADDR:
967 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kDouble, true);
968 break;
969
970 case Instruction::ADD_INT_LIT16:
971 case Instruction::ADD_INT_LIT8:
972 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Add);
973 break;
974
975 case Instruction::RSUB_INT:
976 case Instruction::RSUB_INT_LIT8:
977 EmitInsn_RSubImmediate(ARGS);
978 break;
979
980 case Instruction::MUL_INT_LIT16:
981 case Instruction::MUL_INT_LIT8:
982 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Mul);
983 break;
984
985 case Instruction::DIV_INT_LIT16:
986 case Instruction::DIV_INT_LIT8:
987 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Div);
988 break;
989
990 case Instruction::REM_INT_LIT16:
991 case Instruction::REM_INT_LIT8:
992 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Rem);
993 break;
994
995 case Instruction::AND_INT_LIT16:
996 case Instruction::AND_INT_LIT8:
997 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_And);
998 break;
999
1000 case Instruction::OR_INT_LIT16:
1001 case Instruction::OR_INT_LIT8:
1002 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Or);
1003 break;
1004
1005 case Instruction::XOR_INT_LIT16:
1006 case Instruction::XOR_INT_LIT8:
1007 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Xor);
1008 break;
1009
1010 case Instruction::SHL_INT_LIT8:
1011 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Shl);
1012 break;
1013
1014 case Instruction::SHR_INT_LIT8:
1015 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Shr);
1016 break;
1017
1018 case Instruction::USHR_INT_LIT8:
1019 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_UShr);
1020 break;
1021
1022 case Instruction::UNUSED_3E:
1023 case Instruction::UNUSED_3F:
1024 case Instruction::UNUSED_40:
1025 case Instruction::UNUSED_41:
1026 case Instruction::UNUSED_42:
1027 case Instruction::UNUSED_43:
1028 case Instruction::UNUSED_73:
1029 case Instruction::UNUSED_79:
1030 case Instruction::UNUSED_7A:
1031 case Instruction::UNUSED_E3:
1032 case Instruction::UNUSED_E4:
1033 case Instruction::UNUSED_E5:
1034 case Instruction::UNUSED_E6:
1035 case Instruction::UNUSED_E7:
1036 case Instruction::UNUSED_E8:
1037 case Instruction::UNUSED_E9:
1038 case Instruction::UNUSED_EA:
1039 case Instruction::UNUSED_EB:
1040 case Instruction::UNUSED_EC:
1041 case Instruction::THROW_VERIFICATION_ERROR:
1042 case Instruction::UNUSED_EE:
1043 case Instruction::UNUSED_EF:
1044 case Instruction::UNUSED_F0:
1045 case Instruction::UNUSED_F1:
1046 case Instruction::UNUSED_F2:
1047 case Instruction::UNUSED_F3:
1048 case Instruction::UNUSED_F4:
1049 case Instruction::UNUSED_F5:
1050 case Instruction::UNUSED_F6:
1051 case Instruction::UNUSED_F7:
1052 case Instruction::UNUSED_F8:
1053 case Instruction::UNUSED_F9:
1054 case Instruction::UNUSED_FA:
1055 case Instruction::UNUSED_FB:
1056 case Instruction::UNUSED_FC:
1057 case Instruction::UNUSED_FD:
1058 case Instruction::UNUSED_FE:
1059 case Instruction::UNUSED_FF:
1060 LOG(FATAL) << "Dex file contains UNUSED bytecode: " << insn->Opcode();
1061 break;
1062 }
1063
1064#undef ARGS
1065}
1066
1067
1068void MethodCompiler::EmitInsn_Nop(uint32_t dex_pc,
1069 Instruction const* insn) {
Logan Chiene09a6b72011-12-27 17:50:21 +08001070
1071 uint16_t insn_signature = code_item_->insns_[dex_pc];
1072
1073 if (insn_signature == Instruction::kPackedSwitchSignature ||
1074 insn_signature == Instruction::kSparseSwitchSignature ||
1075 insn_signature == Instruction::kArrayDataSignature) {
1076 irb_.CreateUnreachable();
1077 } else{
1078 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1079 }
Shih-wei Liaod1fec812012-02-13 09:51:10 -08001080}
1081
1082
Logan Chien70f94b42011-12-27 17:49:11 +08001083void MethodCompiler::EmitInsn_Move(uint32_t dex_pc,
1084 Instruction const* insn,
1085 JType jty) {
Logan Chien48173132011-12-27 17:51:13 +08001086
1087 Instruction::DecodedInstruction dec_insn(insn);
1088
1089 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB_, jty, kReg);
1090 EmitStoreDalvikReg(dec_insn.vA_, jty, kReg, src_value);
1091
Logan Chien70f94b42011-12-27 17:49:11 +08001092 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1093}
1094
1095
1096void MethodCompiler::EmitInsn_MoveResult(uint32_t dex_pc,
1097 Instruction const* insn,
1098 JType jty) {
Logan Chien48173132011-12-27 17:51:13 +08001099
1100 Instruction::DecodedInstruction dec_insn(insn);
1101
1102 llvm::Value* src_value = EmitLoadDalvikRetValReg(jty, kReg);
1103 EmitStoreDalvikReg(dec_insn.vA_, jty, kReg, src_value);
1104
Logan Chien70f94b42011-12-27 17:49:11 +08001105 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1106}
1107
1108
1109void MethodCompiler::EmitInsn_MoveException(uint32_t dex_pc,
1110 Instruction const* insn) {
Logan Chien3354cec2012-01-13 14:29:03 +08001111
1112 Instruction::DecodedInstruction dec_insn(insn);
1113
1114 // Get thread-local exception field address
1115 llvm::Constant* exception_field_offset =
1116 irb_.getPtrEquivInt(Thread::ExceptionOffset().Int32Value());
1117
1118 llvm::Value* thread_object_addr =
1119 irb_.CreateCall(irb_.GetRuntime(GetCurrentThread));
1120
1121 llvm::Value* exception_field_addr =
1122 irb_.CreatePtrDisp(thread_object_addr, exception_field_offset,
1123 irb_.getJObjectTy()->getPointerTo());
1124
1125 // Get exception object address
1126 llvm::Value* exception_object_addr = irb_.CreateLoad(exception_field_addr);
1127
1128 // Set thread-local exception field address to NULL
1129 irb_.CreateStore(irb_.getJNull(), exception_field_addr);
1130
1131 // Keep the exception object in the Dalvik register
1132 EmitStoreDalvikReg(dec_insn.vA_, kObject, kAccurate, exception_object_addr);
1133
Logan Chien70f94b42011-12-27 17:49:11 +08001134 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1135}
1136
1137
1138void MethodCompiler::EmitInsn_ThrowException(uint32_t dex_pc,
1139 Instruction const* insn) {
Logan Chien6c6f12d2012-01-13 19:26:27 +08001140
1141 Instruction::DecodedInstruction dec_insn(insn);
1142
1143 llvm::Value* exception_addr =
1144 EmitLoadDalvikReg(dec_insn.vA_, kObject, kAccurate);
1145
1146 irb_.CreateCall(irb_.GetRuntime(ThrowException), exception_addr);
1147
1148 EmitBranchExceptionLandingPad(dex_pc);
Logan Chien70f94b42011-12-27 17:49:11 +08001149}
1150
1151
1152void MethodCompiler::EmitInsn_ReturnVoid(uint32_t dex_pc,
1153 Instruction const* insn) {
Logan Chien8898a272011-12-27 17:51:56 +08001154 // Garbage collection safe-point
1155 EmitGuard_GarbageCollectionSuspend(dex_pc);
1156
1157 // Return!
1158 irb_.CreateRetVoid();
Logan Chien70f94b42011-12-27 17:49:11 +08001159}
1160
1161
1162void MethodCompiler::EmitInsn_Return(uint32_t dex_pc,
1163 Instruction const* insn) {
Logan Chien8898a272011-12-27 17:51:56 +08001164
1165 Instruction::DecodedInstruction dec_insn(insn);
1166
1167 // Garbage collection safe-point
1168 EmitGuard_GarbageCollectionSuspend(dex_pc);
1169
1170 // Return!
1171 char ret_shorty = method_helper_.GetShorty()[0];
1172 llvm::Value* retval = EmitLoadDalvikReg(dec_insn.vA_, ret_shorty, kAccurate);
1173
1174 irb_.CreateRet(retval);
Logan Chien70f94b42011-12-27 17:49:11 +08001175}
1176
1177
1178void MethodCompiler::EmitInsn_LoadConstant(uint32_t dex_pc,
1179 Instruction const* insn,
1180 JType imm_jty) {
Shih-wei Liao798366e2012-02-16 09:25:33 -08001181
1182 Instruction::DecodedInstruction dec_insn(insn);
1183
1184 DCHECK(imm_jty == kInt || imm_jty == kLong) << imm_jty;
1185
1186 int64_t imm = 0;
1187
1188 switch (insn->Opcode()) {
1189 // 32-bit Immediate
1190 case Instruction::CONST_4:
1191 case Instruction::CONST_16:
1192 case Instruction::CONST:
1193 case Instruction::CONST_WIDE_16:
1194 case Instruction::CONST_WIDE_32:
1195 imm = static_cast<int64_t>(static_cast<int32_t>(dec_insn.vB_));
1196 break;
1197
1198 case Instruction::CONST_HIGH16:
1199 imm = static_cast<int64_t>(static_cast<int32_t>(
1200 static_cast<uint32_t>(static_cast<uint16_t>(dec_insn.vB_)) << 16));
1201 break;
1202
1203 // 64-bit Immediate
1204 case Instruction::CONST_WIDE:
1205 imm = static_cast<int64_t>(dec_insn.vB_wide_);
1206 break;
1207
1208 case Instruction::CONST_WIDE_HIGH16:
1209 imm = static_cast<int64_t>(
1210 static_cast<uint64_t>(static_cast<uint16_t>(dec_insn.vB_)) << 48);
1211 break;
1212
1213 // Unknown opcode for load constant (unreachable)
1214 default:
1215 LOG(FATAL) << "Unknown opcode for load constant: " << insn->Opcode();
1216 break;
1217 }
1218
1219 // Store the non-object register
1220 llvm::Type* imm_type = irb_.getJType(imm_jty, kAccurate);
1221 llvm::Constant* imm_value = llvm::ConstantInt::getSigned(imm_type, imm);
1222 EmitStoreDalvikReg(dec_insn.vA_, imm_jty, kAccurate, imm_value);
1223
1224 // Store the object register if it is possible to be null.
1225 if (imm_jty == kInt && imm == 0) {
1226 EmitStoreDalvikReg(dec_insn.vA_, kObject, kAccurate, irb_.getJNull());
1227 }
1228
Logan Chien70f94b42011-12-27 17:49:11 +08001229 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1230}
1231
1232
1233void MethodCompiler::EmitInsn_LoadConstantString(uint32_t dex_pc,
1234 Instruction const* insn) {
1235 // UNIMPLEMENTED(WARNING);
1236 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1237}
1238
1239
Logan Chien27b30252012-01-14 03:43:35 +08001240llvm::Value* MethodCompiler::EmitLoadConstantClass(uint32_t dex_pc,
1241 uint32_t type_idx) {
1242 if (!compiler_->CanAccessTypeWithoutChecks(method_idx_, dex_cache_,
1243 *dex_file_, type_idx)) {
1244 llvm::Value* type_idx_value = irb_.getInt32(type_idx);
1245
1246 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1247
1248 llvm::Function* runtime_func =
1249 irb_.GetRuntime(InitializeTypeAndVerifyAccess);
1250
1251 llvm::Value* type_object_addr =
1252 irb_.CreateCall2(runtime_func, type_idx_value, method_object_addr);
1253
1254 EmitGuard_ExceptionLandingPad(dex_pc);
1255
1256 return type_object_addr;
1257
1258 } else {
1259 // Try to load the class (type) object from the test cache.
1260 llvm::Value* type_field_addr =
1261 EmitLoadDexCacheResolvedTypeFieldAddr(type_idx);
1262
1263 llvm::Value* type_object_addr = irb_.CreateLoad(type_field_addr);
1264
1265 if (compiler_->CanAssumeTypeIsPresentInDexCache(dex_cache_, type_idx)) {
1266 return type_object_addr;
1267 }
1268
1269 llvm::BasicBlock* block_original = irb_.GetInsertBlock();
1270
1271 // Test whether class (type) object is in the dex cache or not
1272 llvm::Value* equal_null =
1273 irb_.CreateICmpEQ(type_object_addr, irb_.getJNull());
1274
1275 llvm::BasicBlock* block_cont =
1276 CreateBasicBlockWithDexPC(dex_pc, "cont");
1277
1278 llvm::BasicBlock* block_load_class =
1279 CreateBasicBlockWithDexPC(dex_pc, "load_class");
1280
1281 irb_.CreateCondBr(equal_null, block_load_class, block_cont);
1282
1283 // Failback routine to load the class object
1284 irb_.SetInsertPoint(block_load_class);
1285
1286 llvm::Function* runtime_func = irb_.GetRuntime(InitializeType);
1287
1288 llvm::Constant* type_idx_value = irb_.getInt32(type_idx);
1289
1290 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1291
1292 llvm::Value* loaded_type_object_addr =
1293 irb_.CreateCall2(runtime_func, type_idx_value, method_object_addr);
1294
1295 EmitGuard_ExceptionLandingPad(dex_pc);
1296
1297 llvm::BasicBlock* block_after_load_class = irb_.GetInsertBlock();
1298
1299 irb_.CreateBr(block_cont);
1300
1301 // Now the class object must be loaded
1302 irb_.SetInsertPoint(block_cont);
1303
1304 llvm::PHINode* phi = irb_.CreatePHI(irb_.getJObjectTy(), 2);
1305
1306 phi->addIncoming(type_object_addr, block_original);
1307 phi->addIncoming(loaded_type_object_addr, block_after_load_class);
1308
1309 return phi;
1310 }
1311}
1312
1313
Logan Chien70f94b42011-12-27 17:49:11 +08001314void MethodCompiler::EmitInsn_LoadConstantClass(uint32_t dex_pc,
1315 Instruction const* insn) {
Logan Chien27b30252012-01-14 03:43:35 +08001316
1317 Instruction::DecodedInstruction dec_insn(insn);
1318
1319 llvm::Value* type_object_addr = EmitLoadConstantClass(dex_pc, dec_insn.vB_);
1320 EmitStoreDalvikReg(dec_insn.vA_, kObject, kAccurate, type_object_addr);
1321
Logan Chien70f94b42011-12-27 17:49:11 +08001322 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1323}
1324
1325
1326void MethodCompiler::EmitInsn_MonitorEnter(uint32_t dex_pc,
1327 Instruction const* insn) {
Logan Chien9e0dbe42012-01-13 12:11:37 +08001328
1329 Instruction::DecodedInstruction dec_insn(insn);
1330
1331 llvm::Value* object_addr =
1332 EmitLoadDalvikReg(dec_insn.vA_, kObject, kAccurate);
1333
1334 // TODO: Slow path always. May not need NullPointerException check.
1335 EmitGuard_NullPointerException(dex_pc, object_addr);
1336
1337 irb_.CreateCall(irb_.GetRuntime(LockObject), object_addr);
1338 EmitGuard_ExceptionLandingPad(dex_pc);
1339
Logan Chien70f94b42011-12-27 17:49:11 +08001340 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1341}
1342
1343
1344void MethodCompiler::EmitInsn_MonitorExit(uint32_t dex_pc,
1345 Instruction const* insn) {
Logan Chien9e0dbe42012-01-13 12:11:37 +08001346
1347 Instruction::DecodedInstruction dec_insn(insn);
1348
1349 llvm::Value* object_addr =
1350 EmitLoadDalvikReg(dec_insn.vA_, kObject, kAccurate);
1351
1352 EmitGuard_NullPointerException(dex_pc, object_addr);
1353
1354 irb_.CreateCall(irb_.GetRuntime(UnlockObject), object_addr);
1355 EmitGuard_ExceptionLandingPad(dex_pc);
1356
Logan Chien70f94b42011-12-27 17:49:11 +08001357 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1358}
1359
1360
1361void MethodCompiler::EmitInsn_CheckCast(uint32_t dex_pc,
1362 Instruction const* insn) {
1363 // UNIMPLEMENTED(WARNING);
1364 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1365}
1366
1367
1368void MethodCompiler::EmitInsn_InstanceOf(uint32_t dex_pc,
1369 Instruction const* insn) {
1370 // UNIMPLEMENTED(WARNING);
1371 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1372}
1373
1374
Logan Chien61bb6142012-02-03 15:34:53 +08001375llvm::Value* MethodCompiler::EmitLoadArrayLength(llvm::Value* array) {
1376 // Load array length field address
1377 llvm::Constant* array_len_field_offset =
1378 irb_.getPtrEquivInt(Array::LengthOffset().Int32Value());
1379
1380 llvm::Value* array_len_field_addr =
1381 irb_.CreatePtrDisp(array, array_len_field_offset,
1382 irb_.getJIntTy()->getPointerTo());
1383
1384 // Load array length
1385 return irb_.CreateLoad(array_len_field_addr);
1386}
1387
1388
Logan Chien70f94b42011-12-27 17:49:11 +08001389void MethodCompiler::EmitInsn_ArrayLength(uint32_t dex_pc,
1390 Instruction const* insn) {
Logan Chien61bb6142012-02-03 15:34:53 +08001391
1392 Instruction::DecodedInstruction dec_insn(insn);
1393
1394 // Get the array object address
1395 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB_, kObject, kAccurate);
1396 EmitGuard_NullPointerException(dex_pc, array_addr);
1397
1398 // Get the array length and store it to the register
1399 llvm::Value* array_len = EmitLoadArrayLength(array_addr);
1400 EmitStoreDalvikReg(dec_insn.vA_, kInt, kAccurate, array_len);
1401
Logan Chien70f94b42011-12-27 17:49:11 +08001402 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1403}
1404
1405
1406void MethodCompiler::EmitInsn_NewInstance(uint32_t dex_pc,
1407 Instruction const* insn) {
1408 // UNIMPLEMENTED(WARNING);
1409 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1410}
1411
1412
1413void MethodCompiler::EmitInsn_NewArray(uint32_t dex_pc,
1414 Instruction const* insn) {
1415 // UNIMPLEMENTED(WARNING);
1416 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1417}
1418
1419
1420void MethodCompiler::EmitInsn_FilledNewArray(uint32_t dex_pc,
1421 Instruction const* insn,
1422 bool is_range) {
1423 // UNIMPLEMENTED(WARNING);
1424 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1425}
1426
1427
1428void MethodCompiler::EmitInsn_FillArrayData(uint32_t dex_pc,
1429 Instruction const* insn) {
1430 // UNIMPLEMENTED(WARNING);
1431 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1432}
1433
1434
1435void MethodCompiler::EmitInsn_UnconditionalBranch(uint32_t dex_pc,
1436 Instruction const* insn) {
Logan Chiena466c162011-12-27 17:55:46 +08001437
1438 Instruction::DecodedInstruction dec_insn(insn);
1439
1440 int32_t branch_offset = dec_insn.vA_;
1441
1442 if (branch_offset <= 0) {
1443 // Garbage collection safe-point on backward branch
1444 EmitGuard_GarbageCollectionSuspend(dex_pc);
1445 }
1446
1447 irb_.CreateBr(GetBasicBlock(dex_pc + branch_offset));
Logan Chien70f94b42011-12-27 17:49:11 +08001448}
1449
1450
1451void MethodCompiler::EmitInsn_PackedSwitch(uint32_t dex_pc,
1452 Instruction const* insn) {
Logan Chien7a89b6d2011-12-27 17:56:56 +08001453
1454 Instruction::DecodedInstruction dec_insn(insn);
1455
1456 struct PACKED Payload {
1457 uint16_t ident_;
1458 uint16_t num_cases_;
1459 int32_t first_key_;
1460 int32_t targets_[];
1461 };
1462
1463 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
1464 static_cast<int32_t>(dec_insn.vB_);
1465
1466 Payload const* payload =
1467 reinterpret_cast<Payload const*>(code_item_->insns_ + payload_offset);
1468
1469 llvm::Value* value = EmitLoadDalvikReg(dec_insn.vA_, kInt, kAccurate);
1470
1471 llvm::SwitchInst* sw =
1472 irb_.CreateSwitch(value, GetNextBasicBlock(dex_pc), payload->num_cases_);
1473
1474 for (uint16_t i = 0; i < payload->num_cases_; ++i) {
1475 sw->addCase(irb_.getInt32(payload->first_key_ + i),
1476 GetBasicBlock(dex_pc + payload->targets_[i]));
1477 }
Logan Chien70f94b42011-12-27 17:49:11 +08001478}
1479
1480
1481void MethodCompiler::EmitInsn_SparseSwitch(uint32_t dex_pc,
1482 Instruction const* insn) {
Logan Chien7a89b6d2011-12-27 17:56:56 +08001483
1484 Instruction::DecodedInstruction dec_insn(insn);
1485
1486 struct PACKED Payload {
1487 uint16_t ident_;
1488 uint16_t num_cases_;
1489 int32_t keys_and_targets_[];
1490 };
1491
1492 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
1493 static_cast<int32_t>(dec_insn.vB_);
1494
1495 Payload const* payload =
1496 reinterpret_cast<Payload const*>(code_item_->insns_ + payload_offset);
1497
1498 int32_t const* keys = payload->keys_and_targets_;
1499 int32_t const* targets = payload->keys_and_targets_ + payload->num_cases_;
1500
1501 llvm::Value* value = EmitLoadDalvikReg(dec_insn.vA_, kInt, kAccurate);
1502
1503 llvm::SwitchInst* sw =
1504 irb_.CreateSwitch(value, GetNextBasicBlock(dex_pc), payload->num_cases_);
1505
1506 for (size_t i = 0; i < payload->num_cases_; ++i) {
1507 sw->addCase(irb_.getInt32(keys[i]), GetBasicBlock(dex_pc + targets[i]));
1508 }
Logan Chien70f94b42011-12-27 17:49:11 +08001509}
1510
1511
1512void MethodCompiler::EmitInsn_FPCompare(uint32_t dex_pc,
1513 Instruction const* insn,
1514 JType fp_jty,
1515 bool gt_bias) {
Logan Chien2c37e8e2011-12-27 17:58:46 +08001516
1517 Instruction::DecodedInstruction dec_insn(insn);
1518
1519 DCHECK(fp_jty == kFloat || fp_jty == kDouble) << "JType: " << fp_jty;
1520
1521 llvm::Value* src1_value = EmitLoadDalvikReg(dec_insn.vB_, fp_jty, kAccurate);
1522 llvm::Value* src2_value = EmitLoadDalvikReg(dec_insn.vC_, fp_jty, kAccurate);
1523
1524 llvm::Value* cmp_eq = irb_.CreateFCmpOEQ(src1_value, src2_value);
1525 llvm::Value* cmp_lt;
1526
1527 if (gt_bias) {
1528 cmp_lt = irb_.CreateFCmpOLT(src1_value, src2_value);
1529 } else {
1530 cmp_lt = irb_.CreateFCmpULT(src1_value, src2_value);
1531 }
1532
1533 llvm::Value* result = EmitCompareResultSelection(cmp_eq, cmp_lt);
1534 EmitStoreDalvikReg(dec_insn.vA_, kInt, kAccurate, result);
1535
Logan Chien70f94b42011-12-27 17:49:11 +08001536 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1537}
1538
1539
1540void MethodCompiler::EmitInsn_LongCompare(uint32_t dex_pc,
1541 Instruction const* insn) {
Logan Chien2c37e8e2011-12-27 17:58:46 +08001542
1543 Instruction::DecodedInstruction dec_insn(insn);
1544
1545 llvm::Value* src1_value = EmitLoadDalvikReg(dec_insn.vB_, kLong, kAccurate);
1546 llvm::Value* src2_value = EmitLoadDalvikReg(dec_insn.vC_, kLong, kAccurate);
1547
1548 llvm::Value* cmp_eq = irb_.CreateICmpEQ(src1_value, src2_value);
1549 llvm::Value* cmp_lt = irb_.CreateICmpSLT(src1_value, src2_value);
1550
1551 llvm::Value* result = EmitCompareResultSelection(cmp_eq, cmp_lt);
1552 EmitStoreDalvikReg(dec_insn.vA_, kInt, kAccurate, result);
1553
Logan Chien70f94b42011-12-27 17:49:11 +08001554 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1555}
1556
1557
Logan Chien2c37e8e2011-12-27 17:58:46 +08001558llvm::Value* MethodCompiler::EmitCompareResultSelection(llvm::Value* cmp_eq,
1559 llvm::Value* cmp_lt) {
1560
1561 llvm::Constant* zero = irb_.getJInt(0);
1562 llvm::Constant* pos1 = irb_.getJInt(1);
1563 llvm::Constant* neg1 = irb_.getJInt(-1);
1564
1565 llvm::Value* result_lt = irb_.CreateSelect(cmp_lt, neg1, pos1);
1566 llvm::Value* result_eq = irb_.CreateSelect(cmp_eq, zero, result_lt);
1567
1568 return result_eq;
1569}
1570
1571
Logan Chien70f94b42011-12-27 17:49:11 +08001572void MethodCompiler::EmitInsn_BinaryConditionalBranch(uint32_t dex_pc,
1573 Instruction const* insn,
1574 CondBranchKind cond) {
Logan Chiena78e3c82011-12-27 17:59:35 +08001575
1576 Instruction::DecodedInstruction dec_insn(insn);
1577
1578 int8_t src1_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vA_);
1579 int8_t src2_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vB_);
1580
1581 DCHECK_NE(kRegUnknown, src1_reg_cat);
1582 DCHECK_NE(kRegUnknown, src2_reg_cat);
1583 DCHECK_NE(kRegCat2, src1_reg_cat);
1584 DCHECK_NE(kRegCat2, src2_reg_cat);
1585
1586 int32_t branch_offset = dec_insn.vC_;
1587
1588 if (branch_offset <= 0) {
1589 // Garbage collection safe-point on backward branch
1590 EmitGuard_GarbageCollectionSuspend(dex_pc);
1591 }
1592
1593 if (src1_reg_cat == kRegZero && src2_reg_cat == kRegZero) {
1594 irb_.CreateBr(GetBasicBlock(dex_pc + branch_offset));
1595 return;
1596 }
1597
1598 llvm::Value* src1_value;
1599 llvm::Value* src2_value;
1600
1601 if (src1_reg_cat != kRegZero && src2_reg_cat != kRegZero) {
1602 CHECK_EQ(src1_reg_cat, src2_reg_cat);
1603
1604 if (src1_reg_cat == kRegCat1nr) {
1605 src1_value = EmitLoadDalvikReg(dec_insn.vA_, kInt, kAccurate);
1606 src2_value = EmitLoadDalvikReg(dec_insn.vB_, kInt, kAccurate);
1607 } else {
1608 src1_value = EmitLoadDalvikReg(dec_insn.vA_, kObject, kAccurate);
1609 src2_value = EmitLoadDalvikReg(dec_insn.vB_, kObject, kAccurate);
1610 }
1611 } else {
1612 DCHECK(src1_reg_cat == kRegZero ||
1613 src2_reg_cat == kRegZero);
1614
1615 if (src1_reg_cat == kRegZero) {
1616 if (src2_reg_cat == kRegCat1nr) {
1617 src1_value = irb_.getJInt(0);
1618 src2_value = EmitLoadDalvikReg(dec_insn.vA_, kInt, kAccurate);
1619 } else {
1620 src1_value = irb_.getJNull();
1621 src2_value = EmitLoadDalvikReg(dec_insn.vA_, kObject, kAccurate);
1622 }
1623 } else { // src2_reg_cat == kRegZero
1624 if (src2_reg_cat == kRegCat1nr) {
1625 src1_value = EmitLoadDalvikReg(dec_insn.vA_, kInt, kAccurate);
1626 src2_value = irb_.getJInt(0);
1627 } else {
1628 src1_value = EmitLoadDalvikReg(dec_insn.vA_, kObject, kAccurate);
1629 src2_value = irb_.getJNull();
1630 }
1631 }
1632 }
1633
1634 llvm::Value* cond_value =
1635 EmitConditionResult(src1_value, src2_value, cond);
1636
1637 irb_.CreateCondBr(cond_value,
1638 GetBasicBlock(dex_pc + branch_offset),
1639 GetNextBasicBlock(dex_pc));
Logan Chien70f94b42011-12-27 17:49:11 +08001640}
1641
1642
1643void MethodCompiler::EmitInsn_UnaryConditionalBranch(uint32_t dex_pc,
1644 Instruction const* insn,
1645 CondBranchKind cond) {
Logan Chiena78e3c82011-12-27 17:59:35 +08001646
1647 Instruction::DecodedInstruction dec_insn(insn);
1648
1649 int8_t src_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vA_);
1650
1651 DCHECK_NE(kRegUnknown, src_reg_cat);
1652 DCHECK_NE(kRegCat2, src_reg_cat);
1653
1654 int32_t branch_offset = dec_insn.vB_;
1655
1656 if (branch_offset <= 0) {
1657 // Garbage collection safe-point on backward branch
1658 EmitGuard_GarbageCollectionSuspend(dex_pc);
1659 }
1660
1661 if (src_reg_cat == kRegZero) {
1662 irb_.CreateBr(GetBasicBlock(dex_pc + branch_offset));
1663 return;
1664 }
1665
1666 llvm::Value* src1_value;
1667 llvm::Value* src2_value;
1668
1669 if (src_reg_cat == kRegCat1nr) {
1670 src1_value = EmitLoadDalvikReg(dec_insn.vA_, kInt, kAccurate);
1671 src2_value = irb_.getInt32(0);
1672 } else {
1673 src1_value = EmitLoadDalvikReg(dec_insn.vA_, kObject, kAccurate);
1674 src2_value = irb_.getJNull();
1675 }
1676
1677 llvm::Value* cond_value =
1678 EmitConditionResult(src1_value, src2_value, cond);
1679
1680 irb_.CreateCondBr(cond_value,
1681 GetBasicBlock(dex_pc + branch_offset),
1682 GetNextBasicBlock(dex_pc));
1683}
1684
1685
1686RegCategory MethodCompiler::GetInferredRegCategory(uint32_t dex_pc,
1687 uint16_t reg_idx) {
1688 InferredRegCategoryMap const* map = method_->GetInferredRegCategoryMap();
1689 CHECK_NE(map, static_cast<InferredRegCategoryMap*>(NULL));
1690
1691 return map->GetRegCategory(dex_pc, reg_idx);
1692}
1693
1694
1695llvm::Value* MethodCompiler::EmitConditionResult(llvm::Value* lhs,
1696 llvm::Value* rhs,
1697 CondBranchKind cond) {
1698 switch (cond) {
1699 case kCondBranch_EQ:
1700 return irb_.CreateICmpEQ(lhs, rhs);
1701
1702 case kCondBranch_NE:
1703 return irb_.CreateICmpNE(lhs, rhs);
1704
1705 case kCondBranch_LT:
1706 return irb_.CreateICmpSLT(lhs, rhs);
1707
1708 case kCondBranch_GE:
1709 return irb_.CreateICmpSGE(lhs, rhs);
1710
1711 case kCondBranch_GT:
1712 return irb_.CreateICmpSGT(lhs, rhs);
1713
1714 case kCondBranch_LE:
1715 return irb_.CreateICmpSLE(lhs, rhs);
1716
1717 default: // Unreachable
1718 LOG(FATAL) << "Unknown conditional branch kind: " << cond;
1719 return NULL;
1720 }
Logan Chien70f94b42011-12-27 17:49:11 +08001721}
1722
1723
Logan Chiene27fdbb2012-01-02 23:27:26 +08001724void
1725MethodCompiler::EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc,
1726 llvm::Value* array,
1727 llvm::Value* index) {
1728 llvm::Value* array_len = EmitLoadArrayLength(array);
1729
1730 llvm::Value* cmp = irb_.CreateICmpUGE(index, array_len);
1731
1732 llvm::BasicBlock* block_exception =
1733 CreateBasicBlockWithDexPC(dex_pc, "overflow");
1734
1735 llvm::BasicBlock* block_continue =
1736 CreateBasicBlockWithDexPC(dex_pc, "cont");
1737
1738 irb_.CreateCondBr(cmp, block_exception, block_continue);
1739
1740 irb_.SetInsertPoint(block_exception);
1741 irb_.CreateCall2(irb_.GetRuntime(ThrowIndexOutOfBounds), index, array_len);
1742 EmitBranchExceptionLandingPad(dex_pc);
1743
1744 irb_.SetInsertPoint(block_continue);
1745}
1746
1747
1748void MethodCompiler::EmitGuard_ArrayException(uint32_t dex_pc,
1749 llvm::Value* array,
1750 llvm::Value* index) {
1751 EmitGuard_NullPointerException(dex_pc, array);
1752 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array, index);
1753}
1754
1755
1756// Emit Array GetElementPtr
1757llvm::Value* MethodCompiler::EmitArrayGEP(llvm::Value* array_addr,
1758 llvm::Value* index_value,
1759 llvm::Type* elem_type) {
1760
1761 llvm::Constant* data_offset_value =
1762 irb_.getPtrEquivInt(Array::DataOffset().Int32Value());
1763
1764 llvm::Value* array_data_addr =
1765 irb_.CreatePtrDisp(array_addr, data_offset_value,
1766 elem_type->getPointerTo());
1767
1768 return irb_.CreateGEP(array_data_addr, index_value);
1769}
1770
1771
Logan Chien70f94b42011-12-27 17:49:11 +08001772void MethodCompiler::EmitInsn_AGet(uint32_t dex_pc,
1773 Instruction const* insn,
1774 JType elem_jty) {
Logan Chiene27fdbb2012-01-02 23:27:26 +08001775
1776 Instruction::DecodedInstruction dec_insn(insn);
1777
1778 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB_, kObject, kAccurate);
1779 llvm::Value* index_value = EmitLoadDalvikReg(dec_insn.vC_, kInt, kAccurate);
1780
1781 EmitGuard_ArrayException(dex_pc, array_addr, index_value);
1782
1783 llvm::Type* elem_type = irb_.getJType(elem_jty, kArray);
1784
1785 llvm::Value* array_elem_addr =
1786 EmitArrayGEP(array_addr, index_value, elem_type);
1787
1788 llvm::Value* array_elem_value = irb_.CreateLoad(array_elem_addr);
1789
1790 EmitStoreDalvikReg(dec_insn.vA_, elem_jty, kArray, array_elem_value);
1791
Logan Chien70f94b42011-12-27 17:49:11 +08001792 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1793}
1794
1795
1796void MethodCompiler::EmitInsn_APut(uint32_t dex_pc,
1797 Instruction const* insn,
1798 JType elem_jty) {
Logan Chien8dabb432012-01-02 23:29:32 +08001799
1800 Instruction::DecodedInstruction dec_insn(insn);
1801
1802 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB_, kObject, kAccurate);
1803 llvm::Value* index_value = EmitLoadDalvikReg(dec_insn.vC_, kInt, kAccurate);
1804
1805 EmitGuard_ArrayException(dex_pc, array_addr, index_value);
1806
1807 llvm::Type* elem_type = irb_.getJType(elem_jty, kArray);
1808
1809 llvm::Value* array_elem_addr =
1810 EmitArrayGEP(array_addr, index_value, elem_type);
1811
1812 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA_, elem_jty, kArray);
1813
1814 irb_.CreateStore(new_value, array_elem_addr);
1815
Logan Chien70f94b42011-12-27 17:49:11 +08001816 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1817}
1818
1819
Logan Chien48f1d2a2012-01-02 22:49:53 +08001820void MethodCompiler::PrintUnresolvedFieldWarning(int32_t field_idx) {
1821 DexFile const& dex_file = method_helper_.GetDexFile();
1822 DexFile::FieldId const& field_id = dex_file.GetFieldId(field_idx);
1823
1824 LOG(WARNING) << "unable to resolve static field " << field_idx << " ("
1825 << dex_file.GetFieldName(field_id) << ") in "
1826 << dex_file.GetFieldDeclaringClassDescriptor(field_id);
1827}
1828
1829
Logan Chien70f94b42011-12-27 17:49:11 +08001830void MethodCompiler::EmitInsn_IGet(uint32_t dex_pc,
1831 Instruction const* insn,
1832 JType field_jty) {
Logan Chien48f1d2a2012-01-02 22:49:53 +08001833
1834 Instruction::DecodedInstruction dec_insn(insn);
1835
1836 uint32_t reg_idx = dec_insn.vB_;
1837 uint32_t field_idx = dec_insn.vC_;
1838
1839 Field* field = dex_cache_->GetResolvedField(field_idx);
1840
1841 llvm::Value* object_addr = EmitLoadDalvikReg(reg_idx, kObject, kAccurate);
1842
1843 EmitGuard_NullPointerException(dex_pc, object_addr);
1844
1845 llvm::Value* field_value;
1846
1847 if (field == NULL) {
1848 PrintUnresolvedFieldWarning(field_idx);
1849
1850 llvm::Function* runtime_func;
1851
1852 if (field_jty == kObject) {
1853 runtime_func = irb_.GetRuntime(SetObjectInstance);
1854 } else if (field_jty == kLong || field_jty == kDouble) {
1855 runtime_func = irb_.GetRuntime(Set64Instance);
1856 } else {
1857 runtime_func = irb_.GetRuntime(Set32Instance);
1858 }
1859
1860 llvm::ConstantInt* field_idx_value = irb_.getInt32(field_idx);
1861
1862 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1863
1864 field_value = irb_.CreateCall2(runtime_func, field_idx_value,
1865 method_object_addr);
1866
1867 EmitGuard_ExceptionLandingPad(dex_pc);
1868
1869 } else {
1870 llvm::PointerType* field_type =
1871 irb_.getJType(field_jty, kField)->getPointerTo();
1872
1873 llvm::ConstantInt* field_offset =
1874 irb_.getPtrEquivInt(field->GetOffset().Int32Value());
1875
1876 llvm::Value* field_addr =
1877 irb_.CreatePtrDisp(object_addr, field_offset, field_type);
1878
1879 field_value = irb_.CreateLoad(field_addr);
1880 }
1881
1882 EmitStoreDalvikReg(dec_insn.vA_, field_jty, kField, field_value);
1883
Logan Chien70f94b42011-12-27 17:49:11 +08001884 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1885}
1886
1887
1888void MethodCompiler::EmitInsn_IPut(uint32_t dex_pc,
1889 Instruction const* insn,
1890 JType field_jty) {
Logan Chiendd6aa872012-01-03 16:06:32 +08001891
1892 Instruction::DecodedInstruction dec_insn(insn);
1893
1894 uint32_t reg_idx = dec_insn.vB_;
1895 uint32_t field_idx = dec_insn.vC_;
1896
1897 Field* field = dex_cache_->GetResolvedField(field_idx);
1898
1899 llvm::Value* object_addr = EmitLoadDalvikReg(reg_idx, kObject, kAccurate);
1900
1901 EmitGuard_NullPointerException(dex_pc, object_addr);
1902
1903 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA_, field_jty, kField);
1904
1905 if (field == NULL) {
1906 PrintUnresolvedFieldWarning(field_idx);
1907
1908 llvm::Function* runtime_func;
1909
1910 if (field_jty == kObject) {
1911 runtime_func = irb_.GetRuntime(SetObjectInstance);
1912 } else if (field_jty == kLong || field_jty == kDouble) {
1913 runtime_func = irb_.GetRuntime(Set64Instance);
1914 } else {
1915 runtime_func = irb_.GetRuntime(Set32Instance);
1916 }
1917
1918 llvm::Value* field_idx_value = irb_.getInt32(field_idx);
1919
1920 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1921
1922 irb_.CreateCall3(runtime_func, field_idx_value,
1923 method_object_addr, new_value);
1924
1925 EmitGuard_ExceptionLandingPad(dex_pc);
1926
1927 } else {
1928 llvm::PointerType* field_type =
1929 irb_.getJType(field_jty, kField)->getPointerTo();
1930
1931 llvm::Value* field_offset =
1932 irb_.getPtrEquivInt(field->GetOffset().Int32Value());
1933
1934 llvm::Value* field_addr =
1935 irb_.CreatePtrDisp(object_addr, field_offset, field_type);
1936
1937 irb_.CreateStore(new_value, field_addr);
1938 }
1939
Logan Chien70f94b42011-12-27 17:49:11 +08001940 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1941}
1942
1943
1944void MethodCompiler::EmitInsn_SGet(uint32_t dex_pc,
1945 Instruction const* insn,
1946 JType field_jty) {
1947 // UNIMPLEMENTED(WARNING);
1948 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1949}
1950
1951
1952void MethodCompiler::EmitInsn_SPut(uint32_t dex_pc,
1953 Instruction const* insn,
1954 JType field_jty) {
1955 // UNIMPLEMENTED(WARNING);
1956 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1957}
1958
1959
1960void MethodCompiler::EmitInsn_InvokeVirtual(uint32_t dex_pc,
1961 Instruction const* insn,
1962 bool is_range) {
1963 // UNIMPLEMENTED(WARNING);
1964 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1965}
1966
1967
1968void MethodCompiler::EmitInsn_InvokeSuper(uint32_t dex_pc,
1969 Instruction const* insn,
1970 bool is_range) {
1971 // UNIMPLEMENTED(WARNING);
1972 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1973}
1974
1975
1976void MethodCompiler::EmitInsn_InvokeDirect(uint32_t dex_pc,
1977 Instruction const* insn,
1978 bool is_range) {
1979 // UNIMPLEMENTED(WARNING);
1980 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1981}
1982
1983
1984void MethodCompiler::EmitInsn_InvokeStatic(uint32_t dex_pc,
1985 Instruction const* insn,
1986 bool is_range) {
1987 // UNIMPLEMENTED(WARNING);
1988 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1989}
1990
1991
1992void MethodCompiler::EmitInsn_InvokeInterface(uint32_t dex_pc,
1993 Instruction const* insn,
1994 bool is_range) {
1995 // UNIMPLEMENTED(WARNING);
1996 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1997}
1998
1999
2000void MethodCompiler::EmitInsn_Neg(uint32_t dex_pc,
2001 Instruction const* insn,
2002 JType op_jty) {
Logan Chien1b5685f2011-12-27 18:01:14 +08002003
2004 Instruction::DecodedInstruction dec_insn(insn);
2005
2006 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
2007
2008 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB_, op_jty, kAccurate);
2009 llvm::Value* result_value = irb_.CreateNeg(src_value);
2010 EmitStoreDalvikReg(dec_insn.vA_, op_jty, kAccurate, result_value);
2011
Logan Chien70f94b42011-12-27 17:49:11 +08002012 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2013}
2014
2015
2016void MethodCompiler::EmitInsn_Not(uint32_t dex_pc,
2017 Instruction const* insn,
2018 JType op_jty) {
Logan Chiene53750d2011-12-27 18:02:27 +08002019
2020 Instruction::DecodedInstruction dec_insn(insn);
2021
2022 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
2023
2024 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB_, op_jty, kAccurate);
2025 llvm::Value* result_value =
2026 irb_.CreateXor(src_value, static_cast<uint64_t>(-1));
2027
2028 EmitStoreDalvikReg(dec_insn.vA_, op_jty, kAccurate, result_value);
2029
Logan Chien70f94b42011-12-27 17:49:11 +08002030 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2031}
2032
2033
2034void MethodCompiler::EmitInsn_SExt(uint32_t dex_pc,
2035 Instruction const* insn) {
Logan Chien61752ad2011-12-27 18:03:51 +08002036
2037 Instruction::DecodedInstruction dec_insn(insn);
2038
2039 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB_, kInt, kAccurate);
2040 llvm::Value* result_value = irb_.CreateSExt(src_value, irb_.getJLongTy());
2041 EmitStoreDalvikReg(dec_insn.vA_, kLong, kAccurate, result_value);
2042
Logan Chien70f94b42011-12-27 17:49:11 +08002043 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2044}
2045
2046
2047void MethodCompiler::EmitInsn_Trunc(uint32_t dex_pc,
2048 Instruction const* insn) {
Logan Chien17a57662011-12-27 18:05:14 +08002049
2050 Instruction::DecodedInstruction dec_insn(insn);
2051
2052 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB_, kLong, kAccurate);
2053 llvm::Value* result_value = irb_.CreateTrunc(src_value, irb_.getJIntTy());
2054 EmitStoreDalvikReg(dec_insn.vA_, kInt, kAccurate, result_value);
2055
Logan Chien70f94b42011-12-27 17:49:11 +08002056 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2057}
2058
2059
2060void MethodCompiler::EmitInsn_TruncAndSExt(uint32_t dex_pc,
2061 Instruction const* insn,
2062 unsigned N) {
Logan Chienb6744c52011-12-27 18:06:26 +08002063
2064 Instruction::DecodedInstruction dec_insn(insn);
2065
2066 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB_, kInt, kAccurate);
2067
2068 llvm::Value* trunc_value =
2069 irb_.CreateTrunc(src_value, llvm::Type::getIntNTy(*context_, N));
2070
2071 llvm::Value* result_value = irb_.CreateSExt(trunc_value, irb_.getJIntTy());
2072
2073 EmitStoreDalvikReg(dec_insn.vA_, kInt, kAccurate, result_value);
2074
Logan Chien70f94b42011-12-27 17:49:11 +08002075 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2076}
2077
2078
2079void MethodCompiler::EmitInsn_TruncAndZExt(uint32_t dex_pc,
2080 Instruction const* insn,
2081 unsigned N) {
Logan Chienb6744c52011-12-27 18:06:26 +08002082
2083 Instruction::DecodedInstruction dec_insn(insn);
2084
2085 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB_, kInt, kAccurate);
2086
2087 llvm::Value* trunc_value =
2088 irb_.CreateTrunc(src_value, llvm::Type::getIntNTy(*context_, N));
2089
2090 llvm::Value* result_value = irb_.CreateZExt(trunc_value, irb_.getJIntTy());
2091
2092 EmitStoreDalvikReg(dec_insn.vA_, kInt, kAccurate, result_value);
2093
Logan Chien70f94b42011-12-27 17:49:11 +08002094 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2095}
2096
2097
2098void MethodCompiler::EmitInsn_FNeg(uint32_t dex_pc,
2099 Instruction const* insn,
2100 JType op_jty) {
Logan Chien7a48b092011-12-27 18:07:45 +08002101
2102 Instruction::DecodedInstruction dec_insn(insn);
2103
2104 DCHECK(op_jty == kFloat || op_jty == kDouble) << op_jty;
2105
2106 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB_, op_jty, kAccurate);
2107 llvm::Value* result_value = irb_.CreateFNeg(src_value);
2108 EmitStoreDalvikReg(dec_insn.vA_, op_jty, kAccurate, result_value);
2109
Logan Chien70f94b42011-12-27 17:49:11 +08002110 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2111}
2112
2113
2114void MethodCompiler::EmitInsn_IntToFP(uint32_t dex_pc,
2115 Instruction const* insn,
2116 JType src_jty,
2117 JType dest_jty) {
Logan Chien62dd4532011-12-27 18:09:00 +08002118
2119 Instruction::DecodedInstruction dec_insn(insn);
2120
2121 DCHECK(src_jty == kInt || src_jty == kLong) << src_jty;
2122 DCHECK(dest_jty == kFloat || dest_jty == kDouble) << dest_jty;
2123
2124 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB_, src_jty, kAccurate);
2125 llvm::Type* dest_type = irb_.getJType(dest_jty, kAccurate);
2126 llvm::Value* dest_value = irb_.CreateSIToFP(src_value, dest_type);
2127 EmitStoreDalvikReg(dec_insn.vA_, dest_jty, kAccurate, dest_value);
2128
Logan Chien70f94b42011-12-27 17:49:11 +08002129 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2130}
2131
2132
2133void MethodCompiler::EmitInsn_FPToInt(uint32_t dex_pc,
2134 Instruction const* insn,
2135 JType src_jty,
2136 JType dest_jty) {
Logan Chien12dc1752011-12-27 18:10:15 +08002137
2138 Instruction::DecodedInstruction dec_insn(insn);
2139
2140 DCHECK(src_jty == kFloat || src_jty == kDouble) << src_jty;
2141 DCHECK(dest_jty == kInt || dest_jty == kLong) << dest_jty;
2142
2143 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB_, src_jty, kAccurate);
2144 llvm::Type* dest_type = irb_.getJType(dest_jty, kAccurate);
2145 llvm::Value* dest_value = irb_.CreateFPToSI(src_value, dest_type);
2146 EmitStoreDalvikReg(dec_insn.vA_, dest_jty, kAccurate, dest_value);
2147
Logan Chien70f94b42011-12-27 17:49:11 +08002148 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2149}
2150
2151
2152void MethodCompiler::EmitInsn_FExt(uint32_t dex_pc,
2153 Instruction const* insn) {
Logan Chienc56ded92011-12-27 18:10:57 +08002154
2155 Instruction::DecodedInstruction dec_insn(insn);
2156
2157 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB_, kFloat, kAccurate);
2158 llvm::Value* result_value = irb_.CreateFPExt(src_value, irb_.getJDoubleTy());
2159 EmitStoreDalvikReg(dec_insn.vA_, kDouble, kAccurate, result_value);
2160
Logan Chien70f94b42011-12-27 17:49:11 +08002161 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2162}
2163
2164
2165void MethodCompiler::EmitInsn_FTrunc(uint32_t dex_pc,
2166 Instruction const* insn) {
Logan Chien927744f2011-12-27 18:11:52 +08002167
2168 Instruction::DecodedInstruction dec_insn(insn);
2169
2170 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB_, kDouble, kAccurate);
2171 llvm::Value* result_value = irb_.CreateFPTrunc(src_value, irb_.getJFloatTy());
2172 EmitStoreDalvikReg(dec_insn.vA_, kFloat, kAccurate, result_value);
2173
Logan Chien70f94b42011-12-27 17:49:11 +08002174 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2175}
2176
2177
2178void MethodCompiler::EmitInsn_IntArithm(uint32_t dex_pc,
2179 Instruction const* insn,
2180 IntArithmKind arithm,
2181 JType op_jty,
2182 bool is_2addr) {
Logan Chienc3f7d962011-12-27 18:13:18 +08002183
2184 Instruction::DecodedInstruction dec_insn(insn);
2185
2186 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
2187
2188 llvm::Value* src1_value;
2189 llvm::Value* src2_value;
2190
2191 if (is_2addr) {
2192 src1_value = EmitLoadDalvikReg(dec_insn.vA_, op_jty, kAccurate);
2193 src2_value = EmitLoadDalvikReg(dec_insn.vB_, op_jty, kAccurate);
2194 } else {
2195 src1_value = EmitLoadDalvikReg(dec_insn.vB_, op_jty, kAccurate);
2196 src2_value = EmitLoadDalvikReg(dec_insn.vC_, op_jty, kAccurate);
2197 }
2198
2199 llvm::Value* result_value =
2200 EmitIntArithmResultComputation(dex_pc, src1_value, src2_value,
2201 arithm, op_jty);
2202
2203 EmitStoreDalvikReg(dec_insn.vA_, op_jty, kAccurate, result_value);
2204
Logan Chien70f94b42011-12-27 17:49:11 +08002205 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2206}
2207
2208
2209void MethodCompiler::EmitInsn_IntArithmImmediate(uint32_t dex_pc,
2210 Instruction const* insn,
2211 IntArithmKind arithm) {
Logan Chienc3f7d962011-12-27 18:13:18 +08002212
2213 Instruction::DecodedInstruction dec_insn(insn);
2214
2215 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB_, kInt, kAccurate);
2216
2217 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC_);
2218
2219 llvm::Value* result_value =
2220 EmitIntArithmResultComputation(dex_pc, src_value, imm_value, arithm, kInt);
2221
2222 EmitStoreDalvikReg(dec_insn.vA_, kInt, kAccurate, result_value);
2223
Logan Chien70f94b42011-12-27 17:49:11 +08002224 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2225}
2226
2227
Logan Chienc3f7d962011-12-27 18:13:18 +08002228llvm::Value*
2229MethodCompiler::EmitIntArithmResultComputation(uint32_t dex_pc,
2230 llvm::Value* lhs,
2231 llvm::Value* rhs,
2232 IntArithmKind arithm,
2233 JType op_jty) {
2234 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
2235
2236 switch (arithm) {
2237 case kIntArithm_Add:
2238 return irb_.CreateAdd(lhs, rhs);
2239
2240 case kIntArithm_Sub:
2241 return irb_.CreateSub(lhs, rhs);
2242
2243 case kIntArithm_Mul:
2244 return irb_.CreateMul(lhs, rhs);
2245
2246 case kIntArithm_Div:
2247 EmitGuard_DivZeroException(dex_pc, rhs, op_jty);
2248 return irb_.CreateSDiv(lhs, rhs);
2249
2250 case kIntArithm_Rem:
2251 EmitGuard_DivZeroException(dex_pc, rhs, op_jty);
2252 return irb_.CreateSRem(lhs, rhs);
2253
2254 case kIntArithm_And:
2255 return irb_.CreateAnd(lhs, rhs);
2256
2257 case kIntArithm_Or:
2258 return irb_.CreateOr(lhs, rhs);
2259
2260 case kIntArithm_Xor:
2261 return irb_.CreateXor(lhs, rhs);
2262
2263 case kIntArithm_Shl:
2264 if (op_jty == kLong) {
2265 return irb_.CreateShl(lhs, irb_.CreateAnd(rhs, 0x3f));
2266 } else {
2267 return irb_.CreateShl(lhs, irb_.CreateAnd(rhs, 0x1f));
2268 }
2269
2270 case kIntArithm_Shr:
2271 if (op_jty == kLong) {
2272 return irb_.CreateAShr(lhs, irb_.CreateAnd(rhs, 0x3f));
2273 } else {
2274 return irb_.CreateAShr(lhs, irb_.CreateAnd(rhs, 0x1f));
2275 }
2276
2277 case kIntArithm_UShr:
2278 if (op_jty == kLong) {
2279 return irb_.CreateLShr(lhs, irb_.CreateAnd(rhs, 0x3f));
2280 } else {
2281 return irb_.CreateLShr(lhs, irb_.CreateAnd(rhs, 0x1f));
2282 }
2283
2284 default:
2285 LOG(FATAL) << "Unknown integer arithmetic kind: " << arithm;
2286 return NULL;
2287 }
2288}
2289
2290
Logan Chien70f94b42011-12-27 17:49:11 +08002291void MethodCompiler::EmitInsn_RSubImmediate(uint32_t dex_pc,
2292 Instruction const* insn) {
Logan Chien65c62d42011-12-27 18:14:18 +08002293
2294 Instruction::DecodedInstruction dec_insn(insn);
2295
2296 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB_, kInt, kAccurate);
2297 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC_);
2298 llvm::Value* result_value = irb_.CreateSub(imm_value, src_value);
2299 EmitStoreDalvikReg(dec_insn.vA_, kInt, kAccurate, result_value);
2300
Logan Chien70f94b42011-12-27 17:49:11 +08002301 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2302}
2303
2304
2305void MethodCompiler::EmitInsn_FPArithm(uint32_t dex_pc,
2306 Instruction const* insn,
2307 FPArithmKind arithm,
2308 JType op_jty,
2309 bool is_2addr) {
Logan Chien76e1c792011-12-27 18:15:01 +08002310
2311 Instruction::DecodedInstruction dec_insn(insn);
2312
2313 DCHECK(op_jty == kFloat || op_jty == kDouble) << op_jty;
2314
2315 llvm::Value* src1_value;
2316 llvm::Value* src2_value;
2317
2318 if (is_2addr) {
2319 src1_value = EmitLoadDalvikReg(dec_insn.vA_, op_jty, kAccurate);
2320 src2_value = EmitLoadDalvikReg(dec_insn.vB_, op_jty, kAccurate);
2321 } else {
2322 src1_value = EmitLoadDalvikReg(dec_insn.vB_, op_jty, kAccurate);
2323 src2_value = EmitLoadDalvikReg(dec_insn.vC_, op_jty, kAccurate);
2324 }
2325
2326 llvm::Value* result_value =
2327 EmitFPArithmResultComputation(dex_pc, src1_value, src2_value, arithm);
2328
2329 EmitStoreDalvikReg(dec_insn.vA_, op_jty, kAccurate, result_value);
2330
Logan Chien70f94b42011-12-27 17:49:11 +08002331 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2332}
2333
2334
Logan Chien76e1c792011-12-27 18:15:01 +08002335llvm::Value*
2336MethodCompiler::EmitFPArithmResultComputation(uint32_t dex_pc,
2337 llvm::Value *lhs,
2338 llvm::Value *rhs,
2339 FPArithmKind arithm) {
2340 switch (arithm) {
2341 case kFPArithm_Add:
2342 return irb_.CreateFAdd(lhs, rhs);
2343
2344 case kFPArithm_Sub:
2345 return irb_.CreateFSub(lhs, rhs);
2346
2347 case kFPArithm_Mul:
2348 return irb_.CreateFMul(lhs, rhs);
2349
2350 case kFPArithm_Div:
2351 return irb_.CreateFDiv(lhs, rhs);
2352
2353 case kFPArithm_Rem:
2354 return irb_.CreateFRem(lhs, rhs);
2355
2356 default:
2357 LOG(FATAL) << "Unknown floating-point arithmetic kind: " << arithm;
2358 return NULL;
2359 }
2360}
2361
2362
Logan Chienc3f7d962011-12-27 18:13:18 +08002363void MethodCompiler::EmitGuard_DivZeroException(uint32_t dex_pc,
2364 llvm::Value* denominator,
2365 JType op_jty) {
2366 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
2367
2368 llvm::Constant* zero = irb_.getJZero(op_jty);
2369
2370 llvm::Value* equal_zero = irb_.CreateICmpEQ(denominator, zero);
2371
2372 llvm::BasicBlock* block_exception = CreateBasicBlockWithDexPC(dex_pc, "div0");
2373
2374 llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont");
2375
2376 irb_.CreateCondBr(equal_zero, block_exception, block_continue);
2377
2378 irb_.SetInsertPoint(block_exception);
2379 irb_.CreateCall(irb_.GetRuntime(ThrowDivZeroException));
2380 EmitBranchExceptionLandingPad(dex_pc);
2381
2382 irb_.SetInsertPoint(block_continue);
2383}
2384
2385
Logan Chien61bb6142012-02-03 15:34:53 +08002386void MethodCompiler::EmitGuard_NullPointerException(uint32_t dex_pc,
2387 llvm::Value* object) {
2388 llvm::Value* equal_null = irb_.CreateICmpEQ(object, irb_.getJNull());
2389
2390 llvm::BasicBlock* block_exception =
2391 CreateBasicBlockWithDexPC(dex_pc, "nullp");
2392
2393 llvm::BasicBlock* block_continue =
2394 CreateBasicBlockWithDexPC(dex_pc, "cont");
2395
2396 irb_.CreateCondBr(equal_null, block_exception, block_continue);
2397
2398 irb_.SetInsertPoint(block_exception);
2399 irb_.CreateCall(irb_.GetRuntime(ThrowNullPointerException));
2400 EmitBranchExceptionLandingPad(dex_pc);
2401
2402 irb_.SetInsertPoint(block_continue);
2403}
2404
2405
Logan Chienbb4d12a2012-02-17 14:10:01 +08002406llvm::Value* MethodCompiler::EmitLoadDexCacheAddr(MemberOffset offset) {
2407 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2408
2409 llvm::Value* dex_cache_offset_value =
2410 irb_.getPtrEquivInt(offset.Int32Value());
2411
2412 llvm::Value* dex_cache_field_addr =
2413 irb_.CreatePtrDisp(method_object_addr, dex_cache_offset_value,
2414 irb_.getJObjectTy()->getPointerTo());
2415
2416 return irb_.CreateLoad(dex_cache_field_addr);
2417}
2418
2419
2420void MethodCompiler::
2421EmitLoadDexCacheCodeAndDirectMethodFieldAddr(llvm::Value*& code_field_addr,
2422 llvm::Value*& method_field_addr,
2423 uint32_t method_idx) {
2424 llvm::Value* cadms_dex_cache_addr =
2425 EmitLoadDexCacheAddr(Method::GetDexCacheCodeAndDirectMethodsOffset());
2426
2427 llvm::Value* code_index_value =
2428 irb_.getPtrEquivInt(CodeAndDirectMethods::CodeIndex(method_idx));
2429
2430 llvm::Value* method_index_value =
2431 irb_.getPtrEquivInt(CodeAndDirectMethods::MethodIndex(method_idx));
2432
2433 // Return the field address
2434 code_field_addr = EmitArrayGEP(cadms_dex_cache_addr, code_index_value,
2435 irb_.getJIntTy());
2436
2437 method_field_addr = EmitArrayGEP(cadms_dex_cache_addr, method_index_value,
2438 irb_.getJIntTy());
2439}
2440
2441
2442llvm::Value* MethodCompiler::
2443EmitLoadDexCacheStaticStorageFieldAddr(uint32_t type_idx) {
2444 llvm::Value* static_storage_dex_cache_addr =
2445 EmitLoadDexCacheAddr(Method::DexCacheInitializedStaticStorageOffset());
2446
2447 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
2448
2449 return EmitArrayGEP(static_storage_dex_cache_addr, type_idx_value,
2450 irb_.getJObjectTy());
2451}
2452
2453
2454llvm::Value* MethodCompiler::
2455EmitLoadDexCacheResolvedTypeFieldAddr(uint32_t type_idx) {
2456 llvm::Value* resolved_type_dex_cache_addr =
2457 EmitLoadDexCacheAddr(Method::DexCacheResolvedTypesOffset());
2458
2459 llvm::Value* type_idx_value = irb_.getPtrEquivInt(type_idx);
2460
2461 return EmitArrayGEP(resolved_type_dex_cache_addr, type_idx_value,
2462 irb_.getJObjectTy());
2463}
2464
2465
2466llvm::Value* MethodCompiler::
2467EmitLoadDexCacheStringFieldAddr(uint32_t string_idx) {
2468 llvm::Value* string_dex_cache_addr =
2469 EmitLoadDexCacheAddr(Method::DexCacheStringsOffset());
2470
2471 llvm::Value* string_idx_value = irb_.getPtrEquivInt(string_idx);
2472
2473 return EmitArrayGEP(string_dex_cache_addr, string_idx_value,
2474 irb_.getJObjectTy());
2475}
2476
2477
Logan Chien83426162011-12-09 09:29:50 +08002478CompiledMethod *MethodCompiler::Compile() {
Logan Chien0b827102011-12-20 19:46:14 +08002479 // Code generation
2480 CreateFunction();
2481
2482 EmitPrologue();
2483 EmitInstructions();
Logan Chienc670a8d2011-12-20 21:25:56 +08002484 EmitPrologueLastBranch();
Logan Chien0b827102011-12-20 19:46:14 +08002485
Logan Chiend6c239a2011-12-23 15:11:45 +08002486 // Verify the generated bitcode
2487 llvm::verifyFunction(*func_, llvm::PrintMessageAction);
2488
Logan Chien0b827102011-12-20 19:46:14 +08002489 // Delete the inferred register category map (won't be used anymore)
2490 method_->ResetInferredRegCategoryMap();
2491
2492 return new CompiledMethod(insn_set_, func_);
2493}
2494
2495
2496llvm::Value* MethodCompiler::EmitLoadMethodObjectAddr() {
2497 return func_->arg_begin();
Shih-wei Liaod1fec812012-02-13 09:51:10 -08002498}
Logan Chien83426162011-12-09 09:29:50 +08002499
2500
Logan Chien5bcc04e2012-01-30 14:15:12 +08002501void MethodCompiler::EmitBranchExceptionLandingPad(uint32_t dex_pc) {
2502 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
2503 irb_.CreateBr(lpad);
2504 } else {
2505 irb_.CreateBr(GetUnwindBasicBlock());
2506 }
2507}
2508
2509
2510void MethodCompiler::EmitGuard_ExceptionLandingPad(uint32_t dex_pc) {
2511 llvm::Value* exception_pending =
2512 irb_.CreateCall(irb_.GetRuntime(IsExceptionPending));
2513
2514 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
2515
2516 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
2517 irb_.CreateCondBr(exception_pending, lpad, block_cont);
2518 } else {
2519 irb_.CreateCondBr(exception_pending, GetUnwindBasicBlock(), block_cont);
2520 }
2521
2522 irb_.SetInsertPoint(block_cont);
2523}
2524
2525
Logan Chien924072f2012-01-30 15:07:24 +08002526void MethodCompiler::EmitGuard_GarbageCollectionSuspend(uint32_t dex_pc) {
2527 llvm::Value* runtime_func = irb_.GetRuntime(TestSuspend);
2528 irb_.CreateCall(runtime_func);
2529
2530 EmitGuard_ExceptionLandingPad(dex_pc);
2531}
2532
2533
Logan Chiend6c239a2011-12-23 15:11:45 +08002534llvm::BasicBlock* MethodCompiler::
2535CreateBasicBlockWithDexPC(uint32_t dex_pc, char const* postfix) {
2536 std::string name;
2537
2538 if (postfix) {
2539 StringAppendF(&name, "B%u.%s", dex_pc, postfix);
2540 } else {
2541 StringAppendF(&name, "B%u", dex_pc);
2542 }
2543
2544 return llvm::BasicBlock::Create(*context_, name, func_);
2545}
2546
2547
2548llvm::BasicBlock* MethodCompiler::GetBasicBlock(uint32_t dex_pc) {
2549 DCHECK(dex_pc < code_item_->insns_size_in_code_units_);
2550
2551 llvm::BasicBlock* basic_block = basic_blocks_[dex_pc];
2552
2553 if (!basic_block) {
2554 basic_block = CreateBasicBlockWithDexPC(dex_pc);
2555 basic_blocks_[dex_pc] = basic_block;
2556 }
2557
2558 return basic_block;
2559}
2560
2561
2562llvm::BasicBlock*
2563MethodCompiler::GetNextBasicBlock(uint32_t dex_pc) {
2564 Instruction const* insn = Instruction::At(code_item_->insns_ + dex_pc);
2565 return GetBasicBlock(dex_pc + insn->SizeInCodeUnits());
2566}
2567
2568
Logan Chien5bcc04e2012-01-30 14:15:12 +08002569int32_t MethodCompiler::GetTryItemOffset(uint32_t dex_pc) {
2570 // TODO: Since we are emitting the dex instructions in ascending order
2571 // w.r.t. address, we can cache the lastest try item offset so that we
2572 // don't have to do binary search for every query.
2573
2574 int32_t min = 0;
2575 int32_t max = code_item_->tries_size_ - 1;
2576
2577 while (min <= max) {
2578 int32_t mid = min + (max - min) / 2;
2579
2580 DexFile::TryItem const* ti = DexFile::GetTryItems(*code_item_, mid);
2581 uint32_t start = ti->start_addr_;
2582 uint32_t end = start + ti->insn_count_;
2583
2584 if (dex_pc < start) {
2585 max = mid - 1;
2586 } else if (dex_pc >= end) {
2587 min = mid + 1;
2588 } else {
2589 return mid; // found
2590 }
2591 }
2592
2593 return -1; // not found
2594}
2595
2596
2597llvm::BasicBlock* MethodCompiler::GetLandingPadBasicBlock(uint32_t dex_pc) {
2598 // Find the try item for this address in this method
2599 int32_t ti_offset = GetTryItemOffset(dex_pc);
2600
2601 if (ti_offset == -1) {
2602 return NULL; // No landing pad is available for this address.
2603 }
2604
2605 // Check for the existing landing pad basic block
2606 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
2607 llvm::BasicBlock* block_lpad = basic_block_landing_pads_[ti_offset];
2608
2609 if (block_lpad) {
2610 // We have generated landing pad for this try item already. Return the
2611 // same basic block.
2612 return block_lpad;
2613 }
2614
2615 // Get try item from code item
2616 DexFile::TryItem const* ti = DexFile::GetTryItems(*code_item_, ti_offset);
2617
2618 // Create landing pad basic block
2619 block_lpad = llvm::BasicBlock::Create(*context_,
2620 StringPrintf("lpad%d", ti_offset),
2621 func_);
2622
2623 // Change IRBuilder insert point
2624 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
2625 irb_.SetInsertPoint(block_lpad);
2626
2627 // Find catch block with matching type
2628 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2629
2630 // TODO: Maybe passing try item offset will be a better idea? For now,
2631 // we are passing dex_pc, so that we can use existing runtime support
2632 // function directly. However, in the runtime supporting function we
2633 // have to search for try item with binary search which can be
2634 // eliminated.
2635 llvm::Value* dex_pc_value = irb_.getInt32(ti->start_addr_);
2636
2637 llvm::Value* catch_handler_index_value =
2638 irb_.CreateCall2(irb_.GetRuntime(FindCatchBlock),
2639 method_object_addr, dex_pc_value);
2640
2641 // Switch instruction (Go to unwind basic block by default)
2642 llvm::SwitchInst* sw =
2643 irb_.CreateSwitch(catch_handler_index_value, GetUnwindBasicBlock());
2644
2645 // Cases with matched catch block
2646 CatchHandlerIterator iter(*code_item_, ti->start_addr_);
2647
2648 for (uint32_t c = 0; iter.HasNext(); iter.Next(), ++c) {
2649 sw->addCase(irb_.getInt32(c), GetBasicBlock(iter.GetHandlerAddress()));
2650 }
2651
2652 // Restore the orignal insert point for IRBuilder
2653 irb_.restoreIP(irb_ip_original);
2654
2655 // Cache this landing pad
2656 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
2657 basic_block_landing_pads_[ti_offset] = block_lpad;
2658
2659 return block_lpad;
2660}
2661
2662
2663llvm::BasicBlock* MethodCompiler::GetUnwindBasicBlock() {
2664 // Check the existing unwinding baisc block block
2665 if (basic_block_unwind_ != NULL) {
2666 return basic_block_unwind_;
2667 }
2668
2669 // Create new basic block for unwinding
2670 basic_block_unwind_ =
2671 llvm::BasicBlock::Create(*context_, "exception_unwind", func_);
2672
2673 // Change IRBuilder insert point
2674 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
2675 irb_.SetInsertPoint(basic_block_unwind_);
2676
2677 // Emit the code to return default value (zero) for the given return type.
2678 char ret_shorty = method_helper_.GetShorty()[0];
2679 if (ret_shorty == 'V') {
2680 irb_.CreateRetVoid();
2681 } else {
2682 irb_.CreateRet(irb_.getJZero(ret_shorty));
2683 }
2684
2685 // Restore the orignal insert point for IRBuilder
2686 irb_.restoreIP(irb_ip_original);
2687
2688 return basic_block_unwind_;
2689}
2690
2691
Logan Chienc670a8d2011-12-20 21:25:56 +08002692llvm::Value* MethodCompiler::AllocDalvikLocalVarReg(RegCategory cat,
2693 uint32_t reg_idx) {
2694
2695 // Save current IR builder insert point
2696 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
2697
2698 // Alloca
2699 llvm::Value* reg_addr = NULL;
2700
2701 switch (cat) {
2702 case kRegCat1nr:
2703 irb_.SetInsertPoint(basic_block_reg_alloca_);
2704 reg_addr = irb_.CreateAlloca(irb_.getJIntTy(), 0,
2705 StringPrintf("r%u", reg_idx));
2706
2707 irb_.SetInsertPoint(basic_block_reg_zero_init_);
2708 irb_.CreateStore(irb_.getJInt(0), reg_addr);
2709 break;
2710
2711 case kRegCat2:
2712 irb_.SetInsertPoint(basic_block_reg_alloca_);
2713 reg_addr = irb_.CreateAlloca(irb_.getJLongTy(), 0,
2714 StringPrintf("w%u", reg_idx));
2715
2716 irb_.SetInsertPoint(basic_block_reg_zero_init_);
2717 irb_.CreateStore(irb_.getJLong(0), reg_addr);
2718 break;
2719
2720 case kRegObject:
2721 irb_.SetInsertPoint(basic_block_reg_alloca_);
2722 reg_addr = irb_.CreateAlloca(irb_.getJObjectTy(), 0,
2723 StringPrintf("p%u", reg_idx));
2724
2725 irb_.SetInsertPoint(basic_block_reg_zero_init_);
2726 irb_.CreateStore(irb_.getJNull(), reg_addr);
2727 break;
2728
2729 default:
2730 LOG(FATAL) << "Unknown register category for allocation: " << cat;
2731 }
2732
2733 // Restore IRBuilder insert point
2734 irb_.restoreIP(irb_ip_original);
2735
2736 DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL));
2737 return reg_addr;
2738}
2739
2740
2741llvm::Value* MethodCompiler::AllocDalvikRetValReg(RegCategory cat) {
2742 // Save current IR builder insert point
2743 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
2744
2745 // Alloca
2746 llvm::Value* reg_addr = NULL;
2747
2748 switch (cat) {
2749 case kRegCat1nr:
2750 irb_.SetInsertPoint(basic_block_reg_alloca_);
2751 reg_addr = irb_.CreateAlloca(irb_.getJIntTy(), 0, "r_res");
2752 break;
2753
2754 case kRegCat2:
2755 irb_.SetInsertPoint(basic_block_reg_alloca_);
2756 reg_addr = irb_.CreateAlloca(irb_.getJLongTy(), 0, "w_res");
2757 break;
2758
2759 case kRegObject:
2760 irb_.SetInsertPoint(basic_block_reg_alloca_);
2761 reg_addr = irb_.CreateAlloca(irb_.getJObjectTy(), 0, "p_res");
2762 break;
2763
2764 default:
2765 LOG(FATAL) << "Unknown register category for allocation: " << cat;
2766 }
2767
2768 // Restore IRBuilder insert point
2769 irb_.restoreIP(irb_ip_original);
2770
2771 DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL));
2772 return reg_addr;
2773}
2774
2775
Logan Chien83426162011-12-09 09:29:50 +08002776} // namespace compiler_llvm
2777} // namespace art