blob: 9644ebd976cc405c87b9ed14e79292eb21b13512 [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"
Elliott Hughese222ee02012-12-13 14:41:43 -080018
19#include "base/stringprintf.h"
Shih-wei Liaod1fec812012-02-13 09:51:10 -080020
Brian Carlstrom37d48792013-03-22 14:14:45 -070021#include <llvm/IR/Module.h>
Shih-wei Liaod1fec812012-02-13 09:51:10 -080022
Logan Chien83426162011-12-09 09:29:50 +080023namespace art {
Ian Rogers4c1c2832013-03-04 18:30:13 -080024namespace llvm {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080025
26
27//----------------------------------------------------------------------------
28// General
29//----------------------------------------------------------------------------
30
Ian Rogers4c1c2832013-03-04 18:30:13 -080031IRBuilder::IRBuilder(::llvm::LLVMContext& context, ::llvm::Module& module,
Ian Rogers76ae4fe2013-02-27 16:03:41 -080032 IntrinsicHelper& intrinsic_helper)
33 : LLVMIRBuilder(context), module_(&module), mdb_(context), java_object_type_(NULL),
34 java_method_type_(NULL), java_thread_type_(NULL), intrinsic_helper_(intrinsic_helper) {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080035 // Get java object type from module
Ian Rogers4c1c2832013-03-04 18:30:13 -080036 ::llvm::Type* jobject_struct_type = module.getTypeByName("JavaObject");
Logan Chien1b0a1b72012-03-15 06:20:17 +080037 CHECK(jobject_struct_type != NULL);
Ian Rogers76ae4fe2013-02-27 16:03:41 -080038 java_object_type_ = jobject_struct_type->getPointerTo();
39
40 // If type of Method is not explicitly defined in the module, use JavaObject*
Ian Rogers4c1c2832013-03-04 18:30:13 -080041 ::llvm::Type* type = module.getTypeByName("Method");
Ian Rogers76ae4fe2013-02-27 16:03:41 -080042 if (type != NULL) {
43 java_method_type_ = type->getPointerTo();
44 } else {
45 java_method_type_ = java_object_type_;
46 }
47
48 // If type of Thread is not explicitly defined in the module, use JavaObject*
49 type = module.getTypeByName("Thread");
50 if (type != NULL) {
51 java_thread_type_ = type->getPointerTo();
52 } else {
53 java_thread_type_ = java_object_type_;
54 }
Logan Chien42e0e152012-01-13 15:42:36 +080055
Logan Chienf04364f2012-02-10 12:01:39 +080056 // Create JEnv* type
Ian Rogers4c1c2832013-03-04 18:30:13 -080057 ::llvm::Type* jenv_struct_type = ::llvm::StructType::create(context, "JEnv");
Logan Chienf04364f2012-02-10 12:01:39 +080058 jenv_type_ = jenv_struct_type->getPointerTo();
59
Logan Chien8dfcbea2012-02-17 18:50:32 +080060 // Get Art shadow frame struct type from module
Logan Chien1b0a1b72012-03-15 06:20:17 +080061 art_frame_type_ = module.getTypeByName("ShadowFrame");
62 CHECK(art_frame_type_ != NULL);
Logan Chien8dfcbea2012-02-17 18:50:32 +080063
TDYa127d668a062012-04-13 12:36:57 -070064 runtime_support_ = NULL;
Shih-wei Liaod1fec812012-02-13 09:51:10 -080065}
66
67
68//----------------------------------------------------------------------------
69// Type Helper Function
70//----------------------------------------------------------------------------
71
Ian Rogers4c1c2832013-03-04 18:30:13 -080072::llvm::Type* IRBuilder::getJType(JType jty) {
Shih-wei Liaod1fec812012-02-13 09:51:10 -080073 switch (jty) {
74 case kVoid:
75 return getJVoidTy();
76
77 case kBoolean:
78 return getJBooleanTy();
79
80 case kByte:
81 return getJByteTy();
82
83 case kChar:
84 return getJCharTy();
85
86 case kShort:
87 return getJShortTy();
88
89 case kInt:
90 return getJIntTy();
91
92 case kLong:
93 return getJLongTy();
94
95 case kFloat:
96 return getJFloatTy();
97
98 case kDouble:
99 return getJDoubleTy();
100
101 case kObject:
102 return getJObjectTy();
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800103
TDYa127706e7db2012-05-06 00:05:33 -0700104 default:
105 LOG(FATAL) << "Unknown java type: " << jty;
106 return NULL;
107 }
Shih-wei Liaod1fec812012-02-13 09:51:10 -0800108}
109
Ian Rogers4c1c2832013-03-04 18:30:13 -0800110::llvm::StructType* IRBuilder::getShadowFrameTy(uint32_t vreg_size) {
TDYa127ce4cc0d2012-11-18 16:59:53 -0800111 std::string name(StringPrintf("ShadowFrame%u", vreg_size));
Logan Chien8dfcbea2012-02-17 18:50:32 +0800112
113 // Try to find the existing struct type definition
Ian Rogers4c1c2832013-03-04 18:30:13 -0800114 if (::llvm::Type* type = module_->getTypeByName(name)) {
115 CHECK(::llvm::isa< ::llvm::StructType>(type));
116 return static_cast< ::llvm::StructType*>(type);
Logan Chien8dfcbea2012-02-17 18:50:32 +0800117 }
118
119 // Create new struct type definition
Ian Rogers4c1c2832013-03-04 18:30:13 -0800120 ::llvm::Type* elem_types[] = {
Logan Chien8dfcbea2012-02-17 18:50:32 +0800121 art_frame_type_,
Ian Rogers4c1c2832013-03-04 18:30:13 -0800122 ::llvm::ArrayType::get(getInt32Ty(), vreg_size),
Logan Chien8dfcbea2012-02-17 18:50:32 +0800123 };
124
Ian Rogers4c1c2832013-03-04 18:30:13 -0800125 return ::llvm::StructType::create(elem_types, name);
Logan Chien8dfcbea2012-02-17 18:50:32 +0800126}
127
128
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700129} // namespace llvm
130} // namespace art