blob: 72c43867488c7885e274414cbe777a948dad6c1d [file] [log] [blame]
alan-bakerf67468c2019-11-25 15:51:49 -05001// 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
SJW173c7e92020-03-16 08:44:47 -050018#include <string>
19
alan-bakerf67468c2019-11-25 15:51:49 -050020#include "llvm/ADT/StringRef.h"
21#include "llvm/IR/Function.h"
22
SJW173c7e92020-03-16 08:44:47 -050023#include "BuiltinsEnum.h"
24
SJW61531372020-06-09 07:31:08 -050025#define BUILTIN_IN_GROUP(BUILTIN, GROUP) \
26 (BUILTIN > Builtins::kType_##GROUP##_Start && \
27 BUILTIN < Builtins::kType_##GROUP##_End)
28
alan-bakerf67468c2019-11-25 15:51:49 -050029namespace clspv {
30
SJW173c7e92020-03-16 08:44:47 -050031namespace Builtins {
32
33struct ParamTypeInfo {
34 bool is_signed = false; // is element type signed
35 llvm::Type::TypeID type_id = llvm::Type::VoidTyID; // element type
SJW2c317da2020-03-23 07:39:13 -050036 uint32_t byte_len = 0; // element byte length
SJW173c7e92020-03-16 08:44:47 -050037 int vector_size = 0; // number of elements (0 == not a vector)
38 std::string name; // struct name
SJW2c317da2020-03-23 07:39:13 -050039
40 bool isSampler() const;
SJW173c7e92020-03-16 08:44:47 -050041};
42
43class 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
51public:
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 Antogninid9ccec72020-12-01 16:52:34 +000060 ParamTypeInfo &getParameter(size_t arg);
SJW173c7e92020-03-16 08:44:47 -050061 const ParamTypeInfo &getLastParameter() const { return params_.back(); }
62 size_t getParameterCount() const { return params_.size(); }
63 const ParamTypeInfo &getReturnType() const { return return_type_; }
64
65private:
66 bool GetFromMangledNameCheck(const std::string &mangled_name);
67};
68
69/// Primary Interface
70// returns a FunctionInfo representation of the mangled name
71const FunctionInfo &Lookup(const std::string &mangled_name);
72inline const FunctionInfo &Lookup(llvm::StringRef mangled_name) {
73 return Lookup(mangled_name.str());
74}
75inline const FunctionInfo &Lookup(llvm::Function *func) {
76 return Lookup(func->getName());
77}
78
SJW61531372020-06-09 07:31:08 -050079// Generate a mangled name loosely based on Itanium mangled naming but
80// reversible by GetFromMangledName
81std::string GetMangledFunctionName(const char *name, llvm::Type *type);
alan-bakerf67468c2019-11-25 15:51:49 -050082
SJW61531372020-06-09 07:31:08 -050083std::string GetMangledFunctionName(const char *name);
alan-bakerf67468c2019-11-25 15:51:49 -050084
Marco Antogninid9ccec72020-12-01 16:52:34 +000085std::string GetMangledFunctionName(const FunctionInfo &Info);
86
SJW61531372020-06-09 07:31:08 -050087std::string GetMangledTypeName(llvm::Type *T);
alan-bakerce179f12019-12-06 19:02:22 -050088
SJW173c7e92020-03-16 08:44:47 -050089} // namespace Builtins
90
alan-bakerf67468c2019-11-25 15:51:49 -050091} // namespace clspv
92
93#endif // CLSPV_LIB_BUILTINS_H_