blob: a88b16da10c24845aae50ddc2e7e34b1502f11ad [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"
21
22#include <llvm/Constants.h>
23#include <llvm/DerivedTypes.h>
24#include <llvm/Support/IRBuilder.h>
25#include <llvm/Type.h>
26
27#include <stdint.h>
28
29
30namespace art {
31namespace compiler_llvm {
32
33
34typedef llvm::IRBuilder<> LLVMIRBuilder;
35// NOTE: Here we define our own LLVMIRBuilder type alias, so that we can
36// switch "preserveNames" template parameter easily.
37
38
39class IRBuilder : public LLVMIRBuilder {
40 public:
41 //--------------------------------------------------------------------------
42 // General
43 //--------------------------------------------------------------------------
44
45 IRBuilder(llvm::LLVMContext& context, llvm::Module& module);
46
47
48 //--------------------------------------------------------------------------
Shih-wei Liao4c1f4252012-02-13 09:57:20 -080049 // Pointer Arithmetic Helper Function
50 //--------------------------------------------------------------------------
51
52 llvm::IntegerType* getPtrEquivIntTy() {
53 return getInt32Ty();
54 }
55
56 size_t getSizeOfPtrEquivInt() {
57 return 4;
58 }
59
60 llvm::ConstantInt* getSizeOfPtrEquivIntValue() {
61 return getPtrEquivInt(getSizeOfPtrEquivInt());
62 }
63
64 llvm::ConstantInt* getPtrEquivInt(uint64_t i) {
65 return llvm::ConstantInt::get(getPtrEquivIntTy(), i);
66 }
67
68 llvm::Value* CreatePtrDisp(llvm::Value* base,
69 llvm::Value* offset,
70 llvm::PointerType* ret_ty) {
71
72 llvm::Value* base_int = CreatePtrToInt(base, getPtrEquivIntTy());
73 llvm::Value* result_int = CreateAdd(base_int, offset);
74 llvm::Value* result = CreateIntToPtr(result_int, ret_ty);
75
76 return result;
77 }
78
79 llvm::Value* CreatePtrDisp(llvm::Value* base,
80 llvm::Value* bs,
81 llvm::Value* count,
82 llvm::Value* offset,
83 llvm::PointerType* ret_ty) {
84
85 llvm::Value* block_offset = CreateMul(bs, count);
86 llvm::Value* total_offset = CreateAdd(block_offset, offset);
87
88 return CreatePtrDisp(base, total_offset, ret_ty);
89 }
90
91
92 //--------------------------------------------------------------------------
Shih-wei Liaod1fec812012-02-13 09:51:10 -080093 // Type Helper Function
94 //--------------------------------------------------------------------------
95
96 llvm::Type* getJType(char shorty_jty, JTypeSpace space) {
97 return getJType(GetJTypeFromShorty(shorty_jty), space);
98 }
99
100 llvm::Type* getJType(JType jty, JTypeSpace space) {
101 switch (space) {
102 case kAccurate:
103 return getJTypeInAccurateSpace(jty);
104
105 case kReg:
106 case kField: // Currently field space is equivalent to register space.
107 return getJTypeInRegSpace(jty);
108
109 case kArray:
110 return getJTypeInArraySpace(jty);
111 }
112
Logan Chien83426162011-12-09 09:29:50 +0800113 LOG(FATAL) << "Unknown type space: " << space;
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800114 return NULL;
115 }
116
117 llvm::Type* getJVoidTy() {
118 return getVoidTy();
119 }
120
121 llvm::IntegerType* getJBooleanTy() {
122 return getInt1Ty();
123 }
124
125 llvm::IntegerType* getJByteTy() {
126 return getInt8Ty();
127 }
128
129 llvm::IntegerType* getJCharTy() {
130 return getInt16Ty();
131 }
132
133 llvm::IntegerType* getJShortTy() {
134 return getInt16Ty();
135 }
136
137 llvm::IntegerType* getJIntTy() {
138 return getInt32Ty();
139 }
140
141 llvm::IntegerType* getJLongTy() {
142 return getInt64Ty();
143 }
144
145 llvm::Type* getJFloatTy() {
146 return getFloatTy();
147 }
148
149 llvm::Type* getJDoubleTy() {
150 return getDoubleTy();
151 }
152
153 llvm::PointerType* getJObjectTy() {
154 return jobject_type_;
155 }
156
157
158 //--------------------------------------------------------------------------
159 // Constant Value Helper Function
160 //--------------------------------------------------------------------------
161
162 llvm::ConstantInt* getJBoolean(bool is_true) {
163 return (is_true) ? getTrue() : getFalse();
164 }
165
166 llvm::ConstantInt* getJByte(int8_t i) {
167 return llvm::ConstantInt::getSigned(getJByteTy(), i);
168 }
169
170 llvm::ConstantInt* getJChar(int16_t i) {
171 return llvm::ConstantInt::getSigned(getJCharTy(), i);
172 }
173
174 llvm::ConstantInt* getJShort(int16_t i) {
175 return llvm::ConstantInt::getSigned(getJShortTy(), i);
176 }
177
178 llvm::ConstantInt* getJInt(int32_t i) {
179 return llvm::ConstantInt::getSigned(getJIntTy(), i);
180 }
181
182 llvm::ConstantInt* getJLong(int64_t i) {
183 return llvm::ConstantInt::getSigned(getJLongTy(), i);
184 }
185
186 llvm::Constant* getJFloat(float f) {
187 return llvm::ConstantFP::get(getJFloatTy(), f);
188 }
189
190 llvm::Constant* getJDouble(double d) {
191 return llvm::ConstantFP::get(getJDoubleTy(), d);
192 }
193
194 llvm::ConstantPointerNull* getJNull() {
195 return llvm::ConstantPointerNull::get(getJObjectTy());
196 }
197
198 llvm::Constant* getJZero(char shorty_jty) {
199 return getJZero(GetJTypeFromShorty(shorty_jty));
200 }
201
202 llvm::Constant* getJZero(JType jty) {
203 switch (jty) {
204 case kVoid:
Logan Chien83426162011-12-09 09:29:50 +0800205 LOG(FATAL) << "Zero is not a value of void type";
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800206 return NULL;
207
208 case kBoolean:
209 return getJBoolean(false);
210
211 case kByte:
212 return getJByte(0);
213
214 case kChar:
215 return getJChar(0);
216
217 case kShort:
218 return getJShort(0);
219
220 case kInt:
221 return getJInt(0);
222
223 case kLong:
224 return getJLong(0);
225
226 case kFloat:
227 return getJFloat(0.0f);
228
229 case kDouble:
230 return getJDouble(0.0);
231
232 case kObject:
233 return getJNull();
234 }
235
236 LOG(FATAL) << "Unknown java type: " << jty;
237 return NULL;
238 }
239
240
241 private:
242 //--------------------------------------------------------------------------
243 // Type Helper Function (Private)
244 //--------------------------------------------------------------------------
245
246 llvm::Type* getJTypeInAccurateSpace(JType jty);
247 llvm::Type* getJTypeInRegSpace(JType jty);
248 llvm::Type* getJTypeInArraySpace(JType jty);
249
250
251 private:
252 llvm::PointerType* jobject_type_;
253
254};
255
256
257} // namespace compiler_llvm
258} // namespace art
259
260#endif // ART_SRC_COMPILER_LLVM_IR_BUILDER_H_