blob: 04a549b78927d49e47fc26642393f9d5929598d4 [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) {
Logan Chien8898a272011-12-27 17:51:56 +08001124 // Garbage collection safe-point
1125 EmitGuard_GarbageCollectionSuspend(dex_pc);
1126
1127 // Return!
1128 irb_.CreateRetVoid();
Logan Chien70f94b42011-12-27 17:49:11 +08001129}
1130
1131
1132void MethodCompiler::EmitInsn_Return(uint32_t dex_pc,
1133 Instruction const* insn) {
Logan Chien8898a272011-12-27 17:51:56 +08001134
1135 Instruction::DecodedInstruction dec_insn(insn);
1136
1137 // Garbage collection safe-point
1138 EmitGuard_GarbageCollectionSuspend(dex_pc);
1139
1140 // Return!
1141 char ret_shorty = method_helper_.GetShorty()[0];
1142 llvm::Value* retval = EmitLoadDalvikReg(dec_insn.vA_, ret_shorty, kAccurate);
1143
1144 irb_.CreateRet(retval);
Logan Chien70f94b42011-12-27 17:49:11 +08001145}
1146
1147
1148void MethodCompiler::EmitInsn_LoadConstant(uint32_t dex_pc,
1149 Instruction const* insn,
1150 JType imm_jty) {
Shih-wei Liao798366e2012-02-16 09:25:33 -08001151
1152 Instruction::DecodedInstruction dec_insn(insn);
1153
1154 DCHECK(imm_jty == kInt || imm_jty == kLong) << imm_jty;
1155
1156 int64_t imm = 0;
1157
1158 switch (insn->Opcode()) {
1159 // 32-bit Immediate
1160 case Instruction::CONST_4:
1161 case Instruction::CONST_16:
1162 case Instruction::CONST:
1163 case Instruction::CONST_WIDE_16:
1164 case Instruction::CONST_WIDE_32:
1165 imm = static_cast<int64_t>(static_cast<int32_t>(dec_insn.vB_));
1166 break;
1167
1168 case Instruction::CONST_HIGH16:
1169 imm = static_cast<int64_t>(static_cast<int32_t>(
1170 static_cast<uint32_t>(static_cast<uint16_t>(dec_insn.vB_)) << 16));
1171 break;
1172
1173 // 64-bit Immediate
1174 case Instruction::CONST_WIDE:
1175 imm = static_cast<int64_t>(dec_insn.vB_wide_);
1176 break;
1177
1178 case Instruction::CONST_WIDE_HIGH16:
1179 imm = static_cast<int64_t>(
1180 static_cast<uint64_t>(static_cast<uint16_t>(dec_insn.vB_)) << 48);
1181 break;
1182
1183 // Unknown opcode for load constant (unreachable)
1184 default:
1185 LOG(FATAL) << "Unknown opcode for load constant: " << insn->Opcode();
1186 break;
1187 }
1188
1189 // Store the non-object register
1190 llvm::Type* imm_type = irb_.getJType(imm_jty, kAccurate);
1191 llvm::Constant* imm_value = llvm::ConstantInt::getSigned(imm_type, imm);
1192 EmitStoreDalvikReg(dec_insn.vA_, imm_jty, kAccurate, imm_value);
1193
1194 // Store the object register if it is possible to be null.
1195 if (imm_jty == kInt && imm == 0) {
1196 EmitStoreDalvikReg(dec_insn.vA_, kObject, kAccurate, irb_.getJNull());
1197 }
1198
Logan Chien70f94b42011-12-27 17:49:11 +08001199 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1200}
1201
1202
1203void MethodCompiler::EmitInsn_LoadConstantString(uint32_t dex_pc,
1204 Instruction const* insn) {
1205 // UNIMPLEMENTED(WARNING);
1206 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1207}
1208
1209
1210void MethodCompiler::EmitInsn_LoadConstantClass(uint32_t dex_pc,
1211 Instruction const* insn) {
1212 // UNIMPLEMENTED(WARNING);
1213 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1214}
1215
1216
1217void MethodCompiler::EmitInsn_MonitorEnter(uint32_t dex_pc,
1218 Instruction const* insn) {
1219 // UNIMPLEMENTED(WARNING);
1220 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1221}
1222
1223
1224void MethodCompiler::EmitInsn_MonitorExit(uint32_t dex_pc,
1225 Instruction const* insn) {
1226 // UNIMPLEMENTED(WARNING);
1227 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1228}
1229
1230
1231void MethodCompiler::EmitInsn_CheckCast(uint32_t dex_pc,
1232 Instruction const* insn) {
1233 // UNIMPLEMENTED(WARNING);
1234 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1235}
1236
1237
1238void MethodCompiler::EmitInsn_InstanceOf(uint32_t dex_pc,
1239 Instruction const* insn) {
1240 // UNIMPLEMENTED(WARNING);
1241 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1242}
1243
1244
1245void MethodCompiler::EmitInsn_ArrayLength(uint32_t dex_pc,
1246 Instruction const* insn) {
1247 // UNIMPLEMENTED(WARNING);
1248 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1249}
1250
1251
1252void MethodCompiler::EmitInsn_NewInstance(uint32_t dex_pc,
1253 Instruction const* insn) {
1254 // UNIMPLEMENTED(WARNING);
1255 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1256}
1257
1258
1259void MethodCompiler::EmitInsn_NewArray(uint32_t dex_pc,
1260 Instruction const* insn) {
1261 // UNIMPLEMENTED(WARNING);
1262 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1263}
1264
1265
1266void MethodCompiler::EmitInsn_FilledNewArray(uint32_t dex_pc,
1267 Instruction const* insn,
1268 bool is_range) {
1269 // UNIMPLEMENTED(WARNING);
1270 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1271}
1272
1273
1274void MethodCompiler::EmitInsn_FillArrayData(uint32_t dex_pc,
1275 Instruction const* insn) {
1276 // UNIMPLEMENTED(WARNING);
1277 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1278}
1279
1280
1281void MethodCompiler::EmitInsn_UnconditionalBranch(uint32_t dex_pc,
1282 Instruction const* insn) {
Logan Chiena466c162011-12-27 17:55:46 +08001283
1284 Instruction::DecodedInstruction dec_insn(insn);
1285
1286 int32_t branch_offset = dec_insn.vA_;
1287
1288 if (branch_offset <= 0) {
1289 // Garbage collection safe-point on backward branch
1290 EmitGuard_GarbageCollectionSuspend(dex_pc);
1291 }
1292
1293 irb_.CreateBr(GetBasicBlock(dex_pc + branch_offset));
Logan Chien70f94b42011-12-27 17:49:11 +08001294}
1295
1296
1297void MethodCompiler::EmitInsn_PackedSwitch(uint32_t dex_pc,
1298 Instruction const* insn) {
Logan Chien7a89b6d2011-12-27 17:56:56 +08001299
1300 Instruction::DecodedInstruction dec_insn(insn);
1301
1302 struct PACKED Payload {
1303 uint16_t ident_;
1304 uint16_t num_cases_;
1305 int32_t first_key_;
1306 int32_t targets_[];
1307 };
1308
1309 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
1310 static_cast<int32_t>(dec_insn.vB_);
1311
1312 Payload const* payload =
1313 reinterpret_cast<Payload const*>(code_item_->insns_ + payload_offset);
1314
1315 llvm::Value* value = EmitLoadDalvikReg(dec_insn.vA_, kInt, kAccurate);
1316
1317 llvm::SwitchInst* sw =
1318 irb_.CreateSwitch(value, GetNextBasicBlock(dex_pc), payload->num_cases_);
1319
1320 for (uint16_t i = 0; i < payload->num_cases_; ++i) {
1321 sw->addCase(irb_.getInt32(payload->first_key_ + i),
1322 GetBasicBlock(dex_pc + payload->targets_[i]));
1323 }
Logan Chien70f94b42011-12-27 17:49:11 +08001324}
1325
1326
1327void MethodCompiler::EmitInsn_SparseSwitch(uint32_t dex_pc,
1328 Instruction const* insn) {
Logan Chien7a89b6d2011-12-27 17:56:56 +08001329
1330 Instruction::DecodedInstruction dec_insn(insn);
1331
1332 struct PACKED Payload {
1333 uint16_t ident_;
1334 uint16_t num_cases_;
1335 int32_t keys_and_targets_[];
1336 };
1337
1338 int32_t payload_offset = static_cast<int32_t>(dex_pc) +
1339 static_cast<int32_t>(dec_insn.vB_);
1340
1341 Payload const* payload =
1342 reinterpret_cast<Payload const*>(code_item_->insns_ + payload_offset);
1343
1344 int32_t const* keys = payload->keys_and_targets_;
1345 int32_t const* targets = payload->keys_and_targets_ + payload->num_cases_;
1346
1347 llvm::Value* value = EmitLoadDalvikReg(dec_insn.vA_, kInt, kAccurate);
1348
1349 llvm::SwitchInst* sw =
1350 irb_.CreateSwitch(value, GetNextBasicBlock(dex_pc), payload->num_cases_);
1351
1352 for (size_t i = 0; i < payload->num_cases_; ++i) {
1353 sw->addCase(irb_.getInt32(keys[i]), GetBasicBlock(dex_pc + targets[i]));
1354 }
Logan Chien70f94b42011-12-27 17:49:11 +08001355}
1356
1357
1358void MethodCompiler::EmitInsn_FPCompare(uint32_t dex_pc,
1359 Instruction const* insn,
1360 JType fp_jty,
1361 bool gt_bias) {
Logan Chien2c37e8e2011-12-27 17:58:46 +08001362
1363 Instruction::DecodedInstruction dec_insn(insn);
1364
1365 DCHECK(fp_jty == kFloat || fp_jty == kDouble) << "JType: " << fp_jty;
1366
1367 llvm::Value* src1_value = EmitLoadDalvikReg(dec_insn.vB_, fp_jty, kAccurate);
1368 llvm::Value* src2_value = EmitLoadDalvikReg(dec_insn.vC_, fp_jty, kAccurate);
1369
1370 llvm::Value* cmp_eq = irb_.CreateFCmpOEQ(src1_value, src2_value);
1371 llvm::Value* cmp_lt;
1372
1373 if (gt_bias) {
1374 cmp_lt = irb_.CreateFCmpOLT(src1_value, src2_value);
1375 } else {
1376 cmp_lt = irb_.CreateFCmpULT(src1_value, src2_value);
1377 }
1378
1379 llvm::Value* result = EmitCompareResultSelection(cmp_eq, cmp_lt);
1380 EmitStoreDalvikReg(dec_insn.vA_, kInt, kAccurate, result);
1381
Logan Chien70f94b42011-12-27 17:49:11 +08001382 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1383}
1384
1385
1386void MethodCompiler::EmitInsn_LongCompare(uint32_t dex_pc,
1387 Instruction const* insn) {
Logan Chien2c37e8e2011-12-27 17:58:46 +08001388
1389 Instruction::DecodedInstruction dec_insn(insn);
1390
1391 llvm::Value* src1_value = EmitLoadDalvikReg(dec_insn.vB_, kLong, kAccurate);
1392 llvm::Value* src2_value = EmitLoadDalvikReg(dec_insn.vC_, kLong, kAccurate);
1393
1394 llvm::Value* cmp_eq = irb_.CreateICmpEQ(src1_value, src2_value);
1395 llvm::Value* cmp_lt = irb_.CreateICmpSLT(src1_value, src2_value);
1396
1397 llvm::Value* result = EmitCompareResultSelection(cmp_eq, cmp_lt);
1398 EmitStoreDalvikReg(dec_insn.vA_, kInt, kAccurate, result);
1399
Logan Chien70f94b42011-12-27 17:49:11 +08001400 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1401}
1402
1403
Logan Chien2c37e8e2011-12-27 17:58:46 +08001404llvm::Value* MethodCompiler::EmitCompareResultSelection(llvm::Value* cmp_eq,
1405 llvm::Value* cmp_lt) {
1406
1407 llvm::Constant* zero = irb_.getJInt(0);
1408 llvm::Constant* pos1 = irb_.getJInt(1);
1409 llvm::Constant* neg1 = irb_.getJInt(-1);
1410
1411 llvm::Value* result_lt = irb_.CreateSelect(cmp_lt, neg1, pos1);
1412 llvm::Value* result_eq = irb_.CreateSelect(cmp_eq, zero, result_lt);
1413
1414 return result_eq;
1415}
1416
1417
Logan Chien70f94b42011-12-27 17:49:11 +08001418void MethodCompiler::EmitInsn_BinaryConditionalBranch(uint32_t dex_pc,
1419 Instruction const* insn,
1420 CondBranchKind cond) {
1421 // UNIMPLEMENTED(WARNING);
1422 irb_.CreateUnreachable();
1423}
1424
1425
1426void MethodCompiler::EmitInsn_UnaryConditionalBranch(uint32_t dex_pc,
1427 Instruction const* insn,
1428 CondBranchKind cond) {
1429 // UNIMPLEMENTED(WARNING);
1430 irb_.CreateUnreachable();
1431}
1432
1433
1434void MethodCompiler::EmitInsn_AGet(uint32_t dex_pc,
1435 Instruction const* insn,
1436 JType elem_jty) {
1437 // UNIMPLEMENTED(WARNING);
1438 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1439}
1440
1441
1442void MethodCompiler::EmitInsn_APut(uint32_t dex_pc,
1443 Instruction const* insn,
1444 JType elem_jty) {
1445 // UNIMPLEMENTED(WARNING);
1446 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1447}
1448
1449
1450void MethodCompiler::EmitInsn_IGet(uint32_t dex_pc,
1451 Instruction const* insn,
1452 JType field_jty) {
1453 // UNIMPLEMENTED(WARNING);
1454 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1455}
1456
1457
1458void MethodCompiler::EmitInsn_IPut(uint32_t dex_pc,
1459 Instruction const* insn,
1460 JType field_jty) {
1461 // UNIMPLEMENTED(WARNING);
1462 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1463}
1464
1465
1466void MethodCompiler::EmitInsn_SGet(uint32_t dex_pc,
1467 Instruction const* insn,
1468 JType field_jty) {
1469 // UNIMPLEMENTED(WARNING);
1470 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1471}
1472
1473
1474void MethodCompiler::EmitInsn_SPut(uint32_t dex_pc,
1475 Instruction const* insn,
1476 JType field_jty) {
1477 // UNIMPLEMENTED(WARNING);
1478 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1479}
1480
1481
1482void MethodCompiler::EmitInsn_InvokeVirtual(uint32_t dex_pc,
1483 Instruction const* insn,
1484 bool is_range) {
1485 // UNIMPLEMENTED(WARNING);
1486 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1487}
1488
1489
1490void MethodCompiler::EmitInsn_InvokeSuper(uint32_t dex_pc,
1491 Instruction const* insn,
1492 bool is_range) {
1493 // UNIMPLEMENTED(WARNING);
1494 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1495}
1496
1497
1498void MethodCompiler::EmitInsn_InvokeDirect(uint32_t dex_pc,
1499 Instruction const* insn,
1500 bool is_range) {
1501 // UNIMPLEMENTED(WARNING);
1502 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1503}
1504
1505
1506void MethodCompiler::EmitInsn_InvokeStatic(uint32_t dex_pc,
1507 Instruction const* insn,
1508 bool is_range) {
1509 // UNIMPLEMENTED(WARNING);
1510 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1511}
1512
1513
1514void MethodCompiler::EmitInsn_InvokeInterface(uint32_t dex_pc,
1515 Instruction const* insn,
1516 bool is_range) {
1517 // UNIMPLEMENTED(WARNING);
1518 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1519}
1520
1521
1522void MethodCompiler::EmitInsn_Neg(uint32_t dex_pc,
1523 Instruction const* insn,
1524 JType op_jty) {
1525 // UNIMPLEMENTED(WARNING);
1526 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1527}
1528
1529
1530void MethodCompiler::EmitInsn_Not(uint32_t dex_pc,
1531 Instruction const* insn,
1532 JType op_jty) {
1533 // UNIMPLEMENTED(WARNING);
1534 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1535}
1536
1537
1538void MethodCompiler::EmitInsn_SExt(uint32_t dex_pc,
1539 Instruction const* insn) {
1540 // UNIMPLEMENTED(WARNING);
1541 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1542}
1543
1544
1545void MethodCompiler::EmitInsn_Trunc(uint32_t dex_pc,
1546 Instruction const* insn) {
1547 // UNIMPLEMENTED(WARNING);
1548 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1549}
1550
1551
1552void MethodCompiler::EmitInsn_TruncAndSExt(uint32_t dex_pc,
1553 Instruction const* insn,
1554 unsigned N) {
1555 // UNIMPLEMENTED(WARNING);
1556 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1557}
1558
1559
1560void MethodCompiler::EmitInsn_TruncAndZExt(uint32_t dex_pc,
1561 Instruction const* insn,
1562 unsigned N) {
1563 // UNIMPLEMENTED(WARNING);
1564 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1565}
1566
1567
1568void MethodCompiler::EmitInsn_FNeg(uint32_t dex_pc,
1569 Instruction const* insn,
1570 JType op_jty) {
1571 // UNIMPLEMENTED(WARNING);
1572 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1573}
1574
1575
1576void MethodCompiler::EmitInsn_IntToFP(uint32_t dex_pc,
1577 Instruction const* insn,
1578 JType src_jty,
1579 JType dest_jty) {
1580 // UNIMPLEMENTED(WARNING);
1581 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1582}
1583
1584
1585void MethodCompiler::EmitInsn_FPToInt(uint32_t dex_pc,
1586 Instruction const* insn,
1587 JType src_jty,
1588 JType dest_jty) {
1589 // UNIMPLEMENTED(WARNING);
1590 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1591}
1592
1593
1594void MethodCompiler::EmitInsn_FExt(uint32_t dex_pc,
1595 Instruction const* insn) {
1596 // UNIMPLEMENTED(WARNING);
1597 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1598}
1599
1600
1601void MethodCompiler::EmitInsn_FTrunc(uint32_t dex_pc,
1602 Instruction const* insn) {
1603 // UNIMPLEMENTED(WARNING);
1604 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1605}
1606
1607
1608void MethodCompiler::EmitInsn_IntArithm(uint32_t dex_pc,
1609 Instruction const* insn,
1610 IntArithmKind arithm,
1611 JType op_jty,
1612 bool is_2addr) {
1613 // UNIMPLEMENTED(WARNING);
1614 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1615}
1616
1617
1618void MethodCompiler::EmitInsn_IntArithmImmediate(uint32_t dex_pc,
1619 Instruction const* insn,
1620 IntArithmKind arithm) {
1621 // UNIMPLEMENTED(WARNING);
1622 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1623}
1624
1625
1626void MethodCompiler::EmitInsn_RSubImmediate(uint32_t dex_pc,
1627 Instruction const* insn) {
1628 // UNIMPLEMENTED(WARNING);
1629 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1630}
1631
1632
1633void MethodCompiler::EmitInsn_FPArithm(uint32_t dex_pc,
1634 Instruction const* insn,
1635 FPArithmKind arithm,
1636 JType op_jty,
1637 bool is_2addr) {
1638 // UNIMPLEMENTED(WARNING);
1639 irb_.CreateBr(GetNextBasicBlock(dex_pc));
1640}
1641
1642
Logan Chien83426162011-12-09 09:29:50 +08001643CompiledMethod *MethodCompiler::Compile() {
Logan Chien0b827102011-12-20 19:46:14 +08001644 // Code generation
1645 CreateFunction();
1646
1647 EmitPrologue();
1648 EmitInstructions();
Logan Chienc670a8d2011-12-20 21:25:56 +08001649 EmitPrologueLastBranch();
Logan Chien0b827102011-12-20 19:46:14 +08001650
Logan Chiend6c239a2011-12-23 15:11:45 +08001651 // Verify the generated bitcode
1652 llvm::verifyFunction(*func_, llvm::PrintMessageAction);
1653
Logan Chien0b827102011-12-20 19:46:14 +08001654 // Delete the inferred register category map (won't be used anymore)
1655 method_->ResetInferredRegCategoryMap();
1656
1657 return new CompiledMethod(insn_set_, func_);
1658}
1659
1660
1661llvm::Value* MethodCompiler::EmitLoadMethodObjectAddr() {
1662 return func_->arg_begin();
Shih-wei Liaod1fec812012-02-13 09:51:10 -08001663}
Logan Chien83426162011-12-09 09:29:50 +08001664
1665
Logan Chien5bcc04e2012-01-30 14:15:12 +08001666void MethodCompiler::EmitBranchExceptionLandingPad(uint32_t dex_pc) {
1667 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
1668 irb_.CreateBr(lpad);
1669 } else {
1670 irb_.CreateBr(GetUnwindBasicBlock());
1671 }
1672}
1673
1674
1675void MethodCompiler::EmitGuard_ExceptionLandingPad(uint32_t dex_pc) {
1676 llvm::Value* exception_pending =
1677 irb_.CreateCall(irb_.GetRuntime(IsExceptionPending));
1678
1679 llvm::BasicBlock* block_cont = CreateBasicBlockWithDexPC(dex_pc, "cont");
1680
1681 if (llvm::BasicBlock* lpad = GetLandingPadBasicBlock(dex_pc)) {
1682 irb_.CreateCondBr(exception_pending, lpad, block_cont);
1683 } else {
1684 irb_.CreateCondBr(exception_pending, GetUnwindBasicBlock(), block_cont);
1685 }
1686
1687 irb_.SetInsertPoint(block_cont);
1688}
1689
1690
Logan Chien924072f2012-01-30 15:07:24 +08001691void MethodCompiler::EmitGuard_GarbageCollectionSuspend(uint32_t dex_pc) {
1692 llvm::Value* runtime_func = irb_.GetRuntime(TestSuspend);
1693 irb_.CreateCall(runtime_func);
1694
1695 EmitGuard_ExceptionLandingPad(dex_pc);
1696}
1697
1698
Logan Chiend6c239a2011-12-23 15:11:45 +08001699llvm::BasicBlock* MethodCompiler::
1700CreateBasicBlockWithDexPC(uint32_t dex_pc, char const* postfix) {
1701 std::string name;
1702
1703 if (postfix) {
1704 StringAppendF(&name, "B%u.%s", dex_pc, postfix);
1705 } else {
1706 StringAppendF(&name, "B%u", dex_pc);
1707 }
1708
1709 return llvm::BasicBlock::Create(*context_, name, func_);
1710}
1711
1712
1713llvm::BasicBlock* MethodCompiler::GetBasicBlock(uint32_t dex_pc) {
1714 DCHECK(dex_pc < code_item_->insns_size_in_code_units_);
1715
1716 llvm::BasicBlock* basic_block = basic_blocks_[dex_pc];
1717
1718 if (!basic_block) {
1719 basic_block = CreateBasicBlockWithDexPC(dex_pc);
1720 basic_blocks_[dex_pc] = basic_block;
1721 }
1722
1723 return basic_block;
1724}
1725
1726
1727llvm::BasicBlock*
1728MethodCompiler::GetNextBasicBlock(uint32_t dex_pc) {
1729 Instruction const* insn = Instruction::At(code_item_->insns_ + dex_pc);
1730 return GetBasicBlock(dex_pc + insn->SizeInCodeUnits());
1731}
1732
1733
Logan Chien5bcc04e2012-01-30 14:15:12 +08001734int32_t MethodCompiler::GetTryItemOffset(uint32_t dex_pc) {
1735 // TODO: Since we are emitting the dex instructions in ascending order
1736 // w.r.t. address, we can cache the lastest try item offset so that we
1737 // don't have to do binary search for every query.
1738
1739 int32_t min = 0;
1740 int32_t max = code_item_->tries_size_ - 1;
1741
1742 while (min <= max) {
1743 int32_t mid = min + (max - min) / 2;
1744
1745 DexFile::TryItem const* ti = DexFile::GetTryItems(*code_item_, mid);
1746 uint32_t start = ti->start_addr_;
1747 uint32_t end = start + ti->insn_count_;
1748
1749 if (dex_pc < start) {
1750 max = mid - 1;
1751 } else if (dex_pc >= end) {
1752 min = mid + 1;
1753 } else {
1754 return mid; // found
1755 }
1756 }
1757
1758 return -1; // not found
1759}
1760
1761
1762llvm::BasicBlock* MethodCompiler::GetLandingPadBasicBlock(uint32_t dex_pc) {
1763 // Find the try item for this address in this method
1764 int32_t ti_offset = GetTryItemOffset(dex_pc);
1765
1766 if (ti_offset == -1) {
1767 return NULL; // No landing pad is available for this address.
1768 }
1769
1770 // Check for the existing landing pad basic block
1771 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
1772 llvm::BasicBlock* block_lpad = basic_block_landing_pads_[ti_offset];
1773
1774 if (block_lpad) {
1775 // We have generated landing pad for this try item already. Return the
1776 // same basic block.
1777 return block_lpad;
1778 }
1779
1780 // Get try item from code item
1781 DexFile::TryItem const* ti = DexFile::GetTryItems(*code_item_, ti_offset);
1782
1783 // Create landing pad basic block
1784 block_lpad = llvm::BasicBlock::Create(*context_,
1785 StringPrintf("lpad%d", ti_offset),
1786 func_);
1787
1788 // Change IRBuilder insert point
1789 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
1790 irb_.SetInsertPoint(block_lpad);
1791
1792 // Find catch block with matching type
1793 llvm::Value* method_object_addr = EmitLoadMethodObjectAddr();
1794
1795 // TODO: Maybe passing try item offset will be a better idea? For now,
1796 // we are passing dex_pc, so that we can use existing runtime support
1797 // function directly. However, in the runtime supporting function we
1798 // have to search for try item with binary search which can be
1799 // eliminated.
1800 llvm::Value* dex_pc_value = irb_.getInt32(ti->start_addr_);
1801
1802 llvm::Value* catch_handler_index_value =
1803 irb_.CreateCall2(irb_.GetRuntime(FindCatchBlock),
1804 method_object_addr, dex_pc_value);
1805
1806 // Switch instruction (Go to unwind basic block by default)
1807 llvm::SwitchInst* sw =
1808 irb_.CreateSwitch(catch_handler_index_value, GetUnwindBasicBlock());
1809
1810 // Cases with matched catch block
1811 CatchHandlerIterator iter(*code_item_, ti->start_addr_);
1812
1813 for (uint32_t c = 0; iter.HasNext(); iter.Next(), ++c) {
1814 sw->addCase(irb_.getInt32(c), GetBasicBlock(iter.GetHandlerAddress()));
1815 }
1816
1817 // Restore the orignal insert point for IRBuilder
1818 irb_.restoreIP(irb_ip_original);
1819
1820 // Cache this landing pad
1821 DCHECK_GT(basic_block_landing_pads_.size(), static_cast<size_t>(ti_offset));
1822 basic_block_landing_pads_[ti_offset] = block_lpad;
1823
1824 return block_lpad;
1825}
1826
1827
1828llvm::BasicBlock* MethodCompiler::GetUnwindBasicBlock() {
1829 // Check the existing unwinding baisc block block
1830 if (basic_block_unwind_ != NULL) {
1831 return basic_block_unwind_;
1832 }
1833
1834 // Create new basic block for unwinding
1835 basic_block_unwind_ =
1836 llvm::BasicBlock::Create(*context_, "exception_unwind", func_);
1837
1838 // Change IRBuilder insert point
1839 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
1840 irb_.SetInsertPoint(basic_block_unwind_);
1841
1842 // Emit the code to return default value (zero) for the given return type.
1843 char ret_shorty = method_helper_.GetShorty()[0];
1844 if (ret_shorty == 'V') {
1845 irb_.CreateRetVoid();
1846 } else {
1847 irb_.CreateRet(irb_.getJZero(ret_shorty));
1848 }
1849
1850 // Restore the orignal insert point for IRBuilder
1851 irb_.restoreIP(irb_ip_original);
1852
1853 return basic_block_unwind_;
1854}
1855
1856
Logan Chienc670a8d2011-12-20 21:25:56 +08001857llvm::Value* MethodCompiler::AllocDalvikLocalVarReg(RegCategory cat,
1858 uint32_t reg_idx) {
1859
1860 // Save current IR builder insert point
1861 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
1862
1863 // Alloca
1864 llvm::Value* reg_addr = NULL;
1865
1866 switch (cat) {
1867 case kRegCat1nr:
1868 irb_.SetInsertPoint(basic_block_reg_alloca_);
1869 reg_addr = irb_.CreateAlloca(irb_.getJIntTy(), 0,
1870 StringPrintf("r%u", reg_idx));
1871
1872 irb_.SetInsertPoint(basic_block_reg_zero_init_);
1873 irb_.CreateStore(irb_.getJInt(0), reg_addr);
1874 break;
1875
1876 case kRegCat2:
1877 irb_.SetInsertPoint(basic_block_reg_alloca_);
1878 reg_addr = irb_.CreateAlloca(irb_.getJLongTy(), 0,
1879 StringPrintf("w%u", reg_idx));
1880
1881 irb_.SetInsertPoint(basic_block_reg_zero_init_);
1882 irb_.CreateStore(irb_.getJLong(0), reg_addr);
1883 break;
1884
1885 case kRegObject:
1886 irb_.SetInsertPoint(basic_block_reg_alloca_);
1887 reg_addr = irb_.CreateAlloca(irb_.getJObjectTy(), 0,
1888 StringPrintf("p%u", reg_idx));
1889
1890 irb_.SetInsertPoint(basic_block_reg_zero_init_);
1891 irb_.CreateStore(irb_.getJNull(), reg_addr);
1892 break;
1893
1894 default:
1895 LOG(FATAL) << "Unknown register category for allocation: " << cat;
1896 }
1897
1898 // Restore IRBuilder insert point
1899 irb_.restoreIP(irb_ip_original);
1900
1901 DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL));
1902 return reg_addr;
1903}
1904
1905
1906llvm::Value* MethodCompiler::AllocDalvikRetValReg(RegCategory cat) {
1907 // Save current IR builder insert point
1908 llvm::IRBuilderBase::InsertPoint irb_ip_original = irb_.saveIP();
1909
1910 // Alloca
1911 llvm::Value* reg_addr = NULL;
1912
1913 switch (cat) {
1914 case kRegCat1nr:
1915 irb_.SetInsertPoint(basic_block_reg_alloca_);
1916 reg_addr = irb_.CreateAlloca(irb_.getJIntTy(), 0, "r_res");
1917 break;
1918
1919 case kRegCat2:
1920 irb_.SetInsertPoint(basic_block_reg_alloca_);
1921 reg_addr = irb_.CreateAlloca(irb_.getJLongTy(), 0, "w_res");
1922 break;
1923
1924 case kRegObject:
1925 irb_.SetInsertPoint(basic_block_reg_alloca_);
1926 reg_addr = irb_.CreateAlloca(irb_.getJObjectTy(), 0, "p_res");
1927 break;
1928
1929 default:
1930 LOG(FATAL) << "Unknown register category for allocation: " << cat;
1931 }
1932
1933 // Restore IRBuilder insert point
1934 irb_.restoreIP(irb_ip_original);
1935
1936 DCHECK_NE(reg_addr, static_cast<llvm::Value*>(NULL));
1937 return reg_addr;
1938}
1939
1940
Logan Chien83426162011-12-09 09:29:50 +08001941} // namespace compiler_llvm
1942} // namespace art