alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 1 | // Copyright 2019 The Clspv Authors. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #ifndef CLSPV_LIB_BUILTINS_H_ |
| 16 | #define CLSPV_LIB_BUILTINS_H_ |
| 17 | |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 18 | #include <string> |
| 19 | |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 20 | #include "llvm/ADT/StringRef.h" |
| 21 | #include "llvm/IR/Function.h" |
| 22 | |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 23 | #include "BuiltinsEnum.h" |
| 24 | |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 25 | #define BUILTIN_IN_GROUP(BUILTIN, GROUP) \ |
| 26 | (BUILTIN > Builtins::kType_##GROUP##_Start && \ |
| 27 | BUILTIN < Builtins::kType_##GROUP##_End) |
| 28 | |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 29 | namespace clspv { |
| 30 | |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 31 | namespace Builtins { |
| 32 | |
| 33 | struct ParamTypeInfo { |
| 34 | bool is_signed = false; // is element type signed |
| 35 | llvm::Type::TypeID type_id = llvm::Type::VoidTyID; // element type |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 36 | uint32_t byte_len = 0; // element byte length |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 37 | int vector_size = 0; // number of elements (0 == not a vector) |
| 38 | std::string name; // struct name |
SJW | 2c317da | 2020-03-23 07:39:13 -0500 | [diff] [blame] | 39 | |
| 40 | bool isSampler() const; |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | class FunctionInfo { |
| 44 | bool is_valid_ = false; |
| 45 | Builtins::BuiltinType type_ = Builtins::kBuiltinNone; |
| 46 | std::string name_; |
| 47 | ParamTypeInfo return_type_; // only used for convert, where return type is |
| 48 | // embedded in the name |
| 49 | std::vector<ParamTypeInfo> params_; |
| 50 | |
| 51 | public: |
| 52 | FunctionInfo() = default; |
| 53 | FunctionInfo(const std::string &_name); |
| 54 | |
| 55 | bool isValid() const { return is_valid_; } |
| 56 | Builtins::BuiltinType getType() const { return type_; } |
| 57 | operator int() const { return type_; } |
| 58 | const std::string &getName() const { return name_; } |
| 59 | const ParamTypeInfo &getParameter(size_t arg) const; |
Marco Antognini | d9ccec7 | 2020-12-01 16:52:34 +0000 | [diff] [blame] | 60 | ParamTypeInfo &getParameter(size_t arg); |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 61 | const ParamTypeInfo &getLastParameter() const { return params_.back(); } |
| 62 | size_t getParameterCount() const { return params_.size(); } |
| 63 | const ParamTypeInfo &getReturnType() const { return return_type_; } |
| 64 | |
| 65 | private: |
| 66 | bool GetFromMangledNameCheck(const std::string &mangled_name); |
| 67 | }; |
| 68 | |
| 69 | /// Primary Interface |
| 70 | // returns a FunctionInfo representation of the mangled name |
| 71 | const FunctionInfo &Lookup(const std::string &mangled_name); |
| 72 | inline const FunctionInfo &Lookup(llvm::StringRef mangled_name) { |
| 73 | return Lookup(mangled_name.str()); |
| 74 | } |
| 75 | inline const FunctionInfo &Lookup(llvm::Function *func) { |
| 76 | return Lookup(func->getName()); |
| 77 | } |
| 78 | |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 79 | // Generate a mangled name loosely based on Itanium mangled naming but |
| 80 | // reversible by GetFromMangledName |
| 81 | std::string GetMangledFunctionName(const char *name, llvm::Type *type); |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 82 | |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 83 | std::string GetMangledFunctionName(const char *name); |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 84 | |
Marco Antognini | d9ccec7 | 2020-12-01 16:52:34 +0000 | [diff] [blame] | 85 | std::string GetMangledFunctionName(const FunctionInfo &Info); |
| 86 | |
SJW | 6153137 | 2020-06-09 07:31:08 -0500 | [diff] [blame] | 87 | std::string GetMangledTypeName(llvm::Type *T); |
alan-baker | ce179f1 | 2019-12-06 19:02:22 -0500 | [diff] [blame] | 88 | |
SJW | 173c7e9 | 2020-03-16 08:44:47 -0500 | [diff] [blame] | 89 | } // namespace Builtins |
| 90 | |
alan-baker | f67468c | 2019-11-25 15:51:49 -0500 | [diff] [blame] | 91 | } // namespace clspv |
| 92 | |
| 93 | #endif // CLSPV_LIB_BUILTINS_H_ |