blob: f9e73f98945c90e46d16848aac012e397c0d116f [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#ifndef ART_SRC_COMPILER_LLVM_METHOD_COMPILER_H_
18#define ART_SRC_COMPILER_LLVM_METHOD_COMPILER_H_
19
Logan Chienfca7e872011-12-20 20:08:22 +080020#include "backend_types.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080021#include "constants.h"
Logan Chienc670a8d2011-12-20 21:25:56 +080022#include "dalvik_reg.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080023#include "dex_file.h"
24#include "dex_instruction.h"
25#include "object_utils.h"
26
27#include <llvm/Support/IRBuilder.h>
28
29#include <vector>
30
31#include <stdint.h>
32
33
34namespace art {
35 class ClassLinker;
36 class ClassLoader;
37 class CompiledMethod;
38 class Compiler;
39 class DexCache;
40}
41
42
43namespace llvm {
44 class AllocaInst;
45 class BasicBlock;
46 class Function;
47 class FunctionType;
48 class LLVMContext;
49 class Module;
50 class Type;
51}
52
53
54namespace art {
55namespace compiler_llvm {
56
57class CompilerLLVM;
58class IRBuilder;
59
60class MethodCompiler {
61 private:
Logan Chien83426162011-12-09 09:29:50 +080062 InstructionSet insn_set_;
63 Compiler* compiler_;
64 compiler_llvm::CompilerLLVM* compiler_llvm_;
Shih-wei Liaod1fec812012-02-13 09:51:10 -080065
Logan Chien83426162011-12-09 09:29:50 +080066 ClassLinker* class_linker_;
67 ClassLoader const* class_loader_;
Shih-wei Liaod1fec812012-02-13 09:51:10 -080068
Logan Chien83426162011-12-09 09:29:50 +080069 DexFile const* dex_file_;
70 DexCache* dex_cache_;
71 DexFile::CodeItem const* code_item_;
Shih-wei Liaod1fec812012-02-13 09:51:10 -080072
Logan Chien83426162011-12-09 09:29:50 +080073 Method* method_;
74 MethodHelper method_helper_;
Shih-wei Liaod1fec812012-02-13 09:51:10 -080075
76 uint32_t method_idx_;
77 uint32_t access_flags_;
78
79 llvm::Module* module_;
80 llvm::LLVMContext* context_;
81 IRBuilder& irb_;
82 llvm::Function* func_;
83
Logan Chienc670a8d2011-12-20 21:25:56 +080084 std::vector<DalvikReg*> regs_;
85 UniquePtr<DalvikReg> retval_reg_;
Logan Chiend6c239a2011-12-23 15:11:45 +080086
Logan Chienc670a8d2011-12-20 21:25:56 +080087 llvm::BasicBlock* basic_block_reg_alloca_;
88 llvm::BasicBlock* basic_block_reg_zero_init_;
Logan Chiend6ececa2011-12-27 16:20:15 +080089 llvm::BasicBlock* basic_block_reg_arg_init_;
Logan Chiend6c239a2011-12-23 15:11:45 +080090 std::vector<llvm::BasicBlock*> basic_blocks_;
91
Logan Chien5bcc04e2012-01-30 14:15:12 +080092 std::vector<llvm::BasicBlock*> basic_block_landing_pads_;
93 llvm::BasicBlock* basic_block_unwind_;
94 llvm::BasicBlock* basic_block_unreachable_;
95
Logan Chiend6c239a2011-12-23 15:11:45 +080096
Shih-wei Liaod1fec812012-02-13 09:51:10 -080097 public:
Logan Chien83426162011-12-09 09:29:50 +080098 MethodCompiler(InstructionSet insn_set,
99 Compiler* compiler,
100 ClassLinker* class_linker,
101 ClassLoader const* class_loader,
102 DexFile const* dex_file,
103 DexCache* dex_cache,
104 DexFile::CodeItem const* code_item,
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800105 uint32_t method_idx,
106 uint32_t access_flags);
107
108 ~MethodCompiler();
109
Logan Chien83426162011-12-09 09:29:50 +0800110 CompiledMethod* Compile();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800111
Logan Chienc670a8d2011-12-20 21:25:56 +0800112
113 // Code generation helper function
114
115 IRBuilder& GetIRBuilder() const {
116 return irb_;
117 }
118
119
120 // Register helper function
121
122 llvm::Value* AllocDalvikLocalVarReg(RegCategory cat, uint32_t reg_idx);
123
124 llvm::Value* AllocDalvikRetValReg(RegCategory cat);
125
126
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800127 private:
128 void CreateFunction();
129
Logan Chienc670a8d2011-12-20 21:25:56 +0800130
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800131 void EmitPrologue();
Logan Chienc670a8d2011-12-20 21:25:56 +0800132 void EmitPrologueLastBranch();
Logan Chiend6ececa2011-12-27 16:20:15 +0800133 void EmitPrologueAssignArgRegister();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800134 void EmitInstructions();
Logan Chien83426162011-12-09 09:29:50 +0800135 void EmitInstruction(uint32_t dex_pc, Instruction const* insn);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800136
Logan Chien0b827102011-12-20 19:46:14 +0800137
138 // Code generation helper function
139
140 llvm::Value* EmitLoadMethodObjectAddr();
141
142 llvm::FunctionType* GetFunctionType(uint32_t method_idx, bool is_static);
143
Logan Chien5bcc04e2012-01-30 14:15:12 +0800144 void EmitGuard_ExceptionLandingPad(uint32_t dex_pc);
145
146 void EmitBranchExceptionLandingPad(uint32_t dex_pc);
147
Logan Chien924072f2012-01-30 15:07:24 +0800148 void EmitGuard_GarbageCollectionSuspend(uint32_t addr);
149
Logan Chiend6c239a2011-12-23 15:11:45 +0800150
151 // Basic block helper functions
152 llvm::BasicBlock* GetBasicBlock(uint32_t dex_pc);
153
154 llvm::BasicBlock* GetNextBasicBlock(uint32_t dex_pc);
155
156 llvm::BasicBlock* CreateBasicBlockWithDexPC(uint32_t dex_pc,
157 char const* postfix = NULL);
158
Logan Chien5bcc04e2012-01-30 14:15:12 +0800159 int32_t GetTryItemOffset(uint32_t dex_pc);
160
161 llvm::BasicBlock* GetLandingPadBasicBlock(uint32_t dex_pc);
162
163 llvm::BasicBlock* GetUnwindBasicBlock();
164
Logan Chienc670a8d2011-12-20 21:25:56 +0800165
166 // Register helper function
167
168 llvm::Value* EmitLoadDalvikReg(uint32_t reg_idx, JType jty,
169 JTypeSpace space) {
170 return regs_[reg_idx]->GetValue(jty, space);
171 }
172
173 llvm::Value* EmitLoadDalvikReg(uint32_t reg_idx, char shorty,
174 JTypeSpace space) {
175 return EmitLoadDalvikReg(reg_idx, GetJTypeFromShorty(shorty), space);
176 }
177
178 void EmitStoreDalvikReg(uint32_t reg_idx, JType jty,
179 JTypeSpace space, llvm::Value* new_value) {
180 regs_[reg_idx]->SetValue(jty, space, new_value);
181 }
182
183 void EmitStoreDalvikReg(uint32_t reg_idx, char shorty,
184 JTypeSpace space, llvm::Value* new_value) {
185 EmitStoreDalvikReg(reg_idx, GetJTypeFromShorty(shorty), space, new_value);
186 }
187
188 llvm::Value* EmitLoadDalvikRetValReg(JType jty, JTypeSpace space) {
189 return retval_reg_->GetValue(jty, space);
190 }
191
192 llvm::Value* EmitLoadDalvikRetValReg(char shorty, JTypeSpace space) {
193 return EmitLoadDalvikRetValReg(GetJTypeFromShorty(shorty), space);
194 }
195
196 void EmitStoreDalvikRetValReg(JType jty, JTypeSpace space,
197 llvm::Value* new_value) {
198 retval_reg_->SetValue(jty, space, new_value);
199 }
200
201 void EmitStoreDalvikRetValReg(char shorty, JTypeSpace space,
202 llvm::Value* new_value) {
203 EmitStoreDalvikRetValReg(GetJTypeFromShorty(shorty), space, new_value);
204 }
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800205};
206
207
208} // namespace compiler_llvm
209} // namespace art
210
211#endif // ART_SRC_COMPILER_LLVM_METHOD_COMPILER_H_