blob: 0762a0dd89bf39da59e171bdf342d3fe9baa82de [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_IR_BUILDER_H_
18#define ART_SRC_COMPILER_LLVM_IR_BUILDER_H_
19
20#include "backend_types.h"
Logan Chien42e0e152012-01-13 15:42:36 +080021#include "runtime_support_func.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080022
23#include <llvm/Constants.h>
24#include <llvm/DerivedTypes.h>
25#include <llvm/Support/IRBuilder.h>
26#include <llvm/Type.h>
27
28#include <stdint.h>
29
30
31namespace art {
32namespace compiler_llvm {
33
34
35typedef llvm::IRBuilder<> LLVMIRBuilder;
36// NOTE: Here we define our own LLVMIRBuilder type alias, so that we can
37// switch "preserveNames" template parameter easily.
38
39
40class IRBuilder : public LLVMIRBuilder {
41 public:
42 //--------------------------------------------------------------------------
43 // General
44 //--------------------------------------------------------------------------
45
46 IRBuilder(llvm::LLVMContext& context, llvm::Module& module);
47
48
49 //--------------------------------------------------------------------------
Shih-wei Liao4c1f4252012-02-13 09:57:20 -080050 // Pointer Arithmetic Helper Function
51 //--------------------------------------------------------------------------
52
53 llvm::IntegerType* getPtrEquivIntTy() {
54 return getInt32Ty();
55 }
56
57 size_t getSizeOfPtrEquivInt() {
58 return 4;
59 }
60
61 llvm::ConstantInt* getSizeOfPtrEquivIntValue() {
62 return getPtrEquivInt(getSizeOfPtrEquivInt());
63 }
64
65 llvm::ConstantInt* getPtrEquivInt(uint64_t i) {
66 return llvm::ConstantInt::get(getPtrEquivIntTy(), i);
67 }
68
69 llvm::Value* CreatePtrDisp(llvm::Value* base,
70 llvm::Value* offset,
71 llvm::PointerType* ret_ty) {
72
73 llvm::Value* base_int = CreatePtrToInt(base, getPtrEquivIntTy());
74 llvm::Value* result_int = CreateAdd(base_int, offset);
75 llvm::Value* result = CreateIntToPtr(result_int, ret_ty);
76
77 return result;
78 }
79
80 llvm::Value* CreatePtrDisp(llvm::Value* base,
81 llvm::Value* bs,
82 llvm::Value* count,
83 llvm::Value* offset,
84 llvm::PointerType* ret_ty) {
85
86 llvm::Value* block_offset = CreateMul(bs, count);
87 llvm::Value* total_offset = CreateAdd(block_offset, offset);
88
89 return CreatePtrDisp(base, total_offset, ret_ty);
90 }
91
92
93 //--------------------------------------------------------------------------
Logan Chien42e0e152012-01-13 15:42:36 +080094 // Runtime Helper Function
95 //--------------------------------------------------------------------------
96
97 llvm::Function* GetRuntime(runtime_support::RuntimeId rt) const;
98
99
100 //--------------------------------------------------------------------------
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800101 // Type Helper Function
102 //--------------------------------------------------------------------------
103
104 llvm::Type* getJType(char shorty_jty, JTypeSpace space) {
105 return getJType(GetJTypeFromShorty(shorty_jty), space);
106 }
107
108 llvm::Type* getJType(JType jty, JTypeSpace space) {
109 switch (space) {
110 case kAccurate:
111 return getJTypeInAccurateSpace(jty);
112
113 case kReg:
114 case kField: // Currently field space is equivalent to register space.
115 return getJTypeInRegSpace(jty);
116
117 case kArray:
118 return getJTypeInArraySpace(jty);
119 }
120
Logan Chien83426162011-12-09 09:29:50 +0800121 LOG(FATAL) << "Unknown type space: " << space;
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800122 return NULL;
123 }
124
125 llvm::Type* getJVoidTy() {
126 return getVoidTy();
127 }
128
129 llvm::IntegerType* getJBooleanTy() {
130 return getInt1Ty();
131 }
132
133 llvm::IntegerType* getJByteTy() {
134 return getInt8Ty();
135 }
136
137 llvm::IntegerType* getJCharTy() {
138 return getInt16Ty();
139 }
140
141 llvm::IntegerType* getJShortTy() {
142 return getInt16Ty();
143 }
144
145 llvm::IntegerType* getJIntTy() {
146 return getInt32Ty();
147 }
148
149 llvm::IntegerType* getJLongTy() {
150 return getInt64Ty();
151 }
152
153 llvm::Type* getJFloatTy() {
154 return getFloatTy();
155 }
156
157 llvm::Type* getJDoubleTy() {
158 return getDoubleTy();
159 }
160
161 llvm::PointerType* getJObjectTy() {
162 return jobject_type_;
163 }
164
Logan Chienf04364f2012-02-10 12:01:39 +0800165 llvm::PointerType* getJEnvTy() {
166 return jenv_type_;
167 }
168
169 llvm::Type* getJValueTy() {
170 // NOTE: JValue is an union type, which may contains boolean, byte, char,
171 // short, int, long, float, double, Object. However, LLVM itself does
172 // not support union type, so we have to return a type with biggest size,
173 // then bitcast it before we use it.
174 return getJLongTy();
175 }
176
Logan Chien8dfcbea2012-02-17 18:50:32 +0800177 llvm::StructType* getShadowFrameTy(uint32_t sirt_size);
178
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800179
180 //--------------------------------------------------------------------------
181 // Constant Value Helper Function
182 //--------------------------------------------------------------------------
183
184 llvm::ConstantInt* getJBoolean(bool is_true) {
185 return (is_true) ? getTrue() : getFalse();
186 }
187
188 llvm::ConstantInt* getJByte(int8_t i) {
189 return llvm::ConstantInt::getSigned(getJByteTy(), i);
190 }
191
192 llvm::ConstantInt* getJChar(int16_t i) {
193 return llvm::ConstantInt::getSigned(getJCharTy(), i);
194 }
195
196 llvm::ConstantInt* getJShort(int16_t i) {
197 return llvm::ConstantInt::getSigned(getJShortTy(), i);
198 }
199
200 llvm::ConstantInt* getJInt(int32_t i) {
201 return llvm::ConstantInt::getSigned(getJIntTy(), i);
202 }
203
204 llvm::ConstantInt* getJLong(int64_t i) {
205 return llvm::ConstantInt::getSigned(getJLongTy(), i);
206 }
207
208 llvm::Constant* getJFloat(float f) {
209 return llvm::ConstantFP::get(getJFloatTy(), f);
210 }
211
212 llvm::Constant* getJDouble(double d) {
213 return llvm::ConstantFP::get(getJDoubleTy(), d);
214 }
215
216 llvm::ConstantPointerNull* getJNull() {
217 return llvm::ConstantPointerNull::get(getJObjectTy());
218 }
219
220 llvm::Constant* getJZero(char shorty_jty) {
221 return getJZero(GetJTypeFromShorty(shorty_jty));
222 }
223
224 llvm::Constant* getJZero(JType jty) {
225 switch (jty) {
226 case kVoid:
Logan Chien83426162011-12-09 09:29:50 +0800227 LOG(FATAL) << "Zero is not a value of void type";
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800228 return NULL;
229
230 case kBoolean:
231 return getJBoolean(false);
232
233 case kByte:
234 return getJByte(0);
235
236 case kChar:
237 return getJChar(0);
238
239 case kShort:
240 return getJShort(0);
241
242 case kInt:
243 return getJInt(0);
244
245 case kLong:
246 return getJLong(0);
247
248 case kFloat:
249 return getJFloat(0.0f);
250
251 case kDouble:
252 return getJDouble(0.0);
253
254 case kObject:
255 return getJNull();
256 }
257
258 LOG(FATAL) << "Unknown java type: " << jty;
259 return NULL;
260 }
261
262
263 private:
264 //--------------------------------------------------------------------------
Logan Chien42e0e152012-01-13 15:42:36 +0800265 // Runtime Helper Function (Private)
266 //--------------------------------------------------------------------------
267
Logan Chien6a917992012-02-17 18:43:48 +0800268 void InitRuntimeSupportFuncDecl();
Logan Chien42e0e152012-01-13 15:42:36 +0800269
270
271 //--------------------------------------------------------------------------
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800272 // Type Helper Function (Private)
273 //--------------------------------------------------------------------------
274
275 llvm::Type* getJTypeInAccurateSpace(JType jty);
276 llvm::Type* getJTypeInRegSpace(JType jty);
277 llvm::Type* getJTypeInArraySpace(JType jty);
278
279
280 private:
Logan Chien6a917992012-02-17 18:43:48 +0800281 llvm::Module* module_;
282
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800283 llvm::PointerType* jobject_type_;
284
Logan Chienf04364f2012-02-10 12:01:39 +0800285 llvm::PointerType* jenv_type_;
286
Logan Chien8dfcbea2012-02-17 18:50:32 +0800287 llvm::StructType* art_frame_type_;
288
Logan Chien42e0e152012-01-13 15:42:36 +0800289 llvm::Function* runtime_support_func_decls_[runtime_support::MAX_ID];
290
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800291};
292
293
294} // namespace compiler_llvm
295} // namespace art
296
297#endif // ART_SRC_COMPILER_LLVM_IR_BUILDER_H_