blob: b2c795b9c4a5aaca1a24e9c8063a88cc973d1bda [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"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080019
20#include <llvm/Module.h>
21
Logan Chien83426162011-12-09 09:29:50 +080022namespace art {
23namespace compiler_llvm {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080024
25
26//----------------------------------------------------------------------------
27// General
28//----------------------------------------------------------------------------
29
30IRBuilder::IRBuilder(llvm::LLVMContext& context, llvm::Module& module)
31: LLVMIRBuilder(context) {
32
33 // Get java object type from module
Logan Chien42e0e152012-01-13 15:42:36 +080034 llvm::Type* jobject_struct_type = module.getTypeByName("JavaObject");
35 CHECK_NE(jobject_struct_type, static_cast<llvm::Type*>(NULL));
Shih-wei Liaod1fec812012-02-13 09:51:10 -080036 jobject_type_ = jobject_struct_type->getPointerTo();
Logan Chien42e0e152012-01-13 15:42:36 +080037
38 // Load the runtime support function declaration from module
39 InitRuntimeSupportFuncDecl(module);
40}
41
42
43//----------------------------------------------------------------------------
44// Runtime Helper Function
45//----------------------------------------------------------------------------
46
47void IRBuilder::InitRuntimeSupportFuncDecl(llvm::Module& module) {
48 using namespace runtime_support;
49
50#define GET_RUNTIME_SUPPORT_FUNC_DECL(ID, NAME) \
51 do { \
52 llvm::Function* fn = module.getFunction(NAME); \
53 DCHECK_NE(fn, (void*)NULL) << "Function not found: " << NAME; \
54 runtime_support_func_decls_[ID] = fn; \
55 } while (0);
56
57#include "runtime_support_func_list.h"
58 RUNTIME_SUPPORT_FUNC_LIST(GET_RUNTIME_SUPPORT_FUNC_DECL)
59#undef RUNTIME_SUPPORT_FUNC_LIST
60#undef GET_RUNTIME_SUPPORT_FUNC_DECL
61}
62
63
64llvm::Function* IRBuilder::GetRuntime(runtime_support::RuntimeId rt) const {
65 using namespace runtime_support;
66
67 if (rt >= 0 && rt < MAX_ID){
68 return runtime_support_func_decls_[rt];
69 } else {
70 LOG(ERROR) << "Unknown runtime function id: " << rt;
71 return NULL;
72 }
Shih-wei Liaod1fec812012-02-13 09:51:10 -080073}
74
75
76//----------------------------------------------------------------------------
77// Type Helper Function
78//----------------------------------------------------------------------------
79
80llvm::Type* IRBuilder::getJTypeInAccurateSpace(JType jty) {
81 switch (jty) {
82 case kVoid:
83 return getJVoidTy();
84
85 case kBoolean:
86 return getJBooleanTy();
87
88 case kByte:
89 return getJByteTy();
90
91 case kChar:
92 return getJCharTy();
93
94 case kShort:
95 return getJShortTy();
96
97 case kInt:
98 return getJIntTy();
99
100 case kLong:
101 return getJLongTy();
102
103 case kFloat:
104 return getJFloatTy();
105
106 case kDouble:
107 return getJDoubleTy();
108
109 case kObject:
110 return getJObjectTy();
111 }
112
113 LOG(FATAL) << "Unknown java type: " << jty;
114 return NULL;
115}
116
117
118llvm::Type* IRBuilder::getJTypeInRegSpace(JType jty) {
Logan Chien83426162011-12-09 09:29:50 +0800119 RegCategory regcat = GetRegCategoryFromJType(jty);
120
121 switch (regcat) {
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800122 case kRegUnknown:
123 case kRegZero:
Logan Chien83426162011-12-09 09:29:50 +0800124 LOG(FATAL) << "Register category \"Unknown\" or \"Zero\" does not have "
125 << "the LLVM type";
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800126 return NULL;
127
128 case kRegCat1nr:
129 return getInt32Ty();
130
131 case kRegCat2:
132 return getInt64Ty();
133
134 case kRegObject:
135 return getJObjectTy();
136 }
137
Logan Chien83426162011-12-09 09:29:50 +0800138 LOG(FATAL) << "Unknown register category: " << regcat;
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800139 return NULL;
140}
141
142
143llvm::Type* IRBuilder::getJTypeInArraySpace(JType jty) {
144 switch (jty) {
145 case kVoid:
Logan Chien83426162011-12-09 09:29:50 +0800146 LOG(FATAL) << "void type should not be used in array type space";
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800147 return NULL;
148
149 case kBoolean:
150 case kByte:
151 return getInt8Ty();
152
153 case kChar:
154 case kShort:
155 return getInt16Ty();
156
157 case kInt:
158 return getInt32Ty();
159
160 case kLong:
161 return getInt64Ty();
162
163 case kFloat:
164 return getFloatTy();
165
166 case kDouble:
167 return getDoubleTy();
168
169 case kObject:
170 return getJObjectTy();
171 }
172
Logan Chien83426162011-12-09 09:29:50 +0800173 LOG(FATAL) << "Unknown java type: " << jty;
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800174 return NULL;
175}
Logan Chien83426162011-12-09 09:29:50 +0800176
177
178} // namespace compiler_llvm
179} // namespace art