Shih-wei Liao | e94d9b2 | 2012-05-22 09:01:24 -0700 | [diff] [blame] | 1 | /* |
| 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_GREENLAND_INTRINSIC_HELPER_H_ |
| 18 | #define ART_SRC_GREENLAND_INTRINSIC_HELPER_H_ |
| 19 | |
| 20 | #include "logging.h" |
| 21 | |
| 22 | #include <llvm/ADT/DenseMap.h> |
| 23 | |
| 24 | namespace llvm { |
| 25 | class Function; |
| 26 | class FunctionType; |
| 27 | class LLVMContext; |
| 28 | class Module; |
| 29 | } |
| 30 | |
| 31 | namespace art { |
| 32 | namespace greenland { |
| 33 | |
| 34 | class IRBuilder; |
| 35 | |
| 36 | class IntrinsicHelper { |
| 37 | public: |
| 38 | enum IntrinsicId { |
| 39 | #define DEF_INTRINSICS_FUNC(ID, ...) ID, |
| 40 | #include "intrinsic_func_list.def" |
| 41 | MaxIntrinsicId, |
| 42 | |
| 43 | // Pseudo-intrinsics Id |
| 44 | UnknownId |
| 45 | }; |
| 46 | |
| 47 | enum IntrinsicAttribute { |
| 48 | kAttrNone = 0, |
| 49 | |
| 50 | // Intrinsic that doesn't modify the memory state |
| 51 | kAttrReadOnly = 1 << 0, |
| 52 | |
| 53 | // Intrinsic that never generates exception |
| 54 | kAttrNoThrow = 1 << 1, |
| 55 | }; |
| 56 | |
| 57 | enum IntrinsicValType { |
| 58 | kNone, |
| 59 | |
| 60 | kVoidTy, |
| 61 | |
| 62 | kJavaObjectTy, |
| 63 | kJavaMethodTy, |
| 64 | kJavaThreadTy, |
| 65 | |
| 66 | kInt1Ty, |
| 67 | kInt8Ty, |
| 68 | kInt16Ty, |
| 69 | kInt32Ty, |
| 70 | kInt64Ty, |
| 71 | |
| 72 | kInt1ConstantTy, |
| 73 | kInt8ConstantTy, |
| 74 | kInt16ConstantTy, |
| 75 | kInt32ConstantTy, |
| 76 | kInt64ConstantTy, |
| 77 | |
buzbee | 2cfc639 | 2012-05-07 14:51:40 -0700 | [diff] [blame] | 78 | kFloatTy, |
| 79 | kDoubleTy, |
| 80 | |
Shih-wei Liao | e94d9b2 | 2012-05-22 09:01:24 -0700 | [diff] [blame] | 81 | kVarArgTy, |
| 82 | }; |
| 83 | |
| 84 | enum { |
| 85 | kIntrinsicMaxArgc = 5 |
| 86 | }; |
| 87 | |
| 88 | typedef struct IntrinsicInfo { |
| 89 | const char* name_; |
| 90 | unsigned attr_; |
| 91 | IntrinsicValType ret_val_type_; |
| 92 | IntrinsicValType arg_type_[kIntrinsicMaxArgc]; |
| 93 | } IntrinsicInfo; |
| 94 | |
| 95 | private: |
| 96 | static const IntrinsicInfo Info[MaxIntrinsicId]; |
| 97 | |
| 98 | public: |
| 99 | static const IntrinsicInfo& GetInfo(IntrinsicId id) { |
| 100 | DCHECK(id >= 0 && id < MaxIntrinsicId) << "Unknown Dalvik intrinsics ID: " |
| 101 | << id; |
| 102 | return Info[id]; |
| 103 | } |
| 104 | |
| 105 | static const char* GetName(IntrinsicId id) { |
buzbee | 2cfc639 | 2012-05-07 14:51:40 -0700 | [diff] [blame] | 106 | return (id <= MaxIntrinsicId) ? GetInfo(id).name_ : "InvalidIntrinsic"; |
Shih-wei Liao | e94d9b2 | 2012-05-22 09:01:24 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | static unsigned GetAttr(IntrinsicId id) { |
| 110 | return GetInfo(id).attr_; |
| 111 | } |
| 112 | |
| 113 | public: |
| 114 | IntrinsicHelper(llvm::LLVMContext& context, llvm::Module& module); |
| 115 | |
| 116 | inline llvm::Function* GetIntrinsicFunction(IntrinsicId id) { |
| 117 | DCHECK(id >= 0 && id < MaxIntrinsicId) << "Unknown Dalvik intrinsics ID: " |
| 118 | << id; |
| 119 | return intrinsic_funcs_[id]; |
| 120 | } |
| 121 | |
| 122 | inline IntrinsicId GetIntrinsicId(const llvm::Function* func) const { |
| 123 | llvm::DenseMap<const llvm::Function*, IntrinsicId>::const_iterator |
| 124 | i = intrinsic_funcs_map_.find(func); |
| 125 | if (i == intrinsic_funcs_map_.end()) { |
| 126 | return UnknownId; |
| 127 | } else { |
| 128 | return i->second; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | private: |
| 133 | llvm::Function* intrinsic_funcs_[MaxIntrinsicId]; |
| 134 | |
| 135 | // Map a llvm::Function to its intrinsic id |
| 136 | llvm::DenseMap<const llvm::Function*, IntrinsicId> intrinsic_funcs_map_; |
| 137 | }; |
| 138 | |
| 139 | } // namespace greenland |
| 140 | } // namespace art |
| 141 | |
| 142 | #endif // ART_SRC_GREENLAND_INTRINSIC_HELPER_H_ |