blob: 312b282d170216dfd548a9bf9d92cdb191af5b1f [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"
21#include "ir_builder.h"
22#include "logging.h"
23#include "object.h"
24#include "object_utils.h"
Logan Chien42e0e152012-01-13 15:42:36 +080025#include "runtime_support_func.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080026#include "stl_util.h"
Logan Chien0b827102011-12-20 19:46:14 +080027#include "stringprintf.h"
28#include "utils_llvm.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080029
30#include <iomanip>
31
32#include <llvm/Analysis/Verifier.h>
Logan Chienc670a8d2011-12-20 21:25:56 +080033#include <llvm/BasicBlock.h>
Shih-wei Liaod1fec812012-02-13 09:51:10 -080034#include <llvm/Function.h>
35
Logan Chien83426162011-12-09 09:29:50 +080036namespace art {
37namespace compiler_llvm {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080038
Logan Chien42e0e152012-01-13 15:42:36 +080039using namespace runtime_support;
40
Shih-wei Liaod1fec812012-02-13 09:51:10 -080041
Logan Chien83426162011-12-09 09:29:50 +080042MethodCompiler::MethodCompiler(InstructionSet insn_set,
43 Compiler* compiler,
44 ClassLinker* class_linker,
45 ClassLoader const* class_loader,
46 DexFile const* dex_file,
47 DexCache* dex_cache,
48 DexFile::CodeItem const* code_item,
Shih-wei Liaod1fec812012-02-13 09:51:10 -080049 uint32_t method_idx,
50 uint32_t access_flags)
51: insn_set_(insn_set),
52 compiler_(compiler), compiler_llvm_(compiler->GetCompilerLLVM()),
53 class_linker_(class_linker), class_loader_(class_loader),
54 dex_file_(dex_file), dex_cache_(dex_cache), code_item_(code_item),
55 method_(dex_cache->GetResolvedMethod(method_idx)),
56 method_helper_(method_), method_idx_(method_idx),
57 access_flags_(access_flags), module_(compiler_llvm_->GetModule()),
58 context_(compiler_llvm_->GetLLVMContext()),
Logan Chienc670a8d2011-12-20 21:25:56 +080059 irb_(*compiler_llvm_->GetIRBuilder()), func_(NULL), retval_reg_(NULL),
Logan Chiend6ececa2011-12-27 16:20:15 +080060 basic_block_reg_alloca_(NULL),
61 basic_block_reg_zero_init_(NULL), basic_block_reg_arg_init_(NULL),
Logan Chien5bcc04e2012-01-30 14:15:12 +080062 basic_blocks_(code_item->insns_size_in_code_units_),
63 basic_block_landing_pads_(code_item->tries_size_, NULL),
64 basic_block_unwind_(NULL), basic_block_unreachable_(NULL) {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080065}
66
67
68MethodCompiler::~MethodCompiler() {
Logan Chienc670a8d2011-12-20 21:25:56 +080069 STLDeleteElements(&regs_);
Shih-wei Liaod1fec812012-02-13 09:51:10 -080070}
71
72
Logan Chien0b827102011-12-20 19:46:14 +080073void MethodCompiler::CreateFunction() {
74 // LLVM function name
75 std::string func_name(LLVMLongName(method_));
76
77 // Get function type
78 llvm::FunctionType* func_type =
79 GetFunctionType(method_idx_, method_->IsStatic());
80
81 // Create function
82 func_ = llvm::Function::Create(func_type, llvm::Function::ExternalLinkage,
83 func_name, module_);
84
85 // Set argument name
86 llvm::Function::arg_iterator arg_iter(func_->arg_begin());
87 llvm::Function::arg_iterator arg_end(func_->arg_end());
88
89 DCHECK_NE(arg_iter, arg_end);
90 arg_iter->setName("method");
91 ++arg_iter;
92
93 if (!method_->IsStatic()) {
94 DCHECK_NE(arg_iter, arg_end);
95 arg_iter->setName("this");
96 ++arg_iter;
97 }
98
99 for (unsigned i = 0; arg_iter != arg_end; ++i, ++arg_iter) {
100 arg_iter->setName(StringPrintf("a%u", i));
101 }
102}
103
104
105llvm::FunctionType* MethodCompiler::GetFunctionType(uint32_t method_idx,
106 bool is_static) {
107 // Get method signature
108 DexFile::MethodId const& method_id = dex_file_->GetMethodId(method_idx);
109
110 int32_t shorty_size;
111 char const* shorty = dex_file_->GetMethodShorty(method_id, &shorty_size);
112 CHECK_GE(shorty_size, 1);
113
114 // Get return type
115 llvm::Type* ret_type = irb_.getJType(shorty[0], kAccurate);
116
117 // Get argument type
118 std::vector<llvm::Type*> args_type;
119
120 args_type.push_back(irb_.getJObjectTy()); // method object pointer
121
122 if (!is_static) {
123 args_type.push_back(irb_.getJType('L', kAccurate)); // "this" object pointer
124 }
125
126 for (int32_t i = 1; i < shorty_size; ++i) {
127 args_type.push_back(irb_.getJType(shorty[i], kAccurate));
128 }
129
130 return llvm::FunctionType::get(ret_type, args_type, false);
131}
132
133
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800134void MethodCompiler::EmitPrologue() {
Logan Chienc670a8d2011-12-20 21:25:56 +0800135 // Create basic blocks for prologue
136 basic_block_reg_alloca_ =
137 llvm::BasicBlock::Create(*context_, "prologue.alloca", func_);
138
139 basic_block_reg_zero_init_ =
140 llvm::BasicBlock::Create(*context_, "prologue.zeroinit", func_);
141
Logan Chiend6ececa2011-12-27 16:20:15 +0800142 basic_block_reg_arg_init_ =
143 llvm::BasicBlock::Create(*context_, "prologue.arginit", func_);
144
Logan Chienc670a8d2011-12-20 21:25:56 +0800145 // Create register array
146 for (uint16_t r = 0; r < code_item_->registers_size_; ++r) {
147 regs_.push_back(DalvikReg::CreateLocalVarReg(*this, r));
148 }
149
150 retval_reg_.reset(DalvikReg::CreateRetValReg(*this));
Logan Chiend6ececa2011-12-27 16:20:15 +0800151
152 // Store argument to dalvik register
153 irb_.SetInsertPoint(basic_block_reg_arg_init_);
154 EmitPrologueAssignArgRegister();
155
156 // Branch to start address
157 irb_.CreateBr(GetBasicBlock(0));
Logan Chienc670a8d2011-12-20 21:25:56 +0800158}
159
160
161void MethodCompiler::EmitPrologueLastBranch() {
162 irb_.SetInsertPoint(basic_block_reg_alloca_);
163 irb_.CreateBr(basic_block_reg_zero_init_);
164
165 irb_.SetInsertPoint(basic_block_reg_zero_init_);
Logan Chiend6ececa2011-12-27 16:20:15 +0800166 irb_.CreateBr(basic_block_reg_arg_init_);
167}
168
169
170void MethodCompiler::EmitPrologueAssignArgRegister() {
171 uint16_t arg_reg = code_item_->registers_size_ - code_item_->ins_size_;
172
173 llvm::Function::arg_iterator arg_iter(func_->arg_begin());
174 llvm::Function::arg_iterator arg_end(func_->arg_end());
175
176 char const* shorty = method_helper_.GetShorty();
177 int32_t shorty_size = method_helper_.GetShortyLength();
178 CHECK_LE(1, shorty_size);
179
180 ++arg_iter; // skip method object
181
182 if (!method_->IsStatic()) {
183 EmitStoreDalvikReg(arg_reg, kObject, kAccurate, arg_iter);
184 ++arg_iter;
185 ++arg_reg;
186 }
187
188 for (int32_t i = 1; i < shorty_size; ++i, ++arg_iter) {
189 EmitStoreDalvikReg(arg_reg, shorty[i], kAccurate, arg_iter);
190
191 ++arg_reg;
192 if (shorty[i] == 'J' || shorty[i] == 'D') {
193 // Wide types, such as long and double, are using a pair of registers
194 // to store the value, so we have to increase arg_reg again.
195 ++arg_reg;
196 }
197 }
198
199 DCHECK_EQ(arg_end, arg_iter);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800200}
201
202
Logan Chien83426162011-12-09 09:29:50 +0800203void MethodCompiler::EmitInstructions() {
Logan Chiend6c239a2011-12-23 15:11:45 +0800204 uint32_t dex_pc = 0;
205 while (dex_pc < code_item_->insns_size_in_code_units_) {
206 Instruction const* insn = Instruction::At(code_item_->insns_ + dex_pc);
207 EmitInstruction(dex_pc, insn);
208 dex_pc += insn->SizeInCodeUnits();
209 }
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800210}
211
212
Logan Chien83426162011-12-09 09:29:50 +0800213void MethodCompiler::EmitInstruction(uint32_t dex_pc,
214 Instruction const* insn) {
Logan Chiend6c239a2011-12-23 15:11:45 +0800215
216 // Set the IRBuilder insertion point
217 irb_.SetInsertPoint(GetBasicBlock(dex_pc));
218
Logan Chien70f94b42011-12-27 17:49:11 +0800219#define ARGS dex_pc, insn
220
221 // Dispatch the instruction
222 switch (insn->Opcode()) {
223 case Instruction::NOP:
224 EmitInsn_Nop(ARGS);
225 break;
226
227 case Instruction::MOVE:
228 case Instruction::MOVE_FROM16:
229 case Instruction::MOVE_16:
230 EmitInsn_Move(ARGS, kInt);
231 break;
232
233 case Instruction::MOVE_WIDE:
234 case Instruction::MOVE_WIDE_FROM16:
235 case Instruction::MOVE_WIDE_16:
236 EmitInsn_Move(ARGS, kLong);
237 break;
238
239 case Instruction::MOVE_OBJECT:
240 case Instruction::MOVE_OBJECT_FROM16:
241 case Instruction::MOVE_OBJECT_16:
242 EmitInsn_Move(ARGS, kObject);
243 break;
244
245 case Instruction::MOVE_RESULT:
246 EmitInsn_MoveResult(ARGS, kInt);
247 break;
248
249 case Instruction::MOVE_RESULT_WIDE:
250 EmitInsn_MoveResult(ARGS, kLong);
251 break;
252
253 case Instruction::MOVE_RESULT_OBJECT:
254 EmitInsn_MoveResult(ARGS, kObject);
255 break;
256
257 case Instruction::MOVE_EXCEPTION:
258 EmitInsn_MoveException(ARGS);
259 break;
260
261 case Instruction::RETURN_VOID:
262 EmitInsn_ReturnVoid(ARGS);
263 break;
264
265 case Instruction::RETURN:
266 case Instruction::RETURN_WIDE:
267 case Instruction::RETURN_OBJECT:
268 EmitInsn_Return(ARGS);
269 break;
270
271 case Instruction::CONST_4:
272 case Instruction::CONST_16:
273 case Instruction::CONST:
274 case Instruction::CONST_HIGH16:
275 EmitInsn_LoadConstant(ARGS, kInt);
276 break;
277
278 case Instruction::CONST_WIDE_16:
279 case Instruction::CONST_WIDE_32:
280 case Instruction::CONST_WIDE:
281 case Instruction::CONST_WIDE_HIGH16:
282 EmitInsn_LoadConstant(ARGS, kLong);
283 break;
284
285 case Instruction::CONST_STRING:
286 case Instruction::CONST_STRING_JUMBO:
287 EmitInsn_LoadConstantString(ARGS);
288 break;
289
290 case Instruction::CONST_CLASS:
291 EmitInsn_LoadConstantClass(ARGS);
292 break;
293
294 case Instruction::MONITOR_ENTER:
295 EmitInsn_MonitorEnter(ARGS);
296 break;
297
298 case Instruction::MONITOR_EXIT:
299 EmitInsn_MonitorExit(ARGS);
300 break;
301
302 case Instruction::CHECK_CAST:
303 EmitInsn_CheckCast(ARGS);
304 break;
305
306 case Instruction::INSTANCE_OF:
307 EmitInsn_InstanceOf(ARGS);
308 break;
309
310 case Instruction::ARRAY_LENGTH:
311 EmitInsn_ArrayLength(ARGS);
312 break;
313
314 case Instruction::NEW_INSTANCE:
315 EmitInsn_NewInstance(ARGS);
316 break;
317
318 case Instruction::NEW_ARRAY:
319 EmitInsn_NewArray(ARGS);
320 break;
321
322 case Instruction::FILLED_NEW_ARRAY:
323 EmitInsn_FilledNewArray(ARGS, false);
324 break;
325
326 case Instruction::FILLED_NEW_ARRAY_RANGE:
327 EmitInsn_FilledNewArray(ARGS, true);
328 break;
329
330 case Instruction::FILL_ARRAY_DATA:
331 EmitInsn_FillArrayData(ARGS);
332 break;
333
334 case Instruction::THROW:
335 EmitInsn_ThrowException(ARGS);
336 break;
337
338 case Instruction::GOTO:
339 case Instruction::GOTO_16:
340 case Instruction::GOTO_32:
341 EmitInsn_UnconditionalBranch(ARGS);
342 break;
343
344 case Instruction::PACKED_SWITCH:
345 EmitInsn_PackedSwitch(ARGS);
346 break;
347
348 case Instruction::SPARSE_SWITCH:
349 EmitInsn_SparseSwitch(ARGS);
350 break;
351
352 case Instruction::CMPL_FLOAT:
353 EmitInsn_FPCompare(ARGS, kFloat, false);
354 break;
355
356 case Instruction::CMPG_FLOAT:
357 EmitInsn_FPCompare(ARGS, kFloat, true);
358 break;
359
360 case Instruction::CMPL_DOUBLE:
361 EmitInsn_FPCompare(ARGS, kDouble, false);
362 break;
363
364 case Instruction::CMPG_DOUBLE:
365 EmitInsn_FPCompare(ARGS, kDouble, true);
366 break;
367
368 case Instruction::CMP_LONG:
369 EmitInsn_LongCompare(ARGS);
370 break;
371
372 case Instruction::IF_EQ:
373 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_EQ);
374 break;
375
376 case Instruction::IF_NE:
377 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_NE);
378 break;
379
380 case Instruction::IF_LT:
381 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_LT);
382 break;
383
384 case Instruction::IF_GE:
385 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_GE);
386 break;
387
388 case Instruction::IF_GT:
389 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_GT);
390 break;
391
392 case Instruction::IF_LE:
393 EmitInsn_BinaryConditionalBranch(ARGS, kCondBranch_LE);
394 break;
395
396 case Instruction::IF_EQZ:
397 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_EQ);
398 break;
399
400 case Instruction::IF_NEZ:
401 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_NE);
402 break;
403
404 case Instruction::IF_LTZ:
405 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_LT);
406 break;
407
408 case Instruction::IF_GEZ:
409 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_GE);
410 break;
411
412 case Instruction::IF_GTZ:
413 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_GT);
414 break;
415
416 case Instruction::IF_LEZ:
417 EmitInsn_UnaryConditionalBranch(ARGS, kCondBranch_LE);
418 break;
419
420 case Instruction::AGET:
421 EmitInsn_AGet(ARGS, kInt);
422 break;
423
424 case Instruction::AGET_WIDE:
425 EmitInsn_AGet(ARGS, kLong);
426 break;
427
428 case Instruction::AGET_OBJECT:
429 EmitInsn_AGet(ARGS, kObject);
430 break;
431
432 case Instruction::AGET_BOOLEAN:
433 EmitInsn_AGet(ARGS, kBoolean);
434 break;
435
436 case Instruction::AGET_BYTE:
437 EmitInsn_AGet(ARGS, kByte);
438 break;
439
440 case Instruction::AGET_CHAR:
441 EmitInsn_AGet(ARGS, kChar);
442 break;
443
444 case Instruction::AGET_SHORT:
445 EmitInsn_AGet(ARGS, kShort);
446 break;
447
448 case Instruction::APUT:
449 EmitInsn_APut(ARGS, kInt);
450 break;
451
452 case Instruction::APUT_WIDE:
453 EmitInsn_APut(ARGS, kLong);
454 break;
455
456 case Instruction::APUT_OBJECT:
457 EmitInsn_APut(ARGS, kObject);
458 break;
459
460 case Instruction::APUT_BOOLEAN:
461 EmitInsn_APut(ARGS, kBoolean);
462 break;
463
464 case Instruction::APUT_BYTE:
465 EmitInsn_APut(ARGS, kByte);
466 break;
467
468 case Instruction::APUT_CHAR:
469 EmitInsn_APut(ARGS, kChar);
470 break;
471
472 case Instruction::APUT_SHORT:
473 EmitInsn_APut(ARGS, kShort);
474 break;
475
476 case Instruction::IGET:
477 EmitInsn_IGet(ARGS, kInt);
478 break;
479
480 case Instruction::IGET_WIDE:
481 EmitInsn_IGet(ARGS, kLong);
482 break;
483
484 case Instruction::IGET_OBJECT:
485 EmitInsn_IGet(ARGS, kObject);
486 break;
487
488 case Instruction::IGET_BOOLEAN:
489 EmitInsn_IGet(ARGS, kBoolean);
490 break;
491
492 case Instruction::IGET_BYTE:
493 EmitInsn_IGet(ARGS, kByte);
494 break;
495
496 case Instruction::IGET_CHAR:
497 EmitInsn_IGet(ARGS, kChar);
498 break;
499
500 case Instruction::IGET_SHORT:
501 EmitInsn_IGet(ARGS, kShort);
502 break;
503
504 case Instruction::IPUT:
505 EmitInsn_IPut(ARGS, kInt);
506 break;
507
508 case Instruction::IPUT_WIDE:
509 EmitInsn_IPut(ARGS, kLong);
510 break;
511
512 case Instruction::IPUT_OBJECT:
513 EmitInsn_IPut(ARGS, kObject);
514 break;
515
516 case Instruction::IPUT_BOOLEAN:
517 EmitInsn_IPut(ARGS, kBoolean);
518 break;
519
520 case Instruction::IPUT_BYTE:
521 EmitInsn_IPut(ARGS, kByte);
522 break;
523
524 case Instruction::IPUT_CHAR:
525 EmitInsn_IPut(ARGS, kChar);
526 break;
527
528 case Instruction::IPUT_SHORT:
529 EmitInsn_IPut(ARGS, kShort);
530 break;
531
532 case Instruction::SGET:
533 EmitInsn_SGet(ARGS, kInt);
534 break;
535
536 case Instruction::SGET_WIDE:
537 EmitInsn_SGet(ARGS, kLong);
538 break;
539
540 case Instruction::SGET_OBJECT:
541 EmitInsn_SGet(ARGS, kObject);
542 break;
543
544 case Instruction::SGET_BOOLEAN:
545 EmitInsn_SGet(ARGS, kBoolean);
546 break;
547
548 case Instruction::SGET_BYTE:
549 EmitInsn_SGet(ARGS, kByte);
550 break;
551
552 case Instruction::SGET_CHAR:
553 EmitInsn_SGet(ARGS, kChar);
554 break;
555
556 case Instruction::SGET_SHORT:
557 EmitInsn_SGet(ARGS, kShort);
558 break;
559
560 case Instruction::SPUT:
561 EmitInsn_SPut(ARGS, kInt);
562 break;
563
564 case Instruction::SPUT_WIDE:
565 EmitInsn_SPut(ARGS, kLong);
566 break;
567
568 case Instruction::SPUT_OBJECT:
569 EmitInsn_SPut(ARGS, kObject);
570 break;
571
572 case Instruction::SPUT_BOOLEAN:
573 EmitInsn_SPut(ARGS, kBoolean);
574 break;
575
576 case Instruction::SPUT_BYTE:
577 EmitInsn_SPut(ARGS, kByte);
578 break;
579
580 case Instruction::SPUT_CHAR:
581 EmitInsn_SPut(ARGS, kChar);
582 break;
583
584 case Instruction::SPUT_SHORT:
585 EmitInsn_SPut(ARGS, kShort);
586 break;
587
588
589 case Instruction::INVOKE_VIRTUAL:
590 EmitInsn_InvokeVirtual(ARGS, false);
591 break;
592
593 case Instruction::INVOKE_SUPER:
594 EmitInsn_InvokeSuper(ARGS, false);
595 break;
596
597 case Instruction::INVOKE_DIRECT:
598 EmitInsn_InvokeDirect(ARGS, false);
599 break;
600
601 case Instruction::INVOKE_STATIC:
602 EmitInsn_InvokeStatic(ARGS, false);
603 break;
604
605 case Instruction::INVOKE_INTERFACE:
606 EmitInsn_InvokeInterface(ARGS, false);
607 break;
608
609 case Instruction::INVOKE_VIRTUAL_RANGE:
610 EmitInsn_InvokeVirtual(ARGS, true);
611 break;
612
613 case Instruction::INVOKE_SUPER_RANGE:
614 EmitInsn_InvokeSuper(ARGS, true);
615 break;
616
617 case Instruction::INVOKE_DIRECT_RANGE:
618 EmitInsn_InvokeDirect(ARGS, true);
619 break;
620
621 case Instruction::INVOKE_STATIC_RANGE:
622 EmitInsn_InvokeStatic(ARGS, true);
623 break;
624
625 case Instruction::INVOKE_INTERFACE_RANGE:
626 EmitInsn_InvokeInterface(ARGS, true);
627 break;
628
629 case Instruction::NEG_INT:
630 EmitInsn_Neg(ARGS, kInt);
631 break;
632
633 case Instruction::NOT_INT:
634 EmitInsn_Not(ARGS, kInt);
635 break;
636
637 case Instruction::NEG_LONG:
638 EmitInsn_Neg(ARGS, kLong);
639 break;
640
641 case Instruction::NOT_LONG:
642 EmitInsn_Not(ARGS, kLong);
643 break;
644
645 case Instruction::NEG_FLOAT:
646 EmitInsn_FNeg(ARGS, kFloat);
647 break;
648
649 case Instruction::NEG_DOUBLE:
650 EmitInsn_FNeg(ARGS, kDouble);
651 break;
652
653 case Instruction::INT_TO_LONG:
654 EmitInsn_SExt(ARGS);
655 break;
656
657 case Instruction::INT_TO_FLOAT:
658 EmitInsn_IntToFP(ARGS, kInt, kFloat);
659 break;
660
661 case Instruction::INT_TO_DOUBLE:
662 EmitInsn_IntToFP(ARGS, kInt, kDouble);
663 break;
664
665 case Instruction::LONG_TO_INT:
666 EmitInsn_Trunc(ARGS);
667 break;
668
669 case Instruction::LONG_TO_FLOAT:
670 EmitInsn_IntToFP(ARGS, kLong, kFloat);
671 break;
672
673 case Instruction::LONG_TO_DOUBLE:
674 EmitInsn_IntToFP(ARGS, kLong, kDouble);
675 break;
676
677 case Instruction::FLOAT_TO_INT:
678 EmitInsn_FPToInt(ARGS, kFloat, kInt);
679 break;
680
681 case Instruction::FLOAT_TO_LONG:
682 EmitInsn_FPToInt(ARGS, kFloat, kLong);
683 break;
684
685 case Instruction::FLOAT_TO_DOUBLE:
686 EmitInsn_FExt(ARGS);
687 break;
688
689 case Instruction::DOUBLE_TO_INT:
690 EmitInsn_FPToInt(ARGS, kDouble, kInt);
691 break;
692
693 case Instruction::DOUBLE_TO_LONG:
694 EmitInsn_FPToInt(ARGS, kDouble, kLong);
695 break;
696
697 case Instruction::DOUBLE_TO_FLOAT:
698 EmitInsn_FTrunc(ARGS);
699 break;
700
701 case Instruction::INT_TO_BYTE:
702 EmitInsn_TruncAndSExt(ARGS, 8);
703 break;
704
705 case Instruction::INT_TO_CHAR:
706 EmitInsn_TruncAndZExt(ARGS, 16);
707 break;
708
709 case Instruction::INT_TO_SHORT:
710 EmitInsn_TruncAndSExt(ARGS, 16);
711 break;
712
713 case Instruction::ADD_INT:
714 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kInt, false);
715 break;
716
717 case Instruction::SUB_INT:
718 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kInt, false);
719 break;
720
721 case Instruction::MUL_INT:
722 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kInt, false);
723 break;
724
725 case Instruction::DIV_INT:
726 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kInt, false);
727 break;
728
729 case Instruction::REM_INT:
730 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kInt, false);
731 break;
732
733 case Instruction::AND_INT:
734 EmitInsn_IntArithm(ARGS, kIntArithm_And, kInt, false);
735 break;
736
737 case Instruction::OR_INT:
738 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kInt, false);
739 break;
740
741 case Instruction::XOR_INT:
742 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kInt, false);
743 break;
744
745 case Instruction::SHL_INT:
746 EmitInsn_IntArithm(ARGS, kIntArithm_Shl, kInt, false);
747 break;
748
749 case Instruction::SHR_INT:
750 EmitInsn_IntArithm(ARGS, kIntArithm_Shr, kInt, false);
751 break;
752
753 case Instruction::USHR_INT:
754 EmitInsn_IntArithm(ARGS, kIntArithm_UShr, kInt, false);
755 break;
756
757 case Instruction::ADD_LONG:
758 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kLong, false);
759 break;
760
761 case Instruction::SUB_LONG:
762 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kLong, false);
763 break;
764
765 case Instruction::MUL_LONG:
766 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kLong, false);
767 break;
768
769 case Instruction::DIV_LONG:
770 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kLong, false);
771 break;
772
773 case Instruction::REM_LONG:
774 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kLong, false);
775 break;
776
777 case Instruction::AND_LONG:
778 EmitInsn_IntArithm(ARGS, kIntArithm_And, kLong, false);
779 break;
780
781 case Instruction::OR_LONG:
782 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kLong, false);
783 break;
784
785 case Instruction::XOR_LONG:
786 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kLong, false);
787 break;
788
789 case Instruction::SHL_LONG:
790 EmitInsn_IntArithm(ARGS, kIntArithm_Shl, kLong, false);
791 break;
792
793 case Instruction::SHR_LONG:
794 EmitInsn_IntArithm(ARGS, kIntArithm_Shr, kLong, false);
795 break;
796
797 case Instruction::USHR_LONG:
798 EmitInsn_IntArithm(ARGS, kIntArithm_UShr, kLong, false);
799 break;
800
801 case Instruction::ADD_FLOAT:
802 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kFloat, false);
803 break;
804
805 case Instruction::SUB_FLOAT:
806 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kFloat, false);
807 break;
808
809 case Instruction::MUL_FLOAT:
810 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kFloat, false);
811 break;
812
813 case Instruction::DIV_FLOAT:
814 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kFloat, false);
815 break;
816
817 case Instruction::REM_FLOAT:
818 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kFloat, false);
819 break;
820
821 case Instruction::ADD_DOUBLE:
822 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kDouble, false);
823 break;
824
825 case Instruction::SUB_DOUBLE:
826 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kDouble, false);
827 break;
828
829 case Instruction::MUL_DOUBLE:
830 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kDouble, false);
831 break;
832
833 case Instruction::DIV_DOUBLE:
834 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kDouble, false);
835 break;
836
837 case Instruction::REM_DOUBLE:
838 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kDouble, false);
839 break;
840
841 case Instruction::ADD_INT_2ADDR:
842 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kInt, true);
843 break;
844
845 case Instruction::SUB_INT_2ADDR:
846 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kInt, true);
847 break;
848
849 case Instruction::MUL_INT_2ADDR:
850 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kInt, true);
851 break;
852
853 case Instruction::DIV_INT_2ADDR:
854 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kInt, true);
855 break;
856
857 case Instruction::REM_INT_2ADDR:
858 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kInt, true);
859 break;
860
861 case Instruction::AND_INT_2ADDR:
862 EmitInsn_IntArithm(ARGS, kIntArithm_And, kInt, true);
863 break;
864
865 case Instruction::OR_INT_2ADDR:
866 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kInt, true);
867 break;
868
869 case Instruction::XOR_INT_2ADDR:
870 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kInt, true);
871 break;
872
873 case Instruction::SHL_INT_2ADDR:
874 EmitInsn_IntArithm(ARGS, kIntArithm_Shl, kInt, true);
875 break;
876
877 case Instruction::SHR_INT_2ADDR:
878 EmitInsn_IntArithm(ARGS, kIntArithm_Shr, kInt, true);
879 break;
880
881 case Instruction::USHR_INT_2ADDR:
882 EmitInsn_IntArithm(ARGS, kIntArithm_UShr, kInt, true);
883 break;
884
885 case Instruction::ADD_LONG_2ADDR:
886 EmitInsn_IntArithm(ARGS, kIntArithm_Add, kLong, true);
887 break;
888
889 case Instruction::SUB_LONG_2ADDR:
890 EmitInsn_IntArithm(ARGS, kIntArithm_Sub, kLong, true);
891 break;
892
893 case Instruction::MUL_LONG_2ADDR:
894 EmitInsn_IntArithm(ARGS, kIntArithm_Mul, kLong, true);
895 break;
896
897 case Instruction::DIV_LONG_2ADDR:
898 EmitInsn_IntArithm(ARGS, kIntArithm_Div, kLong, true);
899 break;
900
901 case Instruction::REM_LONG_2ADDR:
902 EmitInsn_IntArithm(ARGS, kIntArithm_Rem, kLong, true);
903 break;
904
905 case Instruction::AND_LONG_2ADDR:
906 EmitInsn_IntArithm(ARGS, kIntArithm_And, kLong, true);
907 break;
908
909 case Instruction::OR_LONG_2ADDR:
910 EmitInsn_IntArithm(ARGS, kIntArithm_Or, kLong, true);
911 break;
912
913 case Instruction::XOR_LONG_2ADDR:
914 EmitInsn_IntArithm(ARGS, kIntArithm_Xor, kLong, true);
915 break;
916
917 case Instruction::SHL_LONG_2ADDR:
918 EmitInsn_IntArithm(ARGS, kIntArithm_Shl, kLong, true);
919 break;
920
921 case Instruction::SHR_LONG_2ADDR:
922 EmitInsn_IntArithm(ARGS, kIntArithm_Shr, kLong, true);
923 break;
924
925 case Instruction::USHR_LONG_2ADDR:
926 EmitInsn_IntArithm(ARGS, kIntArithm_UShr, kLong, true);
927 break;
928
929 case Instruction::ADD_FLOAT_2ADDR:
930 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kFloat, true);
931 break;
932
933 case Instruction::SUB_FLOAT_2ADDR:
934 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kFloat, true);
935 break;
936
937 case Instruction::MUL_FLOAT_2ADDR:
938 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kFloat, true);
939 break;
940
941 case Instruction::DIV_FLOAT_2ADDR:
942 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kFloat, true);
943 break;
944
945 case Instruction::REM_FLOAT_2ADDR:
946 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kFloat, true);
947 break;
948
949 case Instruction::ADD_DOUBLE_2ADDR:
950 EmitInsn_FPArithm(ARGS, kFPArithm_Add, kDouble, true);
951 break;
952
953 case Instruction::SUB_DOUBLE_2ADDR:
954 EmitInsn_FPArithm(ARGS, kFPArithm_Sub, kDouble, true);
955 break;
956
957 case Instruction::MUL_DOUBLE_2ADDR:
958 EmitInsn_FPArithm(ARGS, kFPArithm_Mul, kDouble, true);
959 break;
960
961 case Instruction::DIV_DOUBLE_2ADDR:
962 EmitInsn_FPArithm(ARGS, kFPArithm_Div, kDouble, true);
963 break;
964
965 case Instruction::REM_DOUBLE_2ADDR:
966 EmitInsn_FPArithm(ARGS, kFPArithm_Rem, kDouble, true);
967 break;
968
969 case Instruction::ADD_INT_LIT16:
970 case Instruction::ADD_INT_LIT8:
971 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Add);
972 break;
973
974 case Instruction::RSUB_INT:
975 case Instruction::RSUB_INT_LIT8:
976 EmitInsn_RSubImmediate(ARGS);
977 break;
978
979 case Instruction::MUL_INT_LIT16:
980 case Instruction::MUL_INT_LIT8:
981 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Mul);
982 break;
983
984 case Instruction::DIV_INT_LIT16:
985 case Instruction::DIV_INT_LIT8:
986 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Div);
987 break;
988
989 case Instruction::REM_INT_LIT16:
990 case Instruction::REM_INT_LIT8:
991 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Rem);
992 break;
993
994 case Instruction::AND_INT_LIT16:
995 case Instruction::AND_INT_LIT8:
996 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_And);
997 break;
998
999 case Instruction::OR_INT_LIT16:
1000 case Instruction::OR_INT_LIT8:
1001 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Or);
1002 break;
1003
1004 case Instruction::XOR_INT_LIT16:
1005 case Instruction::XOR_INT_LIT8:
1006 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Xor);
1007 break;
1008
1009 case Instruction::SHL_INT_LIT8:
1010 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Shl);
1011 break;
1012
1013 case Instruction::SHR_INT_LIT8:
1014 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_Shr);
1015 break;
1016
1017 case Instruction::USHR_INT_LIT8:
1018 EmitInsn_IntArithmImmediate(ARGS, kIntArithm_UShr);
1019 break;
1020
1021 case Instruction::UNUSED_3E:
1022 case Instruction::UNUSED_3F:
1023 case Instruction::UNUSED_40:
1024 case Instruction::UNUSED_41:
1025 case Instruction::UNUSED_42:
1026 case Instruction::UNUSED_43:
1027 case Instruction::UNUSED_73:
1028 case Instruction::UNUSED_79:
1029 case Instruction::UNUSED_7A:
1030 case Instruction::UNUSED_E3:
1031 case Instruction::UNUSED_E4:
1032 case Instruction::UNUSED_E5:
1033 case Instruction::UNUSED_E6:
1034 case Instruction::UNUSED_E7:
1035 case Instruction::UNUSED_E8:
1036 case Instruction::UNUSED_E9:
1037 case Instruction::UNUSED_EA:
1038 case Instruction::UNUSED_EB:
1039 case Instruction::UNUSED_EC:
1040 case Instruction::THROW_VERIFICATION_ERROR:
1041 case Instruction::UNUSED_EE:
1042 case Instruction::UNUSED_EF:
1043 case Instruction::UNUSED_F0:
1044 case Instruction::UNUSED_F1:
1045 case Instruction::UNUSED_F2:
1046 case Instruction::UNUSED_F3:
1047 case Instruction::UNUSED_F4:
1048 case Instruction::UNUSED_F5:
1049 case Instruction::UNUSED_F6:
1050 case Instruction::UNUSED_F7:
1051 case Instruction::UNUSED_F8:
1052 case Instruction::UNUSED_F9:
1053 case Instruction::UNUSED_FA:
1054 case Instruction::UNUSED_FB:
1055 case Instruction::UNUSED_FC:
1056 case Instruction::UNUSED_FD:
1057 case Instruction::UNUSED_FE:
1058 case Instruction::UNUSED_FF:
1059 LOG(FATAL) << "Dex file contains UNUSED bytecode: " << insn->Opcode();
1060 break;
1061 }
1062
1063#undef ARGS
1064}
1065
1066
1067void MethodCompiler::EmitInsn_Nop(uint32_t dex_pc,
1068 Instruction const* insn) {
Logan Chiene09a6b72011-12-27 17:50:21 +08001069
1070 uint16_t insn_signature = code_item_->insns_[dex_pc];
1071
1072 if (insn_signature == Instruction::kPackedSwitchSignature ||
1073 insn_signature == Instruction::kSparseSwitchSignature ||
1074 insn_signature == Instruction::kArrayDataSignature) {
1075 irb_.CreateUnreachable();
1076 } else{
1077 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1078 }
Shih-wei Liaod1fec812012-02-13 09:51:10 -08001079}
1080
1081
Logan Chien70f94b42011-12-27 17:49:11 +08001082void MethodCompiler::EmitInsn_Move(uint32_t dex_pc,
1083 Instruction const* insn,
1084 JType jty) {
Logan Chien48173132011-12-27 17:51:13 +08001085
1086 Instruction::DecodedInstruction dec_insn(insn);
1087
1088 llvm::Value* src_value = EmitLoadDalvikReg(dec_insn.vB_, jty, kReg);
1089 EmitStoreDalvikReg(dec_insn.vA_, jty, kReg, src_value);
1090
Logan Chien70f94b42011-12-27 17:49:11 +08001091 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1092}
1093
1094
1095void MethodCompiler::EmitInsn_MoveResult(uint32_t dex_pc,
1096 Instruction const* insn,
1097 JType jty) {
Logan Chien48173132011-12-27 17:51:13 +08001098
1099 Instruction::DecodedInstruction dec_insn(insn);
1100
1101 llvm::Value* src_value = EmitLoadDalvikRetValReg(jty, kReg);
1102 EmitStoreDalvikReg(dec_insn.vA_, jty, kReg, src_value);
1103
Logan Chien70f94b42011-12-27 17:49:11 +08001104 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1105}
1106
1107
1108void MethodCompiler::EmitInsn_MoveException(uint32_t dex_pc,
1109 Instruction const* insn) {
1110 // UNIMPLEMENTED(WARNING);
1111 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1112}
1113
1114
1115void MethodCompiler::EmitInsn_ThrowException(uint32_t dex_pc,
1116 Instruction const* insn) {
1117 // UNIMPLEMENTED(WARNING);
1118 irb_.CreateUnreachable();
1119}
1120
1121
1122void MethodCompiler::EmitInsn_ReturnVoid(uint32_t dex_pc,
1123 Instruction const* insn) {
1124 // UNIMPLEMENTED(WARNING);
1125 irb_.CreateUnreachable();
1126}
1127
1128
1129void MethodCompiler::EmitInsn_Return(uint32_t dex_pc,
1130 Instruction const* insn) {
1131 // UNIMPLEMENTED(WARNING);
1132 irb_.CreateUnreachable();
1133}
1134
1135
1136void MethodCompiler::EmitInsn_LoadConstant(uint32_t dex_pc,
1137 Instruction const* insn,
1138 JType imm_jty) {
1139 // UNIMPLEMENTED(WARNING);
1140 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1141}
1142
1143
1144void MethodCompiler::EmitInsn_LoadConstantString(uint32_t dex_pc,
1145 Instruction const* insn) {
1146 // UNIMPLEMENTED(WARNING);
1147 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1148}
1149
1150
1151void MethodCompiler::EmitInsn_LoadConstantClass(uint32_t dex_pc,
1152 Instruction const* insn) {
1153 // UNIMPLEMENTED(WARNING);
1154 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1155}
1156
1157
1158void MethodCompiler::EmitInsn_MonitorEnter(uint32_t dex_pc,
1159 Instruction const* insn) {
1160 // UNIMPLEMENTED(WARNING);
1161 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1162}
1163
1164
1165void MethodCompiler::EmitInsn_MonitorExit(uint32_t dex_pc,
1166 Instruction const* insn) {
1167 // UNIMPLEMENTED(WARNING);
1168 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1169}
1170
1171
1172void MethodCompiler::EmitInsn_CheckCast(uint32_t dex_pc,
1173 Instruction const* insn) {
1174 // UNIMPLEMENTED(WARNING);
1175 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1176}
1177
1178
1179void MethodCompiler::EmitInsn_InstanceOf(uint32_t dex_pc,
1180 Instruction const* insn) {
1181 // UNIMPLEMENTED(WARNING);
1182 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1183}
1184
1185
1186void MethodCompiler::EmitInsn_ArrayLength(uint32_t dex_pc,
1187 Instruction const* insn) {
1188 // UNIMPLEMENTED(WARNING);
1189 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1190}
1191
1192
1193void MethodCompiler::EmitInsn_NewInstance(uint32_t dex_pc,
1194 Instruction const* insn) {
1195 // UNIMPLEMENTED(WARNING);
1196 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1197}
1198
1199
1200void MethodCompiler::EmitInsn_NewArray(uint32_t dex_pc,
1201 Instruction const* insn) {
1202 // UNIMPLEMENTED(WARNING);
1203 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1204}
1205
1206
1207void MethodCompiler::EmitInsn_FilledNewArray(uint32_t dex_pc,
1208 Instruction const* insn,
1209 bool is_range) {
1210 // UNIMPLEMENTED(WARNING);
1211 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1212}
1213
1214
1215void MethodCompiler::EmitInsn_FillArrayData(uint32_t dex_pc,
1216 Instruction const* insn) {
1217 // UNIMPLEMENTED(WARNING);
1218 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1219}
1220
1221
1222void MethodCompiler::EmitInsn_UnconditionalBranch(uint32_t dex_pc,
1223 Instruction const* insn) {
1224 // UNIMPLEMENTED(WARNING);
1225 irb_.CreateUnreachable();
1226}
1227
1228
1229void MethodCompiler::EmitInsn_PackedSwitch(uint32_t dex_pc,
1230 Instruction const* insn) {
1231 // UNIMPLEMENTED(WARNING);
1232 irb_.CreateUnreachable();
1233}
1234
1235
1236void MethodCompiler::EmitInsn_SparseSwitch(uint32_t dex_pc,
1237 Instruction const* insn) {
1238 // UNIMPLEMENTED(WARNING);
1239 irb_.CreateUnreachable();
1240}
1241
1242
1243void MethodCompiler::EmitInsn_FPCompare(uint32_t dex_pc,
1244 Instruction const* insn,
1245 JType fp_jty,
1246 bool gt_bias) {
1247 // UNIMPLEMENTED(WARNING);
1248 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1249}
1250
1251
1252void MethodCompiler::EmitInsn_LongCompare(uint32_t dex_pc,
1253 Instruction const* insn) {
1254 // UNIMPLEMENTED(WARNING);
1255 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1256}
1257
1258
1259void MethodCompiler::EmitInsn_BinaryConditionalBranch(uint32_t dex_pc,
1260 Instruction const* insn,
1261 CondBranchKind cond) {
1262 // UNIMPLEMENTED(WARNING);
1263 irb_.CreateUnreachable();
1264}
1265
1266
1267void MethodCompiler::EmitInsn_UnaryConditionalBranch(uint32_t dex_pc,
1268 Instruction const* insn,
1269 CondBranchKind cond) {
1270 // UNIMPLEMENTED(WARNING);
1271 irb_.CreateUnreachable();
1272}
1273
1274
1275void MethodCompiler::EmitInsn_AGet(uint32_t dex_pc,
1276 Instruction const* insn,
1277 JType elem_jty) {
1278 // UNIMPLEMENTED(WARNING);
1279 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1280}
1281
1282
1283void MethodCompiler::EmitInsn_APut(uint32_t dex_pc,
1284 Instruction const* insn,
1285 JType elem_jty) {
1286 // UNIMPLEMENTED(WARNING);
1287 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1288}
1289
1290
1291void MethodCompiler::EmitInsn_IGet(uint32_t dex_pc,
1292 Instruction const* insn,
1293 JType field_jty) {
1294 // UNIMPLEMENTED(WARNING);
1295 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1296}
1297
1298
1299void MethodCompiler::EmitInsn_IPut(uint32_t dex_pc,
1300 Instruction const* insn,
1301 JType field_jty) {
1302 // UNIMPLEMENTED(WARNING);
1303 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1304}
1305
1306
1307void MethodCompiler::EmitInsn_SGet(uint32_t dex_pc,
1308 Instruction const* insn,
1309 JType field_jty) {
1310 // UNIMPLEMENTED(WARNING);
1311 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1312}
1313
1314
1315void MethodCompiler::EmitInsn_SPut(uint32_t dex_pc,
1316 Instruction const* insn,
1317 JType field_jty) {
1318 // UNIMPLEMENTED(WARNING);
1319 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1320}
1321
1322
1323void MethodCompiler::EmitInsn_InvokeVirtual(uint32_t dex_pc,
1324 Instruction const* insn,
1325 bool is_range) {
1326 // UNIMPLEMENTED(WARNING);
1327 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1328}
1329
1330
1331void MethodCompiler::EmitInsn_InvokeSuper(uint32_t dex_pc,
1332 Instruction const* insn,
1333 bool is_range) {
1334 // UNIMPLEMENTED(WARNING);
1335 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1336}
1337
1338
1339void MethodCompiler::EmitInsn_InvokeDirect(uint32_t dex_pc,
1340 Instruction const* insn,
1341 bool is_range) {
1342 // UNIMPLEMENTED(WARNING);
1343 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1344}
1345
1346
1347void MethodCompiler::EmitInsn_InvokeStatic(uint32_t dex_pc,
1348 Instruction const* insn,
1349 bool is_range) {
1350 // UNIMPLEMENTED(WARNING);
1351 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1352}
1353
1354
1355void MethodCompiler::EmitInsn_InvokeInterface(uint32_t dex_pc,
1356 Instruction const* insn,
1357 bool is_range) {
1358 // UNIMPLEMENTED(WARNING);
1359 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1360}
1361
1362
1363void MethodCompiler::EmitInsn_Neg(uint32_t dex_pc,
1364 Instruction const* insn,
1365 JType op_jty) {
1366 // UNIMPLEMENTED(WARNING);
1367 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1368}
1369
1370
1371void MethodCompiler::EmitInsn_Not(uint32_t dex_pc,
1372 Instruction const* insn,
1373 JType op_jty) {
1374 // UNIMPLEMENTED(WARNING);
1375 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1376}
1377
1378
1379void MethodCompiler::EmitInsn_SExt(uint32_t dex_pc,
1380 Instruction const* insn) {
1381 // UNIMPLEMENTED(WARNING);
1382 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1383}
1384
1385
1386void MethodCompiler::EmitInsn_Trunc(uint32_t dex_pc,
1387 Instruction const* insn) {
1388 // UNIMPLEMENTED(WARNING);
1389 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1390}
1391
1392
1393void MethodCompiler::EmitInsn_TruncAndSExt(uint32_t dex_pc,
1394 Instruction const* insn,
1395 unsigned N) {
1396 // UNIMPLEMENTED(WARNING);
1397 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1398}
1399
1400
1401void MethodCompiler::EmitInsn_TruncAndZExt(uint32_t dex_pc,
1402 Instruction const* insn,
1403 unsigned N) {
1404 // UNIMPLEMENTED(WARNING);
1405 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1406}
1407
1408
1409void MethodCompiler::EmitInsn_FNeg(uint32_t dex_pc,
1410 Instruction const* insn,
1411 JType op_jty) {
1412 // UNIMPLEMENTED(WARNING);
1413 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1414}
1415
1416
1417void MethodCompiler::EmitInsn_IntToFP(uint32_t dex_pc,
1418 Instruction const* insn,
1419 JType src_jty,
1420 JType dest_jty) {
1421 // UNIMPLEMENTED(WARNING);
1422 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1423}
1424
1425
1426void MethodCompiler::EmitInsn_FPToInt(uint32_t dex_pc,
1427 Instruction const* insn,
1428 JType src_jty,
1429 JType dest_jty) {
1430 // UNIMPLEMENTED(WARNING);
1431 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1432}
1433
1434
1435void MethodCompiler::EmitInsn_FExt(uint32_t dex_pc,
1436 Instruction const* insn) {
1437 // UNIMPLEMENTED(WARNING);
1438 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1439}
1440
1441
1442void MethodCompiler::EmitInsn_FTrunc(uint32_t dex_pc,
1443 Instruction const* insn) {
1444 // UNIMPLEMENTED(WARNING);
1445 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1446}
1447
1448
1449void MethodCompiler::EmitInsn_IntArithm(uint32_t dex_pc,
1450 Instruction const* insn,
1451 IntArithmKind arithm,
1452 JType op_jty,
1453 bool is_2addr) {
1454 // UNIMPLEMENTED(WARNING);
1455 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1456}
1457
1458
1459void MethodCompiler::EmitInsn_IntArithmImmediate(uint32_t dex_pc,
1460 Instruction const* insn,
1461 IntArithmKind arithm) {
1462 // UNIMPLEMENTED(WARNING);
1463 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1464}
1465
1466
1467void MethodCompiler::EmitInsn_RSubImmediate(uint32_t dex_pc,
1468 Instruction const* insn) {
1469 // UNIMPLEMENTED(WARNING);
1470 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1471}
1472
1473
1474void MethodCompiler::EmitInsn_FPArithm(uint32_t dex_pc,
1475 Instruction const* insn,
1476 FPArithmKind arithm,
1477 JType op_jty,
1478 bool is_2addr) {
1479 // UNIMPLEMENTED(WARNING);
1480 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1481}
1482
1483
Logan Chien83426162011-12-09 09:29:50 +08001484CompiledMethod *MethodCompiler::Compile() {
Logan Chien0b827102011-12-20 19:46:14 +08001485 // Code generation
1486 CreateFunction();
1487
1488 EmitPrologue();
1489 EmitInstructions();
Logan Chienc670a8d2011-12-20 21:25:56 +08001490 EmitPrologueLastBranch();
Logan Chien0b827102011-12-20 19:46:14 +08001491
Logan Chiend6c239a2011-12-23 15:11:45 +08001492 // Verify the generated bitcode
1493 llvm::verifyFunction(*func_, llvm::PrintMessageAction);
1494
Logan Chien0b827102011-12-20 19:46:14 +08001495 // Delete the inferred register category map (won't be used anymore)
1496 method_->ResetInferredRegCategoryMap();
1497
1498 return new CompiledMethod(insn_set_, func_);
1499}
1500
1501
1502llvm::Value* MethodCompiler::EmitLoadMethodObjectAddr() {
1503 return func_->arg_begin();
Shih-wei Liaod1fec812012-02-13 09:51:10 -08001504}
Logan Chien83426162011-12-09 09:29:50 +08001505
1506
Logan Chien5bcc04e2012-01-30 14:15:12 +08001507void MethodCompiler::EmitBranchExceptionLandingPad(uint32_t dex_pc) {
1508 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
1509 irb_.CreateBr(lpad);
1510 } else {
1511 irb_.CreateBr(GetUnwindBasicBlock());
1512 }
1513}
1514
1515
1516void MethodCompiler::EmitGuard_ExceptionLandingPad(uint32_t dex_pc) {
1517 llvm::Value* exception_pending =
1518 irb_.CreateCall(irb_.GetRuntime(IsExceptionPending));
1519
1520 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
1521
1522 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
1523 irb_.CreateCondBr(exception_pending, lpad, block_cont);
1524 } else {
1525 irb_.CreateCondBr(exception_pending, GetUnwindBasicBlock(), block_cont);
1526 }
1527
1528 irb_.SetInsertPoint(block_cont);
1529}
1530
1531
Logan Chien924072f2012-01-30 15:07:24 +08001532void MethodCompiler::EmitGuard_GarbageCollectionSuspend(uint32_t dex_pc) {
1533 llvm::Value* runtime_func = irb_.GetRuntime(TestSuspend);
1534 irb_.CreateCall(runtime_func);
1535
1536 EmitGuard_ExceptionLandingPad(dex_pc);
1537}
1538
1539
Logan Chiend6c239a2011-12-23 15:11:45 +08001540llvm::BasicBlock* MethodCompiler::
1541CreateBasicBlockWithDexPC(uint32_t dex_pc, char const* postfix) {
1542 std::string name;
1543
1544 if (postfix) {
1545 StringAppendF(&name, "B%u.%s", dex_pc, postfix);
1546 } else {
1547 StringAppendF(&name, "B%u", dex_pc);
1548 }
1549
1550 return llvm::BasicBlock::Create(*context_, name, func_);
1551}
1552
1553
1554llvm::BasicBlock* MethodCompiler::GetBasicBlock(uint32_t dex_pc) {
1555 DCHECK(dex_pc < code_item_->insns_size_in_code_units_);
1556
1557 llvm::BasicBlock* basic_block = basic_blocks_[dex_pc];
1558
1559 if (!basic_block) {
1560 basic_block = CreateBasicBlockWithDexPC(dex_pc);
1561 basic_blocks_[dex_pc] = basic_block;
1562 }
1563
1564 return basic_block;
1565}
1566
1567
1568llvm::BasicBlock*
1569MethodCompiler::GetNextBasicBlock(uint32_t dex_pc) {
1570 Instruction const* insn = Instruction::At(code_item_->insns_ + dex_pc);
1571 return GetBasicBlock(dex_pc + insn->SizeInCodeUnits());
1572}
1573
1574
Logan Chien5bcc04e2012-01-30 14:15:12 +08001575int32_t MethodCompiler::GetTryItemOffset(uint32_t dex_pc) {
1576 // TODO: Since we are emitting the dex instructions in ascending order
1577 // w.r.t. address, we can cache the lastest try item offset so that we
1578 // don't have to do binary search for every query.
1579
1580 int32_t min = 0;
1581 int32_t max = code_item_->tries_size_ - 1;
1582
1583 while (min <= max) {
1584 int32_t mid = min + (max - min) / 2;
1585
1586 DexFile::TryItem const* ti = DexFile::GetTryItems(*code_item_, mid);
1587 uint32_t start = ti->start_addr_;
1588 uint32_t end = start + ti->insn_count_;
1589
1590 if (dex_pc < start) {
1591 max = mid - 1;
1592 } else if (dex_pc >= end) {
1593 min = mid + 1;
1594 } else {
1595 return mid; // found
1596 }
1597 }
1598
1599 return -1; // not found
1600}
1601
1602
1603llvm::BasicBlock* MethodCompiler::GetLandingPadBasicBlock(uint32_t dex_pc) {
1604 // Find the try item for this address in this method
1605 int32_t ti_offset = GetTryItemOffset(dex_pc);
1606
1607 if (ti_offset == -1) {
1608 return NULL; // No landing pad is available for this address.
1609 }
1610
1611 // Check for the existing landing pad basic block
1612 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
1613 llvm::BasicBlock* block_lpad = basic_block_landing_pads_[ti_offset];
1614
1615 if (block_lpad) {
1616 // We have generated landing pad for this try item already. Return the
1617 // same basic block.
1618 return block_lpad;
1619 }
1620
1621 // Get try item from code item
1622 DexFile::TryItem const* ti = DexFile::GetTryItems(*code_item_, ti_offset);
1623
1624 // Create landing pad basic block
1625 block_lpad = llvm::BasicBlock::Create(*context_,
1626 StringPrintf("lpad%d", ti_offset),
1627 func_);
1628
1629 // Change IRBuilder insert point
1630 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
1631 irb_.SetInsertPoint(block_lpad);
1632
1633 // Find catch block with matching type
1634 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1635
1636 // TODO: Maybe passing try item offset will be a better idea? For now,
1637 // we are passing dex_pc, so that we can use existing runtime support
1638 // function directly. However, in the runtime supporting function we
1639 // have to search for try item with binary search which can be
1640 // eliminated.
1641 llvm::Value* dex_pc_value = irb_.getInt32(ti->start_addr_);
1642
1643 llvm::Value* catch_handler_index_value =
1644 irb_.CreateCall2(irb_.GetRuntime(FindCatchBlock),
1645 method_object_addr, dex_pc_value);
1646
1647 // Switch instruction (Go to unwind basic block by default)
1648 llvm::SwitchInst* sw =
1649 irb_.CreateSwitch(catch_handler_index_value, GetUnwindBasicBlock());
1650
1651 // Cases with matched catch block
1652 CatchHandlerIterator iter(*code_item_, ti->start_addr_);
1653
1654 for (uint32_t c = 0; iter.HasNext(); iter.Next(), ++c) {
1655 sw->addCase(irb_.getInt32(c), GetBasicBlock(iter.GetHandlerAddress()));
1656 }
1657
1658 // Restore the orignal insert point for IRBuilder
1659 irb_.restoreIP(irb_ip_original);
1660
1661 // Cache this landing pad
1662 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
1663 basic_block_landing_pads_[ti_offset] = block_lpad;
1664
1665 return block_lpad;
1666}
1667
1668
1669llvm::BasicBlock* MethodCompiler::GetUnwindBasicBlock() {
1670 // Check the existing unwinding baisc block block
1671 if (basic_block_unwind_ != NULL) {
1672 return basic_block_unwind_;
1673 }
1674
1675 // Create new basic block for unwinding
1676 basic_block_unwind_ =
1677 llvm::BasicBlock::Create(*context_, "exception_unwind", func_);
1678
1679 // Change IRBuilder insert point
1680 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
1681 irb_.SetInsertPoint(basic_block_unwind_);
1682
1683 // Emit the code to return default value (zero) for the given return type.
1684 char ret_shorty = method_helper_.GetShorty()[0];
1685 if (ret_shorty == 'V') {
1686 irb_.CreateRetVoid();
1687 } else {
1688 irb_.CreateRet(irb_.getJZero(ret_shorty));
1689 }
1690
1691 // Restore the orignal insert point for IRBuilder
1692 irb_.restoreIP(irb_ip_original);
1693
1694 return basic_block_unwind_;
1695}
1696
1697
Logan Chienc670a8d2011-12-20 21:25:56 +08001698llvm::Value* MethodCompiler::AllocDalvikLocalVarReg(RegCategory cat,
1699 uint32_t reg_idx) {
1700
1701 // Save current IR builder insert point
1702 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
1703
1704 // Alloca
1705 llvm::Value* reg_addr = NULL;
1706
1707 switch (cat) {
1708 case kRegCat1nr:
1709 irb_.SetInsertPoint(basic_block_reg_alloca_);
1710 reg_addr = irb_.CreateAlloca(irb_.getJIntTy(), 0,
1711 StringPrintf("r%u", reg_idx));
1712
1713 irb_.SetInsertPoint(basic_block_reg_zero_init_);
1714 irb_.CreateStore(irb_.getJInt(0), reg_addr);
1715 break;
1716
1717 case kRegCat2:
1718 irb_.SetInsertPoint(basic_block_reg_alloca_);
1719 reg_addr = irb_.CreateAlloca(irb_.getJLongTy(), 0,
1720 StringPrintf("w%u", reg_idx));
1721
1722 irb_.SetInsertPoint(basic_block_reg_zero_init_);
1723 irb_.CreateStore(irb_.getJLong(0), reg_addr);
1724 break;
1725
1726 case kRegObject:
1727 irb_.SetInsertPoint(basic_block_reg_alloca_);
1728 reg_addr = irb_.CreateAlloca(irb_.getJObjectTy(), 0,
1729 StringPrintf("p%u", reg_idx));
1730
1731 irb_.SetInsertPoint(basic_block_reg_zero_init_);
1732 irb_.CreateStore(irb_.getJNull(), reg_addr);
1733 break;
1734
1735 default:
1736 LOG(FATAL) << "Unknown register category for allocation: " << cat;
1737 }
1738
1739 // Restore IRBuilder insert point
1740 irb_.restoreIP(irb_ip_original);
1741
1742 DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL));
1743 return reg_addr;
1744}
1745
1746
1747llvm::Value* MethodCompiler::AllocDalvikRetValReg(RegCategory cat) {
1748 // Save current IR builder insert point
1749 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
1750
1751 // Alloca
1752 llvm::Value* reg_addr = NULL;
1753
1754 switch (cat) {
1755 case kRegCat1nr:
1756 irb_.SetInsertPoint(basic_block_reg_alloca_);
1757 reg_addr = irb_.CreateAlloca(irb_.getJIntTy(), 0, "r_res");
1758 break;
1759
1760 case kRegCat2:
1761 irb_.SetInsertPoint(basic_block_reg_alloca_);
1762 reg_addr = irb_.CreateAlloca(irb_.getJLongTy(), 0, "w_res");
1763 break;
1764
1765 case kRegObject:
1766 irb_.SetInsertPoint(basic_block_reg_alloca_);
1767 reg_addr = irb_.CreateAlloca(irb_.getJObjectTy(), 0, "p_res");
1768 break;
1769
1770 default:
1771 LOG(FATAL) << "Unknown register category for allocation: " << cat;
1772 }
1773
1774 // Restore IRBuilder insert point
1775 irb_.restoreIP(irb_ip_original);
1776
1777 DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL));
1778 return reg_addr;
1779}
1780
1781
Logan Chien83426162011-12-09 09:29:50 +08001782} // namespace compiler_llvm
1783} // namespace art