blob: 5918107611d10f4041cbd4321489a308c6f6702d [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
92
Shih-wei Liaod1fec812012-02-13 09:51:10 -080093 public:
Logan Chien83426162011-12-09 09:29:50 +080094 MethodCompiler(InstructionSet insn_set,
95 Compiler* compiler,
96 ClassLinker* class_linker,
97 ClassLoader const* class_loader,
98 DexFile const* dex_file,
99 DexCache* dex_cache,
100 DexFile::CodeItem const* code_item,
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800101 uint32_t method_idx,
102 uint32_t access_flags);
103
104 ~MethodCompiler();
105
Logan Chien83426162011-12-09 09:29:50 +0800106 CompiledMethod* Compile();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800107
Logan Chienc670a8d2011-12-20 21:25:56 +0800108
109 // Code generation helper function
110
111 IRBuilder& GetIRBuilder() const {
112 return irb_;
113 }
114
115
116 // Register helper function
117
118 llvm::Value* AllocDalvikLocalVarReg(RegCategory cat, uint32_t reg_idx);
119
120 llvm::Value* AllocDalvikRetValReg(RegCategory cat);
121
122
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800123 private:
124 void CreateFunction();
125
Logan Chienc670a8d2011-12-20 21:25:56 +0800126
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800127 void EmitPrologue();
Logan Chienc670a8d2011-12-20 21:25:56 +0800128 void EmitPrologueLastBranch();
Logan Chiend6ececa2011-12-27 16:20:15 +0800129 void EmitPrologueAssignArgRegister();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800130 void EmitInstructions();
Logan Chien83426162011-12-09 09:29:50 +0800131 void EmitInstruction(uint32_t dex_pc, Instruction const* insn);
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800132
Logan Chien0b827102011-12-20 19:46:14 +0800133
134 // Code generation helper function
135
136 llvm::Value* EmitLoadMethodObjectAddr();
137
138 llvm::FunctionType* GetFunctionType(uint32_t method_idx, bool is_static);
139
Logan Chiend6c239a2011-12-23 15:11:45 +0800140
141 // Basic block helper functions
142 llvm::BasicBlock* GetBasicBlock(uint32_t dex_pc);
143
144 llvm::BasicBlock* GetNextBasicBlock(uint32_t dex_pc);
145
146 llvm::BasicBlock* CreateBasicBlockWithDexPC(uint32_t dex_pc,
147 char const* postfix = NULL);
148
Logan Chienc670a8d2011-12-20 21:25:56 +0800149
150 // Register helper function
151
152 llvm::Value* EmitLoadDalvikReg(uint32_t reg_idx, JType jty,
153 JTypeSpace space) {
154 return regs_[reg_idx]->GetValue(jty, space);
155 }
156
157 llvm::Value* EmitLoadDalvikReg(uint32_t reg_idx, char shorty,
158 JTypeSpace space) {
159 return EmitLoadDalvikReg(reg_idx, GetJTypeFromShorty(shorty), space);
160 }
161
162 void EmitStoreDalvikReg(uint32_t reg_idx, JType jty,
163 JTypeSpace space, llvm::Value* new_value) {
164 regs_[reg_idx]->SetValue(jty, space, new_value);
165 }
166
167 void EmitStoreDalvikReg(uint32_t reg_idx, char shorty,
168 JTypeSpace space, llvm::Value* new_value) {
169 EmitStoreDalvikReg(reg_idx, GetJTypeFromShorty(shorty), space, new_value);
170 }
171
172 llvm::Value* EmitLoadDalvikRetValReg(JType jty, JTypeSpace space) {
173 return retval_reg_->GetValue(jty, space);
174 }
175
176 llvm::Value* EmitLoadDalvikRetValReg(char shorty, JTypeSpace space) {
177 return EmitLoadDalvikRetValReg(GetJTypeFromShorty(shorty), space);
178 }
179
180 void EmitStoreDalvikRetValReg(JType jty, JTypeSpace space,
181 llvm::Value* new_value) {
182 retval_reg_->SetValue(jty, space, new_value);
183 }
184
185 void EmitStoreDalvikRetValReg(char shorty, JTypeSpace space,
186 llvm::Value* new_value) {
187 EmitStoreDalvikRetValReg(GetJTypeFromShorty(shorty), space, new_value);
188 }
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800189};
190
191
192} // namespace compiler_llvm
193} // namespace art
194
195#endif // ART_SRC_COMPILER_LLVM_METHOD_COMPILER_H_