blob: 596ae876e3101708ab333a36ac36c555d4ecadf0 [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) {
1111 // UNIMPLEMENTED(WARNING);
1112 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1113}
1114
1115
1116void MethodCompiler::EmitInsn_ThrowException(uint32_t dex_pc,
1117 Instruction const* insn) {
1118 // UNIMPLEMENTED(WARNING);
1119 irb_.CreateUnreachable();
1120}
1121
1122
1123void MethodCompiler::EmitInsn_ReturnVoid(uint32_t dex_pc,
1124 Instruction const* insn) {
Logan Chien8898a272011-12-27 17:51:56 +08001125 // Garbage collection safe-point
1126 EmitGuard_GarbageCollectionSuspend(dex_pc);
1127
1128 // Return!
1129 irb_.CreateRetVoid();
Logan Chien70f94b42011-12-27 17:49:11 +08001130}
1131
1132
1133void MethodCompiler::EmitInsn_Return(uint32_t dex_pc,
1134 Instruction const* insn) {
Logan Chien8898a272011-12-27 17:51:56 +08001135
1136 Instruction::DecodedInstruction dec_insn(insn);
1137
1138 // Garbage collection safe-point
1139 EmitGuard_GarbageCollectionSuspend(dex_pc);
1140
1141 // Return!
1142 char ret_shorty = method_helper_.GetShorty()[0];
1143 llvm::Value* retval = EmitLoadDalvikReg(dec_insn.vA_, ret_shorty, kAccurate);
1144
1145 irb_.CreateRet(retval);
Logan Chien70f94b42011-12-27 17:49:11 +08001146}
1147
1148
1149void MethodCompiler::EmitInsn_LoadConstant(uint32_t dex_pc,
1150 Instruction const* insn,
1151 JType imm_jty) {
Shih-wei Liao798366e2012-02-16 09:25:33 -08001152
1153 Instruction::DecodedInstruction dec_insn(insn);
1154
1155 DCHECK(imm_jty == kInt || imm_jty == kLong) << imm_jty;
1156
1157 int64_t imm = 0;
1158
1159 switch (insn->Opcode()) {
1160 // 32-bit Immediate
1161 case Instruction::CONST_4:
1162 case Instruction::CONST_16:
1163 case Instruction::CONST:
1164 case Instruction::CONST_WIDE_16:
1165 case Instruction::CONST_WIDE_32:
1166 imm = static_cast<int64_t>(static_cast<int32_t>(dec_insn.vB_));
1167 break;
1168
1169 case Instruction::CONST_HIGH16:
1170 imm = static_cast<int64_t>(static_cast<int32_t>(
1171 static_cast<uint32_t>(static_cast<uint16_t>(dec_insn.vB_)) << 16));
1172 break;
1173
1174 // 64-bit Immediate
1175 case Instruction::CONST_WIDE:
1176 imm = static_cast<int64_t>(dec_insn.vB_wide_);
1177 break;
1178
1179 case Instruction::CONST_WIDE_HIGH16:
1180 imm = static_cast<int64_t>(
1181 static_cast<uint64_t>(static_cast<uint16_t>(dec_insn.vB_)) << 48);
1182 break;
1183
1184 // Unknown opcode for load constant (unreachable)
1185 default:
1186 LOG(FATAL) << "Unknown opcode for load constant: " << insn->Opcode();
1187 break;
1188 }
1189
1190 // Store the non-object register
1191 llvm::Type* imm_type = irb_.getJType(imm_jty, kAccurate);
1192 llvm::Constant* imm_value = llvm::ConstantInt::getSigned(imm_type, imm);
1193 EmitStoreDalvikReg(dec_insn.vA_, imm_jty, kAccurate, imm_value);
1194
1195 // Store the object register if it is possible to be null.
1196 if (imm_jty == kInt && imm == 0) {
1197 EmitStoreDalvikReg(dec_insn.vA_, kObject, kAccurate, irb_.getJNull());
1198 }
1199
Logan Chien70f94b42011-12-27 17:49:11 +08001200 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1201}
1202
1203
1204void MethodCompiler::EmitInsn_LoadConstantString(uint32_t dex_pc,
1205 Instruction const* insn) {
1206 // UNIMPLEMENTED(WARNING);
1207 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1208}
1209
1210
1211void MethodCompiler::EmitInsn_LoadConstantClass(uint32_t dex_pc,
1212 Instruction const* insn) {
1213 // UNIMPLEMENTED(WARNING);
1214 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1215}
1216
1217
1218void MethodCompiler::EmitInsn_MonitorEnter(uint32_t dex_pc,
1219 Instruction const* insn) {
1220 // UNIMPLEMENTED(WARNING);
1221 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1222}
1223
1224
1225void MethodCompiler::EmitInsn_MonitorExit(uint32_t dex_pc,
1226 Instruction const* insn) {
1227 // UNIMPLEMENTED(WARNING);
1228 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1229}
1230
1231
1232void MethodCompiler::EmitInsn_CheckCast(uint32_t dex_pc,
1233 Instruction const* insn) {
1234 // UNIMPLEMENTED(WARNING);
1235 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1236}
1237
1238
1239void MethodCompiler::EmitInsn_InstanceOf(uint32_t dex_pc,
1240 Instruction const* insn) {
1241 // UNIMPLEMENTED(WARNING);
1242 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1243}
1244
1245
Logan Chien61bb6142012-02-03 15:34:53 +08001246llvm::Value* MethodCompiler::EmitLoadArrayLength(llvm::Value* array) {
1247 // Load array length field address
1248 llvm::Constant* array_len_field_offset =
1249 irb_.getPtrEquivInt(Array::LengthOffset().Int32Value());
1250
1251 llvm::Value* array_len_field_addr =
1252 irb_.CreatePtrDisp(array, array_len_field_offset,
1253 irb_.getJIntTy()->getPointerTo());
1254
1255 // Load array length
1256 return irb_.CreateLoad(array_len_field_addr);
1257}
1258
1259
Logan Chien70f94b42011-12-27 17:49:11 +08001260void MethodCompiler::EmitInsn_ArrayLength(uint32_t dex_pc,
1261 Instruction const* insn) {
Logan Chien61bb6142012-02-03 15:34:53 +08001262
1263 Instruction::DecodedInstruction dec_insn(insn);
1264
1265 // Get the array object address
1266 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB_, kObject, kAccurate);
1267 EmitGuard_NullPointerException(dex_pc, array_addr);
1268
1269 // Get the array length and store it to the register
1270 llvm::Value* array_len = EmitLoadArrayLength(array_addr);
1271 EmitStoreDalvikReg(dec_insn.vA_, kInt, kAccurate, array_len);
1272
Logan Chien70f94b42011-12-27 17:49:11 +08001273 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1274}
1275
1276
1277void MethodCompiler::EmitInsn_NewInstance(uint32_t dex_pc,
1278 Instruction const* insn) {
1279 // UNIMPLEMENTED(WARNING);
1280 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1281}
1282
1283
1284void MethodCompiler::EmitInsn_NewArray(uint32_t dex_pc,
1285 Instruction const* insn) {
1286 // UNIMPLEMENTED(WARNING);
1287 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1288}
1289
1290
1291void MethodCompiler::EmitInsn_FilledNewArray(uint32_t dex_pc,
1292 Instruction const* insn,
1293 bool is_range) {
1294 // UNIMPLEMENTED(WARNING);
1295 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1296}
1297
1298
1299void MethodCompiler::EmitInsn_FillArrayData(uint32_t dex_pc,
1300 Instruction const* insn) {
1301 // UNIMPLEMENTED(WARNING);
1302 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1303}
1304
1305
1306void MethodCompiler::EmitInsn_UnconditionalBranch(uint32_t dex_pc,
1307 Instruction const* insn) {
Logan Chiena466c162011-12-27 17:55:46 +08001308
1309 Instruction::DecodedInstruction dec_insn(insn);
1310
1311 int32_t branch_offset = dec_insn.vA_;
1312
1313 if (branch_offset <= 0) {
1314 // Garbage collection safe-point on backward branch
1315 EmitGuard_GarbageCollectionSuspend(dex_pc);
1316 }
1317
1318 irb_.CreateBr(GetBasicBlock(dex_pc + branch_offset));
Logan Chien70f94b42011-12-27 17:49:11 +08001319}
1320
1321
1322void MethodCompiler::EmitInsn_PackedSwitch(uint32_t dex_pc,
1323 Instruction const* insn) {
Logan Chien7a89b6d2011-12-27 17:56:56 +08001324
1325 Instruction::DecodedInstruction dec_insn(insn);
1326
1327 struct PACKED Payload {
1328 uint16_t ident_;
1329 uint16_t num_cases_;
1330 int32_t first_key_;
1331 int32_t targets_[];
1332 };
1333
1334 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
1335 static_cast<int32_t>(dec_insn.vB_);
1336
1337 Payload const* payload =
1338 reinterpret_cast<Payload const*>(code_item_->insns_ + payload_offset);
1339
1340 llvm::Value* value = EmitLoadDalvikReg(dec_insn.vA_, kInt, kAccurate);
1341
1342 llvm::SwitchInst* sw =
1343 irb_.CreateSwitch(value, GetNextBasicBlock(dex_pc), payload->num_cases_);
1344
1345 for (uint16_t i = 0; i < payload->num_cases_; ++i) {
1346 sw->addCase(irb_.getInt32(payload->first_key_ + i),
1347 GetBasicBlock(dex_pc + payload->targets_[i]));
1348 }
Logan Chien70f94b42011-12-27 17:49:11 +08001349}
1350
1351
1352void MethodCompiler::EmitInsn_SparseSwitch(uint32_t dex_pc,
1353 Instruction const* insn) {
Logan Chien7a89b6d2011-12-27 17:56:56 +08001354
1355 Instruction::DecodedInstruction dec_insn(insn);
1356
1357 struct PACKED Payload {
1358 uint16_t ident_;
1359 uint16_t num_cases_;
1360 int32_t keys_and_targets_[];
1361 };
1362
1363 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
1364 static_cast<int32_t>(dec_insn.vB_);
1365
1366 Payload const* payload =
1367 reinterpret_cast<Payload const*>(code_item_->insns_ + payload_offset);
1368
1369 int32_t const* keys = payload->keys_and_targets_;
1370 int32_t const* targets = payload->keys_and_targets_ + payload->num_cases_;
1371
1372 llvm::Value* value = EmitLoadDalvikReg(dec_insn.vA_, kInt, kAccurate);
1373
1374 llvm::SwitchInst* sw =
1375 irb_.CreateSwitch(value, GetNextBasicBlock(dex_pc), payload->num_cases_);
1376
1377 for (size_t i = 0; i < payload->num_cases_; ++i) {
1378 sw->addCase(irb_.getInt32(keys[i]), GetBasicBlock(dex_pc + targets[i]));
1379 }
Logan Chien70f94b42011-12-27 17:49:11 +08001380}
1381
1382
1383void MethodCompiler::EmitInsn_FPCompare(uint32_t dex_pc,
1384 Instruction const* insn,
1385 JType fp_jty,
1386 bool gt_bias) {
Logan Chien2c37e8e2011-12-27 17:58:46 +08001387
1388 Instruction::DecodedInstruction dec_insn(insn);
1389
1390 DCHECK(fp_jty == kFloat || fp_jty == kDouble) << "JType: " << fp_jty;
1391
1392 llvm::Value* src1_value = EmitLoadDalvikReg(dec_insn.vB_, fp_jty, kAccurate);
1393 llvm::Value* src2_value = EmitLoadDalvikReg(dec_insn.vC_, fp_jty, kAccurate);
1394
1395 llvm::Value* cmp_eq = irb_.CreateFCmpOEQ(src1_value, src2_value);
1396 llvm::Value* cmp_lt;
1397
1398 if (gt_bias) {
1399 cmp_lt = irb_.CreateFCmpOLT(src1_value, src2_value);
1400 } else {
1401 cmp_lt = irb_.CreateFCmpULT(src1_value, src2_value);
1402 }
1403
1404 llvm::Value* result = EmitCompareResultSelection(cmp_eq, cmp_lt);
1405 EmitStoreDalvikReg(dec_insn.vA_, kInt, kAccurate, result);
1406
Logan Chien70f94b42011-12-27 17:49:11 +08001407 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1408}
1409
1410
1411void MethodCompiler::EmitInsn_LongCompare(uint32_t dex_pc,
1412 Instruction const* insn) {
Logan Chien2c37e8e2011-12-27 17:58:46 +08001413
1414 Instruction::DecodedInstruction dec_insn(insn);
1415
1416 llvm::Value* src1_value = EmitLoadDalvikReg(dec_insn.vB_, kLong, kAccurate);
1417 llvm::Value* src2_value = EmitLoadDalvikReg(dec_insn.vC_, kLong, kAccurate);
1418
1419 llvm::Value* cmp_eq = irb_.CreateICmpEQ(src1_value, src2_value);
1420 llvm::Value* cmp_lt = irb_.CreateICmpSLT(src1_value, src2_value);
1421
1422 llvm::Value* result = EmitCompareResultSelection(cmp_eq, cmp_lt);
1423 EmitStoreDalvikReg(dec_insn.vA_, kInt, kAccurate, result);
1424
Logan Chien70f94b42011-12-27 17:49:11 +08001425 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1426}
1427
1428
Logan Chien2c37e8e2011-12-27 17:58:46 +08001429llvm::Value* MethodCompiler::EmitCompareResultSelection(llvm::Value* cmp_eq,
1430 llvm::Value* cmp_lt) {
1431
1432 llvm::Constant* zero = irb_.getJInt(0);
1433 llvm::Constant* pos1 = irb_.getJInt(1);
1434 llvm::Constant* neg1 = irb_.getJInt(-1);
1435
1436 llvm::Value* result_lt = irb_.CreateSelect(cmp_lt, neg1, pos1);
1437 llvm::Value* result_eq = irb_.CreateSelect(cmp_eq, zero, result_lt);
1438
1439 return result_eq;
1440}
1441
1442
Logan Chien70f94b42011-12-27 17:49:11 +08001443void MethodCompiler::EmitInsn_BinaryConditionalBranch(uint32_t dex_pc,
1444 Instruction const* insn,
1445 CondBranchKind cond) {
Logan Chiena78e3c82011-12-27 17:59:35 +08001446
1447 Instruction::DecodedInstruction dec_insn(insn);
1448
1449 int8_t src1_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vA_);
1450 int8_t src2_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vB_);
1451
1452 DCHECK_NE(kRegUnknown, src1_reg_cat);
1453 DCHECK_NE(kRegUnknown, src2_reg_cat);
1454 DCHECK_NE(kRegCat2, src1_reg_cat);
1455 DCHECK_NE(kRegCat2, src2_reg_cat);
1456
1457 int32_t branch_offset = dec_insn.vC_;
1458
1459 if (branch_offset <= 0) {
1460 // Garbage collection safe-point on backward branch
1461 EmitGuard_GarbageCollectionSuspend(dex_pc);
1462 }
1463
1464 if (src1_reg_cat == kRegZero && src2_reg_cat == kRegZero) {
1465 irb_.CreateBr(GetBasicBlock(dex_pc + branch_offset));
1466 return;
1467 }
1468
1469 llvm::Value* src1_value;
1470 llvm::Value* src2_value;
1471
1472 if (src1_reg_cat != kRegZero && src2_reg_cat != kRegZero) {
1473 CHECK_EQ(src1_reg_cat, src2_reg_cat);
1474
1475 if (src1_reg_cat == kRegCat1nr) {
1476 src1_value = EmitLoadDalvikReg(dec_insn.vA_, kInt, kAccurate);
1477 src2_value = EmitLoadDalvikReg(dec_insn.vB_, kInt, kAccurate);
1478 } else {
1479 src1_value = EmitLoadDalvikReg(dec_insn.vA_, kObject, kAccurate);
1480 src2_value = EmitLoadDalvikReg(dec_insn.vB_, kObject, kAccurate);
1481 }
1482 } else {
1483 DCHECK(src1_reg_cat == kRegZero ||
1484 src2_reg_cat == kRegZero);
1485
1486 if (src1_reg_cat == kRegZero) {
1487 if (src2_reg_cat == kRegCat1nr) {
1488 src1_value = irb_.getJInt(0);
1489 src2_value = EmitLoadDalvikReg(dec_insn.vA_, kInt, kAccurate);
1490 } else {
1491 src1_value = irb_.getJNull();
1492 src2_value = EmitLoadDalvikReg(dec_insn.vA_, kObject, kAccurate);
1493 }
1494 } else { // src2_reg_cat == kRegZero
1495 if (src2_reg_cat == kRegCat1nr) {
1496 src1_value = EmitLoadDalvikReg(dec_insn.vA_, kInt, kAccurate);
1497 src2_value = irb_.getJInt(0);
1498 } else {
1499 src1_value = EmitLoadDalvikReg(dec_insn.vA_, kObject, kAccurate);
1500 src2_value = irb_.getJNull();
1501 }
1502 }
1503 }
1504
1505 llvm::Value* cond_value =
1506 EmitConditionResult(src1_value, src2_value, cond);
1507
1508 irb_.CreateCondBr(cond_value,
1509 GetBasicBlock(dex_pc + branch_offset),
1510 GetNextBasicBlock(dex_pc));
Logan Chien70f94b42011-12-27 17:49:11 +08001511}
1512
1513
1514void MethodCompiler::EmitInsn_UnaryConditionalBranch(uint32_t dex_pc,
1515 Instruction const* insn,
1516 CondBranchKind cond) {
Logan Chiena78e3c82011-12-27 17:59:35 +08001517
1518 Instruction::DecodedInstruction dec_insn(insn);
1519
1520 int8_t src_reg_cat = GetInferredRegCategory(dex_pc, dec_insn.vA_);
1521
1522 DCHECK_NE(kRegUnknown, src_reg_cat);
1523 DCHECK_NE(kRegCat2, src_reg_cat);
1524
1525 int32_t branch_offset = dec_insn.vB_;
1526
1527 if (branch_offset <= 0) {
1528 // Garbage collection safe-point on backward branch
1529 EmitGuard_GarbageCollectionSuspend(dex_pc);
1530 }
1531
1532 if (src_reg_cat == kRegZero) {
1533 irb_.CreateBr(GetBasicBlock(dex_pc + branch_offset));
1534 return;
1535 }
1536
1537 llvm::Value* src1_value;
1538 llvm::Value* src2_value;
1539
1540 if (src_reg_cat == kRegCat1nr) {
1541 src1_value = EmitLoadDalvikReg(dec_insn.vA_, kInt, kAccurate);
1542 src2_value = irb_.getInt32(0);
1543 } else {
1544 src1_value = EmitLoadDalvikReg(dec_insn.vA_, kObject, kAccurate);
1545 src2_value = irb_.getJNull();
1546 }
1547
1548 llvm::Value* cond_value =
1549 EmitConditionResult(src1_value, src2_value, cond);
1550
1551 irb_.CreateCondBr(cond_value,
1552 GetBasicBlock(dex_pc + branch_offset),
1553 GetNextBasicBlock(dex_pc));
1554}
1555
1556
1557RegCategory MethodCompiler::GetInferredRegCategory(uint32_t dex_pc,
1558 uint16_t reg_idx) {
1559 InferredRegCategoryMap const* map = method_->GetInferredRegCategoryMap();
1560 CHECK_NE(map, static_cast<InferredRegCategoryMap*>(NULL));
1561
1562 return map->GetRegCategory(dex_pc, reg_idx);
1563}
1564
1565
1566llvm::Value* MethodCompiler::EmitConditionResult(llvm::Value* lhs,
1567 llvm::Value* rhs,
1568 CondBranchKind cond) {
1569 switch (cond) {
1570 case kCondBranch_EQ:
1571 return irb_.CreateICmpEQ(lhs, rhs);
1572
1573 case kCondBranch_NE:
1574 return irb_.CreateICmpNE(lhs, rhs);
1575
1576 case kCondBranch_LT:
1577 return irb_.CreateICmpSLT(lhs, rhs);
1578
1579 case kCondBranch_GE:
1580 return irb_.CreateICmpSGE(lhs, rhs);
1581
1582 case kCondBranch_GT:
1583 return irb_.CreateICmpSGT(lhs, rhs);
1584
1585 case kCondBranch_LE:
1586 return irb_.CreateICmpSLE(lhs, rhs);
1587
1588 default: // Unreachable
1589 LOG(FATAL) << "Unknown conditional branch kind: " << cond;
1590 return NULL;
1591 }
Logan Chien70f94b42011-12-27 17:49:11 +08001592}
1593
1594
Logan Chiene27fdbb2012-01-02 23:27:26 +08001595void
1596MethodCompiler::EmitGuard_ArrayIndexOutOfBoundsException(uint32_t dex_pc,
1597 llvm::Value* array,
1598 llvm::Value* index) {
1599 llvm::Value* array_len = EmitLoadArrayLength(array);
1600
1601 llvm::Value* cmp = irb_.CreateICmpUGE(index, array_len);
1602
1603 llvm::BasicBlock* block_exception =
1604 CreateBasicBlockWithDexPC(dex_pc, "overflow");
1605
1606 llvm::BasicBlock* block_continue =
1607 CreateBasicBlockWithDexPC(dex_pc, "cont");
1608
1609 irb_.CreateCondBr(cmp, block_exception, block_continue);
1610
1611 irb_.SetInsertPoint(block_exception);
1612 irb_.CreateCall2(irb_.GetRuntime(ThrowIndexOutOfBounds), index, array_len);
1613 EmitBranchExceptionLandingPad(dex_pc);
1614
1615 irb_.SetInsertPoint(block_continue);
1616}
1617
1618
1619void MethodCompiler::EmitGuard_ArrayException(uint32_t dex_pc,
1620 llvm::Value* array,
1621 llvm::Value* index) {
1622 EmitGuard_NullPointerException(dex_pc, array);
1623 EmitGuard_ArrayIndexOutOfBoundsException(dex_pc, array, index);
1624}
1625
1626
1627// Emit Array GetElementPtr
1628llvm::Value* MethodCompiler::EmitArrayGEP(llvm::Value* array_addr,
1629 llvm::Value* index_value,
1630 llvm::Type* elem_type) {
1631
1632 llvm::Constant* data_offset_value =
1633 irb_.getPtrEquivInt(Array::DataOffset().Int32Value());
1634
1635 llvm::Value* array_data_addr =
1636 irb_.CreatePtrDisp(array_addr, data_offset_value,
1637 elem_type->getPointerTo());
1638
1639 return irb_.CreateGEP(array_data_addr, index_value);
1640}
1641
1642
Logan Chien70f94b42011-12-27 17:49:11 +08001643void MethodCompiler::EmitInsn_AGet(uint32_t dex_pc,
1644 Instruction const* insn,
1645 JType elem_jty) {
Logan Chiene27fdbb2012-01-02 23:27:26 +08001646
1647 Instruction::DecodedInstruction dec_insn(insn);
1648
1649 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB_, kObject, kAccurate);
1650 llvm::Value* index_value = EmitLoadDalvikReg(dec_insn.vC_, kInt, kAccurate);
1651
1652 EmitGuard_ArrayException(dex_pc, array_addr, index_value);
1653
1654 llvm::Type* elem_type = irb_.getJType(elem_jty, kArray);
1655
1656 llvm::Value* array_elem_addr =
1657 EmitArrayGEP(array_addr, index_value, elem_type);
1658
1659 llvm::Value* array_elem_value = irb_.CreateLoad(array_elem_addr);
1660
1661 EmitStoreDalvikReg(dec_insn.vA_, elem_jty, kArray, array_elem_value);
1662
Logan Chien70f94b42011-12-27 17:49:11 +08001663 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1664}
1665
1666
1667void MethodCompiler::EmitInsn_APut(uint32_t dex_pc,
1668 Instruction const* insn,
1669 JType elem_jty) {
Logan Chien8dabb432012-01-02 23:29:32 +08001670
1671 Instruction::DecodedInstruction dec_insn(insn);
1672
1673 llvm::Value* array_addr = EmitLoadDalvikReg(dec_insn.vB_, kObject, kAccurate);
1674 llvm::Value* index_value = EmitLoadDalvikReg(dec_insn.vC_, kInt, kAccurate);
1675
1676 EmitGuard_ArrayException(dex_pc, array_addr, index_value);
1677
1678 llvm::Type* elem_type = irb_.getJType(elem_jty, kArray);
1679
1680 llvm::Value* array_elem_addr =
1681 EmitArrayGEP(array_addr, index_value, elem_type);
1682
1683 llvm::Value* new_value = EmitLoadDalvikReg(dec_insn.vA_, elem_jty, kArray);
1684
1685 irb_.CreateStore(new_value, array_elem_addr);
1686
Logan Chien70f94b42011-12-27 17:49:11 +08001687 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1688}
1689
1690
1691void MethodCompiler::EmitInsn_IGet(uint32_t dex_pc,
1692 Instruction const* insn,
1693 JType field_jty) {
1694 // UNIMPLEMENTED(WARNING);
1695 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1696}
1697
1698
1699void MethodCompiler::EmitInsn_IPut(uint32_t dex_pc,
1700 Instruction const* insn,
1701 JType field_jty) {
1702 // UNIMPLEMENTED(WARNING);
1703 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1704}
1705
1706
1707void MethodCompiler::EmitInsn_SGet(uint32_t dex_pc,
1708 Instruction const* insn,
1709 JType field_jty) {
1710 // UNIMPLEMENTED(WARNING);
1711 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1712}
1713
1714
1715void MethodCompiler::EmitInsn_SPut(uint32_t dex_pc,
1716 Instruction const* insn,
1717 JType field_jty) {
1718 // UNIMPLEMENTED(WARNING);
1719 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1720}
1721
1722
1723void MethodCompiler::EmitInsn_InvokeVirtual(uint32_t dex_pc,
1724 Instruction const* insn,
1725 bool is_range) {
1726 // UNIMPLEMENTED(WARNING);
1727 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1728}
1729
1730
1731void MethodCompiler::EmitInsn_InvokeSuper(uint32_t dex_pc,
1732 Instruction const* insn,
1733 bool is_range) {
1734 // UNIMPLEMENTED(WARNING);
1735 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1736}
1737
1738
1739void MethodCompiler::EmitInsn_InvokeDirect(uint32_t dex_pc,
1740 Instruction const* insn,
1741 bool is_range) {
1742 // UNIMPLEMENTED(WARNING);
1743 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1744}
1745
1746
1747void MethodCompiler::EmitInsn_InvokeStatic(uint32_t dex_pc,
1748 Instruction const* insn,
1749 bool is_range) {
1750 // UNIMPLEMENTED(WARNING);
1751 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1752}
1753
1754
1755void MethodCompiler::EmitInsn_InvokeInterface(uint32_t dex_pc,
1756 Instruction const* insn,
1757 bool is_range) {
1758 // UNIMPLEMENTED(WARNING);
1759 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1760}
1761
1762
1763void MethodCompiler::EmitInsn_Neg(uint32_t dex_pc,
1764 Instruction const* insn,
1765 JType op_jty) {
Logan Chien1b5685f2011-12-27 18:01:14 +08001766
1767 Instruction::DecodedInstruction dec_insn(insn);
1768
1769 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
1770
1771 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB_, op_jty, kAccurate);
1772 llvm::Value* result_value = irb_.CreateNeg(src_value);
1773 EmitStoreDalvikReg(dec_insn.vA_, op_jty, kAccurate, result_value);
1774
Logan Chien70f94b42011-12-27 17:49:11 +08001775 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1776}
1777
1778
1779void MethodCompiler::EmitInsn_Not(uint32_t dex_pc,
1780 Instruction const* insn,
1781 JType op_jty) {
Logan Chiene53750d2011-12-27 18:02:27 +08001782
1783 Instruction::DecodedInstruction dec_insn(insn);
1784
1785 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
1786
1787 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB_, op_jty, kAccurate);
1788 llvm::Value* result_value =
1789 irb_.CreateXor(src_value, static_cast<uint64_t>(-1));
1790
1791 EmitStoreDalvikReg(dec_insn.vA_, op_jty, kAccurate, result_value);
1792
Logan Chien70f94b42011-12-27 17:49:11 +08001793 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1794}
1795
1796
1797void MethodCompiler::EmitInsn_SExt(uint32_t dex_pc,
1798 Instruction const* insn) {
Logan Chien61752ad2011-12-27 18:03:51 +08001799
1800 Instruction::DecodedInstruction dec_insn(insn);
1801
1802 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB_, kInt, kAccurate);
1803 llvm::Value* result_value = irb_.CreateSExt(src_value, irb_.getJLongTy());
1804 EmitStoreDalvikReg(dec_insn.vA_, kLong, kAccurate, result_value);
1805
Logan Chien70f94b42011-12-27 17:49:11 +08001806 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1807}
1808
1809
1810void MethodCompiler::EmitInsn_Trunc(uint32_t dex_pc,
1811 Instruction const* insn) {
Logan Chien17a57662011-12-27 18:05:14 +08001812
1813 Instruction::DecodedInstruction dec_insn(insn);
1814
1815 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB_, kLong, kAccurate);
1816 llvm::Value* result_value = irb_.CreateTrunc(src_value, irb_.getJIntTy());
1817 EmitStoreDalvikReg(dec_insn.vA_, kInt, kAccurate, result_value);
1818
Logan Chien70f94b42011-12-27 17:49:11 +08001819 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1820}
1821
1822
1823void MethodCompiler::EmitInsn_TruncAndSExt(uint32_t dex_pc,
1824 Instruction const* insn,
1825 unsigned N) {
Logan Chienb6744c52011-12-27 18:06:26 +08001826
1827 Instruction::DecodedInstruction dec_insn(insn);
1828
1829 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB_, kInt, kAccurate);
1830
1831 llvm::Value* trunc_value =
1832 irb_.CreateTrunc(src_value, llvm::Type::getIntNTy(*context_, N));
1833
1834 llvm::Value* result_value = irb_.CreateSExt(trunc_value, irb_.getJIntTy());
1835
1836 EmitStoreDalvikReg(dec_insn.vA_, kInt, kAccurate, result_value);
1837
Logan Chien70f94b42011-12-27 17:49:11 +08001838 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1839}
1840
1841
1842void MethodCompiler::EmitInsn_TruncAndZExt(uint32_t dex_pc,
1843 Instruction const* insn,
1844 unsigned N) {
Logan Chienb6744c52011-12-27 18:06:26 +08001845
1846 Instruction::DecodedInstruction dec_insn(insn);
1847
1848 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB_, kInt, kAccurate);
1849
1850 llvm::Value* trunc_value =
1851 irb_.CreateTrunc(src_value, llvm::Type::getIntNTy(*context_, N));
1852
1853 llvm::Value* result_value = irb_.CreateZExt(trunc_value, irb_.getJIntTy());
1854
1855 EmitStoreDalvikReg(dec_insn.vA_, kInt, kAccurate, result_value);
1856
Logan Chien70f94b42011-12-27 17:49:11 +08001857 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1858}
1859
1860
1861void MethodCompiler::EmitInsn_FNeg(uint32_t dex_pc,
1862 Instruction const* insn,
1863 JType op_jty) {
Logan Chien7a48b092011-12-27 18:07:45 +08001864
1865 Instruction::DecodedInstruction dec_insn(insn);
1866
1867 DCHECK(op_jty == kFloat || op_jty == kDouble) << op_jty;
1868
1869 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB_, op_jty, kAccurate);
1870 llvm::Value* result_value = irb_.CreateFNeg(src_value);
1871 EmitStoreDalvikReg(dec_insn.vA_, op_jty, kAccurate, result_value);
1872
Logan Chien70f94b42011-12-27 17:49:11 +08001873 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1874}
1875
1876
1877void MethodCompiler::EmitInsn_IntToFP(uint32_t dex_pc,
1878 Instruction const* insn,
1879 JType src_jty,
1880 JType dest_jty) {
Logan Chien62dd4532011-12-27 18:09:00 +08001881
1882 Instruction::DecodedInstruction dec_insn(insn);
1883
1884 DCHECK(src_jty == kInt || src_jty == kLong) << src_jty;
1885 DCHECK(dest_jty == kFloat || dest_jty == kDouble) << dest_jty;
1886
1887 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB_, src_jty, kAccurate);
1888 llvm::Type* dest_type = irb_.getJType(dest_jty, kAccurate);
1889 llvm::Value* dest_value = irb_.CreateSIToFP(src_value, dest_type);
1890 EmitStoreDalvikReg(dec_insn.vA_, dest_jty, kAccurate, dest_value);
1891
Logan Chien70f94b42011-12-27 17:49:11 +08001892 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1893}
1894
1895
1896void MethodCompiler::EmitInsn_FPToInt(uint32_t dex_pc,
1897 Instruction const* insn,
1898 JType src_jty,
1899 JType dest_jty) {
Logan Chien12dc1752011-12-27 18:10:15 +08001900
1901 Instruction::DecodedInstruction dec_insn(insn);
1902
1903 DCHECK(src_jty == kFloat || src_jty == kDouble) << src_jty;
1904 DCHECK(dest_jty == kInt || dest_jty == kLong) << dest_jty;
1905
1906 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB_, src_jty, kAccurate);
1907 llvm::Type* dest_type = irb_.getJType(dest_jty, kAccurate);
1908 llvm::Value* dest_value = irb_.CreateFPToSI(src_value, dest_type);
1909 EmitStoreDalvikReg(dec_insn.vA_, dest_jty, kAccurate, dest_value);
1910
Logan Chien70f94b42011-12-27 17:49:11 +08001911 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1912}
1913
1914
1915void MethodCompiler::EmitInsn_FExt(uint32_t dex_pc,
1916 Instruction const* insn) {
Logan Chienc56ded92011-12-27 18:10:57 +08001917
1918 Instruction::DecodedInstruction dec_insn(insn);
1919
1920 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB_, kFloat, kAccurate);
1921 llvm::Value* result_value = irb_.CreateFPExt(src_value, irb_.getJDoubleTy());
1922 EmitStoreDalvikReg(dec_insn.vA_, kDouble, kAccurate, result_value);
1923
Logan Chien70f94b42011-12-27 17:49:11 +08001924 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1925}
1926
1927
1928void MethodCompiler::EmitInsn_FTrunc(uint32_t dex_pc,
1929 Instruction const* insn) {
Logan Chien927744f2011-12-27 18:11:52 +08001930
1931 Instruction::DecodedInstruction dec_insn(insn);
1932
1933 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB_, kDouble, kAccurate);
1934 llvm::Value* result_value = irb_.CreateFPTrunc(src_value, irb_.getJFloatTy());
1935 EmitStoreDalvikReg(dec_insn.vA_, kFloat, kAccurate, result_value);
1936
Logan Chien70f94b42011-12-27 17:49:11 +08001937 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1938}
1939
1940
1941void MethodCompiler::EmitInsn_IntArithm(uint32_t dex_pc,
1942 Instruction const* insn,
1943 IntArithmKind arithm,
1944 JType op_jty,
1945 bool is_2addr) {
Logan Chienc3f7d962011-12-27 18:13:18 +08001946
1947 Instruction::DecodedInstruction dec_insn(insn);
1948
1949 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
1950
1951 llvm::Value* src1_value;
1952 llvm::Value* src2_value;
1953
1954 if (is_2addr) {
1955 src1_value = EmitLoadDalvikReg(dec_insn.vA_, op_jty, kAccurate);
1956 src2_value = EmitLoadDalvikReg(dec_insn.vB_, op_jty, kAccurate);
1957 } else {
1958 src1_value = EmitLoadDalvikReg(dec_insn.vB_, op_jty, kAccurate);
1959 src2_value = EmitLoadDalvikReg(dec_insn.vC_, op_jty, kAccurate);
1960 }
1961
1962 llvm::Value* result_value =
1963 EmitIntArithmResultComputation(dex_pc, src1_value, src2_value,
1964 arithm, op_jty);
1965
1966 EmitStoreDalvikReg(dec_insn.vA_, op_jty, kAccurate, result_value);
1967
Logan Chien70f94b42011-12-27 17:49:11 +08001968 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1969}
1970
1971
1972void MethodCompiler::EmitInsn_IntArithmImmediate(uint32_t dex_pc,
1973 Instruction const* insn,
1974 IntArithmKind arithm) {
Logan Chienc3f7d962011-12-27 18:13:18 +08001975
1976 Instruction::DecodedInstruction dec_insn(insn);
1977
1978 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB_, kInt, kAccurate);
1979
1980 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC_);
1981
1982 llvm::Value* result_value =
1983 EmitIntArithmResultComputation(dex_pc, src_value, imm_value, arithm, kInt);
1984
1985 EmitStoreDalvikReg(dec_insn.vA_, kInt, kAccurate, result_value);
1986
Logan Chien70f94b42011-12-27 17:49:11 +08001987 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1988}
1989
1990
Logan Chienc3f7d962011-12-27 18:13:18 +08001991llvm::Value*
1992MethodCompiler::EmitIntArithmResultComputation(uint32_t dex_pc,
1993 llvm::Value* lhs,
1994 llvm::Value* rhs,
1995 IntArithmKind arithm,
1996 JType op_jty) {
1997 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
1998
1999 switch (arithm) {
2000 case kIntArithm_Add:
2001 return irb_.CreateAdd(lhs, rhs);
2002
2003 case kIntArithm_Sub:
2004 return irb_.CreateSub(lhs, rhs);
2005
2006 case kIntArithm_Mul:
2007 return irb_.CreateMul(lhs, rhs);
2008
2009 case kIntArithm_Div:
2010 EmitGuard_DivZeroException(dex_pc, rhs, op_jty);
2011 return irb_.CreateSDiv(lhs, rhs);
2012
2013 case kIntArithm_Rem:
2014 EmitGuard_DivZeroException(dex_pc, rhs, op_jty);
2015 return irb_.CreateSRem(lhs, rhs);
2016
2017 case kIntArithm_And:
2018 return irb_.CreateAnd(lhs, rhs);
2019
2020 case kIntArithm_Or:
2021 return irb_.CreateOr(lhs, rhs);
2022
2023 case kIntArithm_Xor:
2024 return irb_.CreateXor(lhs, rhs);
2025
2026 case kIntArithm_Shl:
2027 if (op_jty == kLong) {
2028 return irb_.CreateShl(lhs, irb_.CreateAnd(rhs, 0x3f));
2029 } else {
2030 return irb_.CreateShl(lhs, irb_.CreateAnd(rhs, 0x1f));
2031 }
2032
2033 case kIntArithm_Shr:
2034 if (op_jty == kLong) {
2035 return irb_.CreateAShr(lhs, irb_.CreateAnd(rhs, 0x3f));
2036 } else {
2037 return irb_.CreateAShr(lhs, irb_.CreateAnd(rhs, 0x1f));
2038 }
2039
2040 case kIntArithm_UShr:
2041 if (op_jty == kLong) {
2042 return irb_.CreateLShr(lhs, irb_.CreateAnd(rhs, 0x3f));
2043 } else {
2044 return irb_.CreateLShr(lhs, irb_.CreateAnd(rhs, 0x1f));
2045 }
2046
2047 default:
2048 LOG(FATAL) << "Unknown integer arithmetic kind: " << arithm;
2049 return NULL;
2050 }
2051}
2052
2053
Logan Chien70f94b42011-12-27 17:49:11 +08002054void MethodCompiler::EmitInsn_RSubImmediate(uint32_t dex_pc,
2055 Instruction const* insn) {
Logan Chien65c62d42011-12-27 18:14:18 +08002056
2057 Instruction::DecodedInstruction dec_insn(insn);
2058
2059 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB_, kInt, kAccurate);
2060 llvm::Value* imm_value = irb_.getInt32(dec_insn.vC_);
2061 llvm::Value* result_value = irb_.CreateSub(imm_value, src_value);
2062 EmitStoreDalvikReg(dec_insn.vA_, kInt, kAccurate, result_value);
2063
Logan Chien70f94b42011-12-27 17:49:11 +08002064 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2065}
2066
2067
2068void MethodCompiler::EmitInsn_FPArithm(uint32_t dex_pc,
2069 Instruction const* insn,
2070 FPArithmKind arithm,
2071 JType op_jty,
2072 bool is_2addr) {
Logan Chien76e1c792011-12-27 18:15:01 +08002073
2074 Instruction::DecodedInstruction dec_insn(insn);
2075
2076 DCHECK(op_jty == kFloat || op_jty == kDouble) << op_jty;
2077
2078 llvm::Value* src1_value;
2079 llvm::Value* src2_value;
2080
2081 if (is_2addr) {
2082 src1_value = EmitLoadDalvikReg(dec_insn.vA_, op_jty, kAccurate);
2083 src2_value = EmitLoadDalvikReg(dec_insn.vB_, op_jty, kAccurate);
2084 } else {
2085 src1_value = EmitLoadDalvikReg(dec_insn.vB_, op_jty, kAccurate);
2086 src2_value = EmitLoadDalvikReg(dec_insn.vC_, op_jty, kAccurate);
2087 }
2088
2089 llvm::Value* result_value =
2090 EmitFPArithmResultComputation(dex_pc, src1_value, src2_value, arithm);
2091
2092 EmitStoreDalvikReg(dec_insn.vA_, op_jty, kAccurate, result_value);
2093
Logan Chien70f94b42011-12-27 17:49:11 +08002094 irb_.CreateBr(GetNextBasicBlock(dex_pc));
2095}
2096
2097
Logan Chien76e1c792011-12-27 18:15:01 +08002098llvm::Value*
2099MethodCompiler::EmitFPArithmResultComputation(uint32_t dex_pc,
2100 llvm::Value *lhs,
2101 llvm::Value *rhs,
2102 FPArithmKind arithm) {
2103 switch (arithm) {
2104 case kFPArithm_Add:
2105 return irb_.CreateFAdd(lhs, rhs);
2106
2107 case kFPArithm_Sub:
2108 return irb_.CreateFSub(lhs, rhs);
2109
2110 case kFPArithm_Mul:
2111 return irb_.CreateFMul(lhs, rhs);
2112
2113 case kFPArithm_Div:
2114 return irb_.CreateFDiv(lhs, rhs);
2115
2116 case kFPArithm_Rem:
2117 return irb_.CreateFRem(lhs, rhs);
2118
2119 default:
2120 LOG(FATAL) << "Unknown floating-point arithmetic kind: " << arithm;
2121 return NULL;
2122 }
2123}
2124
2125
Logan Chienc3f7d962011-12-27 18:13:18 +08002126void MethodCompiler::EmitGuard_DivZeroException(uint32_t dex_pc,
2127 llvm::Value* denominator,
2128 JType op_jty) {
2129 DCHECK(op_jty == kInt || op_jty == kLong) << op_jty;
2130
2131 llvm::Constant* zero = irb_.getJZero(op_jty);
2132
2133 llvm::Value* equal_zero = irb_.CreateICmpEQ(denominator, zero);
2134
2135 llvm::BasicBlock* block_exception = CreateBasicBlockWithDexPC(dex_pc, "div0");
2136
2137 llvm::BasicBlock* block_continue = CreateBasicBlockWithDexPC(dex_pc, "cont");
2138
2139 irb_.CreateCondBr(equal_zero, block_exception, block_continue);
2140
2141 irb_.SetInsertPoint(block_exception);
2142 irb_.CreateCall(irb_.GetRuntime(ThrowDivZeroException));
2143 EmitBranchExceptionLandingPad(dex_pc);
2144
2145 irb_.SetInsertPoint(block_continue);
2146}
2147
2148
Logan Chien61bb6142012-02-03 15:34:53 +08002149void MethodCompiler::EmitGuard_NullPointerException(uint32_t dex_pc,
2150 llvm::Value* object) {
2151 llvm::Value* equal_null = irb_.CreateICmpEQ(object, irb_.getJNull());
2152
2153 llvm::BasicBlock* block_exception =
2154 CreateBasicBlockWithDexPC(dex_pc, "nullp");
2155
2156 llvm::BasicBlock* block_continue =
2157 CreateBasicBlockWithDexPC(dex_pc, "cont");
2158
2159 irb_.CreateCondBr(equal_null, block_exception, block_continue);
2160
2161 irb_.SetInsertPoint(block_exception);
2162 irb_.CreateCall(irb_.GetRuntime(ThrowNullPointerException));
2163 EmitBranchExceptionLandingPad(dex_pc);
2164
2165 irb_.SetInsertPoint(block_continue);
2166}
2167
2168
Logan Chien83426162011-12-09 09:29:50 +08002169CompiledMethod *MethodCompiler::Compile() {
Logan Chien0b827102011-12-20 19:46:14 +08002170 // Code generation
2171 CreateFunction();
2172
2173 EmitPrologue();
2174 EmitInstructions();
Logan Chienc670a8d2011-12-20 21:25:56 +08002175 EmitPrologueLastBranch();
Logan Chien0b827102011-12-20 19:46:14 +08002176
Logan Chiend6c239a2011-12-23 15:11:45 +08002177 // Verify the generated bitcode
2178 llvm::verifyFunction(*func_, llvm::PrintMessageAction);
2179
Logan Chien0b827102011-12-20 19:46:14 +08002180 // Delete the inferred register category map (won't be used anymore)
2181 method_->ResetInferredRegCategoryMap();
2182
2183 return new CompiledMethod(insn_set_, func_);
2184}
2185
2186
2187llvm::Value* MethodCompiler::EmitLoadMethodObjectAddr() {
2188 return func_->arg_begin();
Shih-wei Liaod1fec812012-02-13 09:51:10 -08002189}
Logan Chien83426162011-12-09 09:29:50 +08002190
2191
Logan Chien5bcc04e2012-01-30 14:15:12 +08002192void MethodCompiler::EmitBranchExceptionLandingPad(uint32_t dex_pc) {
2193 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
2194 irb_.CreateBr(lpad);
2195 } else {
2196 irb_.CreateBr(GetUnwindBasicBlock());
2197 }
2198}
2199
2200
2201void MethodCompiler::EmitGuard_ExceptionLandingPad(uint32_t dex_pc) {
2202 llvm::Value* exception_pending =
2203 irb_.CreateCall(irb_.GetRuntime(IsExceptionPending));
2204
2205 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
2206
2207 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
2208 irb_.CreateCondBr(exception_pending, lpad, block_cont);
2209 } else {
2210 irb_.CreateCondBr(exception_pending, GetUnwindBasicBlock(), block_cont);
2211 }
2212
2213 irb_.SetInsertPoint(block_cont);
2214}
2215
2216
Logan Chien924072f2012-01-30 15:07:24 +08002217void MethodCompiler::EmitGuard_GarbageCollectionSuspend(uint32_t dex_pc) {
2218 llvm::Value* runtime_func = irb_.GetRuntime(TestSuspend);
2219 irb_.CreateCall(runtime_func);
2220
2221 EmitGuard_ExceptionLandingPad(dex_pc);
2222}
2223
2224
Logan Chiend6c239a2011-12-23 15:11:45 +08002225llvm::BasicBlock* MethodCompiler::
2226CreateBasicBlockWithDexPC(uint32_t dex_pc, char const* postfix) {
2227 std::string name;
2228
2229 if (postfix) {
2230 StringAppendF(&name, "B%u.%s", dex_pc, postfix);
2231 } else {
2232 StringAppendF(&name, "B%u", dex_pc);
2233 }
2234
2235 return llvm::BasicBlock::Create(*context_, name, func_);
2236}
2237
2238
2239llvm::BasicBlock* MethodCompiler::GetBasicBlock(uint32_t dex_pc) {
2240 DCHECK(dex_pc < code_item_->insns_size_in_code_units_);
2241
2242 llvm::BasicBlock* basic_block = basic_blocks_[dex_pc];
2243
2244 if (!basic_block) {
2245 basic_block = CreateBasicBlockWithDexPC(dex_pc);
2246 basic_blocks_[dex_pc] = basic_block;
2247 }
2248
2249 return basic_block;
2250}
2251
2252
2253llvm::BasicBlock*
2254MethodCompiler::GetNextBasicBlock(uint32_t dex_pc) {
2255 Instruction const* insn = Instruction::At(code_item_->insns_ + dex_pc);
2256 return GetBasicBlock(dex_pc + insn->SizeInCodeUnits());
2257}
2258
2259
Logan Chien5bcc04e2012-01-30 14:15:12 +08002260int32_t MethodCompiler::GetTryItemOffset(uint32_t dex_pc) {
2261 // TODO: Since we are emitting the dex instructions in ascending order
2262 // w.r.t. address, we can cache the lastest try item offset so that we
2263 // don't have to do binary search for every query.
2264
2265 int32_t min = 0;
2266 int32_t max = code_item_->tries_size_ - 1;
2267
2268 while (min <= max) {
2269 int32_t mid = min + (max - min) / 2;
2270
2271 DexFile::TryItem const* ti = DexFile::GetTryItems(*code_item_, mid);
2272 uint32_t start = ti->start_addr_;
2273 uint32_t end = start + ti->insn_count_;
2274
2275 if (dex_pc < start) {
2276 max = mid - 1;
2277 } else if (dex_pc >= end) {
2278 min = mid + 1;
2279 } else {
2280 return mid; // found
2281 }
2282 }
2283
2284 return -1; // not found
2285}
2286
2287
2288llvm::BasicBlock* MethodCompiler::GetLandingPadBasicBlock(uint32_t dex_pc) {
2289 // Find the try item for this address in this method
2290 int32_t ti_offset = GetTryItemOffset(dex_pc);
2291
2292 if (ti_offset == -1) {
2293 return NULL; // No landing pad is available for this address.
2294 }
2295
2296 // Check for the existing landing pad basic block
2297 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
2298 llvm::BasicBlock* block_lpad = basic_block_landing_pads_[ti_offset];
2299
2300 if (block_lpad) {
2301 // We have generated landing pad for this try item already. Return the
2302 // same basic block.
2303 return block_lpad;
2304 }
2305
2306 // Get try item from code item
2307 DexFile::TryItem const* ti = DexFile::GetTryItems(*code_item_, ti_offset);
2308
2309 // Create landing pad basic block
2310 block_lpad = llvm::BasicBlock::Create(*context_,
2311 StringPrintf("lpad%d", ti_offset),
2312 func_);
2313
2314 // Change IRBuilder insert point
2315 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
2316 irb_.SetInsertPoint(block_lpad);
2317
2318 // Find catch block with matching type
2319 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
2320
2321 // TODO: Maybe passing try item offset will be a better idea? For now,
2322 // we are passing dex_pc, so that we can use existing runtime support
2323 // function directly. However, in the runtime supporting function we
2324 // have to search for try item with binary search which can be
2325 // eliminated.
2326 llvm::Value* dex_pc_value = irb_.getInt32(ti->start_addr_);
2327
2328 llvm::Value* catch_handler_index_value =
2329 irb_.CreateCall2(irb_.GetRuntime(FindCatchBlock),
2330 method_object_addr, dex_pc_value);
2331
2332 // Switch instruction (Go to unwind basic block by default)
2333 llvm::SwitchInst* sw =
2334 irb_.CreateSwitch(catch_handler_index_value, GetUnwindBasicBlock());
2335
2336 // Cases with matched catch block
2337 CatchHandlerIterator iter(*code_item_, ti->start_addr_);
2338
2339 for (uint32_t c = 0; iter.HasNext(); iter.Next(), ++c) {
2340 sw->addCase(irb_.getInt32(c), GetBasicBlock(iter.GetHandlerAddress()));
2341 }
2342
2343 // Restore the orignal insert point for IRBuilder
2344 irb_.restoreIP(irb_ip_original);
2345
2346 // Cache this landing pad
2347 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
2348 basic_block_landing_pads_[ti_offset] = block_lpad;
2349
2350 return block_lpad;
2351}
2352
2353
2354llvm::BasicBlock* MethodCompiler::GetUnwindBasicBlock() {
2355 // Check the existing unwinding baisc block block
2356 if (basic_block_unwind_ != NULL) {
2357 return basic_block_unwind_;
2358 }
2359
2360 // Create new basic block for unwinding
2361 basic_block_unwind_ =
2362 llvm::BasicBlock::Create(*context_, "exception_unwind", func_);
2363
2364 // Change IRBuilder insert point
2365 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
2366 irb_.SetInsertPoint(basic_block_unwind_);
2367
2368 // Emit the code to return default value (zero) for the given return type.
2369 char ret_shorty = method_helper_.GetShorty()[0];
2370 if (ret_shorty == 'V') {
2371 irb_.CreateRetVoid();
2372 } else {
2373 irb_.CreateRet(irb_.getJZero(ret_shorty));
2374 }
2375
2376 // Restore the orignal insert point for IRBuilder
2377 irb_.restoreIP(irb_ip_original);
2378
2379 return basic_block_unwind_;
2380}
2381
2382
Logan Chienc670a8d2011-12-20 21:25:56 +08002383llvm::Value* MethodCompiler::AllocDalvikLocalVarReg(RegCategory cat,
2384 uint32_t reg_idx) {
2385
2386 // Save current IR builder insert point
2387 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
2388
2389 // Alloca
2390 llvm::Value* reg_addr = NULL;
2391
2392 switch (cat) {
2393 case kRegCat1nr:
2394 irb_.SetInsertPoint(basic_block_reg_alloca_);
2395 reg_addr = irb_.CreateAlloca(irb_.getJIntTy(), 0,
2396 StringPrintf("r%u", reg_idx));
2397
2398 irb_.SetInsertPoint(basic_block_reg_zero_init_);
2399 irb_.CreateStore(irb_.getJInt(0), reg_addr);
2400 break;
2401
2402 case kRegCat2:
2403 irb_.SetInsertPoint(basic_block_reg_alloca_);
2404 reg_addr = irb_.CreateAlloca(irb_.getJLongTy(), 0,
2405 StringPrintf("w%u", reg_idx));
2406
2407 irb_.SetInsertPoint(basic_block_reg_zero_init_);
2408 irb_.CreateStore(irb_.getJLong(0), reg_addr);
2409 break;
2410
2411 case kRegObject:
2412 irb_.SetInsertPoint(basic_block_reg_alloca_);
2413 reg_addr = irb_.CreateAlloca(irb_.getJObjectTy(), 0,
2414 StringPrintf("p%u", reg_idx));
2415
2416 irb_.SetInsertPoint(basic_block_reg_zero_init_);
2417 irb_.CreateStore(irb_.getJNull(), reg_addr);
2418 break;
2419
2420 default:
2421 LOG(FATAL) << "Unknown register category for allocation: " << cat;
2422 }
2423
2424 // Restore IRBuilder insert point
2425 irb_.restoreIP(irb_ip_original);
2426
2427 DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL));
2428 return reg_addr;
2429}
2430
2431
2432llvm::Value* MethodCompiler::AllocDalvikRetValReg(RegCategory cat) {
2433 // Save current IR builder insert point
2434 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
2435
2436 // Alloca
2437 llvm::Value* reg_addr = NULL;
2438
2439 switch (cat) {
2440 case kRegCat1nr:
2441 irb_.SetInsertPoint(basic_block_reg_alloca_);
2442 reg_addr = irb_.CreateAlloca(irb_.getJIntTy(), 0, "r_res");
2443 break;
2444
2445 case kRegCat2:
2446 irb_.SetInsertPoint(basic_block_reg_alloca_);
2447 reg_addr = irb_.CreateAlloca(irb_.getJLongTy(), 0, "w_res");
2448 break;
2449
2450 case kRegObject:
2451 irb_.SetInsertPoint(basic_block_reg_alloca_);
2452 reg_addr = irb_.CreateAlloca(irb_.getJObjectTy(), 0, "p_res");
2453 break;
2454
2455 default:
2456 LOG(FATAL) << "Unknown register category for allocation: " << cat;
2457 }
2458
2459 // Restore IRBuilder insert point
2460 irb_.restoreIP(irb_ip_original);
2461
2462 DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL));
2463 return reg_addr;
2464}
2465
2466
Logan Chien83426162011-12-09 09:29:50 +08002467} // namespace compiler_llvm
2468} // namespace art