blob: 1a27d0258f5183ad1faa5175500b90636ced0a57 [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 "ir_builder.h"
Logan Chien42e0e152012-01-13 15:42:36 +080018#include "runtime_support_func.h"
Logan Chien8dfcbea2012-02-17 18:50:32 +080019#include "stringprintf.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080020
21#include <llvm/Module.h>
22
Logan Chien83426162011-12-09 09:29:50 +080023namespace art {
24namespace compiler_llvm {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080025
26
27//----------------------------------------------------------------------------
28// General
29//----------------------------------------------------------------------------
30
31IRBuilder::IRBuilder(llvm::LLVMContext& context, llvm::Module& module)
Logan Chien6a917992012-02-17 18:43:48 +080032: LLVMIRBuilder(context), module_(&module) {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080033
34 // Get java object type from module
Logan Chien42e0e152012-01-13 15:42:36 +080035 llvm::Type* jobject_struct_type = module.getTypeByName("JavaObject");
Logan Chien1b0a1b72012-03-15 06:20:17 +080036 CHECK(jobject_struct_type != NULL);
Shih-wei Liaod1fec812012-02-13 09:51:10 -080037 jobject_type_ = jobject_struct_type->getPointerTo();
Logan Chien42e0e152012-01-13 15:42:36 +080038
Logan Chienf04364f2012-02-10 12:01:39 +080039 // Create JEnv* type
40 llvm::Type* jenv_struct_type = llvm::StructType::create(context, "JEnv");
41 jenv_type_ = jenv_struct_type->getPointerTo();
42
Logan Chien8dfcbea2012-02-17 18:50:32 +080043 // Get Art shadow frame struct type from module
Logan Chien1b0a1b72012-03-15 06:20:17 +080044 art_frame_type_ = module.getTypeByName("ShadowFrame");
45 CHECK(art_frame_type_ != NULL);
Logan Chien8dfcbea2012-02-17 18:50:32 +080046
Logan Chien42e0e152012-01-13 15:42:36 +080047 // Load the runtime support function declaration from module
Logan Chien6a917992012-02-17 18:43:48 +080048 InitRuntimeSupportFuncDecl();
Logan Chien42e0e152012-01-13 15:42:36 +080049}
50
51
52//----------------------------------------------------------------------------
53// Runtime Helper Function
54//----------------------------------------------------------------------------
55
Logan Chien6a917992012-02-17 18:43:48 +080056void IRBuilder::InitRuntimeSupportFuncDecl() {
Logan Chien42e0e152012-01-13 15:42:36 +080057 using namespace runtime_support;
58
59#define GET_RUNTIME_SUPPORT_FUNC_DECL(ID, NAME) \
60 do { \
Logan Chienbae4c852012-03-06 16:53:31 +080061 llvm::Function* fn = module_->getFunction(#NAME); \
62 DCHECK_NE(fn, (void*)NULL) << "Function not found: " << #NAME; \
Logan Chien42e0e152012-01-13 15:42:36 +080063 runtime_support_func_decls_[ID] = fn; \
64 } while (0);
65
66#include "runtime_support_func_list.h"
67 RUNTIME_SUPPORT_FUNC_LIST(GET_RUNTIME_SUPPORT_FUNC_DECL)
68#undef RUNTIME_SUPPORT_FUNC_LIST
69#undef GET_RUNTIME_SUPPORT_FUNC_DECL
70}
71
72
73llvm::Function* IRBuilder::GetRuntime(runtime_support::RuntimeId rt) const {
74 using namespace runtime_support;
75
Elliott Hughesb25c3f62012-03-26 16:35:06 -070076 if (rt >= 0 && rt < MAX_ID) {
Logan Chien42e0e152012-01-13 15:42:36 +080077 return runtime_support_func_decls_[rt];
78 } else {
79 LOG(ERROR) << "Unknown runtime function id: " << rt;
80 return NULL;
81 }
Shih-wei Liaod1fec812012-02-13 09:51:10 -080082}
83
84
85//----------------------------------------------------------------------------
86// Type Helper Function
87//----------------------------------------------------------------------------
88
89llvm::Type* IRBuilder::getJTypeInAccurateSpace(JType jty) {
90 switch (jty) {
91 case kVoid:
92 return getJVoidTy();
93
94 case kBoolean:
95 return getJBooleanTy();
96
97 case kByte:
98 return getJByteTy();
99
100 case kChar:
101 return getJCharTy();
102
103 case kShort:
104 return getJShortTy();
105
106 case kInt:
107 return getJIntTy();
108
109 case kLong:
110 return getJLongTy();
111
112 case kFloat:
113 return getJFloatTy();
114
115 case kDouble:
116 return getJDoubleTy();
117
118 case kObject:
119 return getJObjectTy();
120 }
121
122 LOG(FATAL) << "Unknown java type: " << jty;
123 return NULL;
124}
125
126
127llvm::Type* IRBuilder::getJTypeInRegSpace(JType jty) {
Logan Chien83426162011-12-09 09:29:50 +0800128 RegCategory regcat = GetRegCategoryFromJType(jty);
129
130 switch (regcat) {
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800131 case kRegUnknown:
132 case kRegZero:
Logan Chien83426162011-12-09 09:29:50 +0800133 LOG(FATAL) << "Register category \"Unknown\" or \"Zero\" does not have "
134 << "the LLVM type";
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800135 return NULL;
136
137 case kRegCat1nr:
138 return getInt32Ty();
139
140 case kRegCat2:
141 return getInt64Ty();
142
143 case kRegObject:
144 return getJObjectTy();
145 }
146
Logan Chien83426162011-12-09 09:29:50 +0800147 LOG(FATAL) << "Unknown register category: " << regcat;
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800148 return NULL;
149}
150
151
152llvm::Type* IRBuilder::getJTypeInArraySpace(JType jty) {
153 switch (jty) {
154 case kVoid:
Logan Chien83426162011-12-09 09:29:50 +0800155 LOG(FATAL) << "void type should not be used in array type space";
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800156 return NULL;
157
158 case kBoolean:
159 case kByte:
160 return getInt8Ty();
161
162 case kChar:
163 case kShort:
164 return getInt16Ty();
165
166 case kInt:
167 return getInt32Ty();
168
169 case kLong:
170 return getInt64Ty();
171
172 case kFloat:
173 return getFloatTy();
174
175 case kDouble:
176 return getDoubleTy();
177
178 case kObject:
179 return getJObjectTy();
180 }
181
Logan Chien83426162011-12-09 09:29:50 +0800182 LOG(FATAL) << "Unknown java type: " << jty;
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800183 return NULL;
184}
Logan Chien83426162011-12-09 09:29:50 +0800185
186
Logan Chien8dfcbea2012-02-17 18:50:32 +0800187llvm::StructType* IRBuilder::getShadowFrameTy(uint32_t sirt_size) {
Logan Chien1b0a1b72012-03-15 06:20:17 +0800188 std::string name(StringPrintf("ShadowFrame%u", sirt_size));
Logan Chien8dfcbea2012-02-17 18:50:32 +0800189
190 // Try to find the existing struct type definition
191 if (llvm::Type* type = module_->getTypeByName(name)) {
192 CHECK(llvm::isa<llvm::StructType>(type));
193 return static_cast<llvm::StructType*>(type);
194 }
195
196 // Create new struct type definition
197 llvm::Type* elem_types[] = {
198 art_frame_type_,
199 llvm::ArrayType::get(jobject_type_, sirt_size),
200 };
201
202 return llvm::StructType::create(elem_types, name);
203}
204
205
Logan Chien83426162011-12-09 09:29:50 +0800206} // namespace compiler_llvm
207} // namespace art