| 1 |
//===--------------------------- Mangler.cpp -----------------------------===// |
| 2 |
// |
| 3 |
// SPIR Tools |
| 4 |
// |
| 5 |
// This file is distributed under the University of Illinois Open Source |
| 6 |
// License. See LICENSE.TXT for details. |
| 7 |
// |
| 8 |
//===---------------------------------------------------------------------===// |
| 9 |
/* |
| 10 |
* Contributed by: Intel Corporation. |
| 11 |
*/ |
| 12 |
|
| 13 |
#include "FunctionDescriptor.h" |
| 14 |
#include "ManglingUtils.h" |
| 15 |
#include "NameMangleAPI.h" |
| 16 |
#include "ParameterType.h" |
| 17 |
#include <algorithm> |
| 18 |
#include <map> |
| 19 |
#include <sstream> |
| 20 |
#include <string> |
| 21 |
|
| 22 |
// According to IA64 name mangling spec, |
| 23 |
// builtin vector types should not be substituted |
| 24 |
// This is a workaround till this gets fixed in CLang |
| 25 |
#define ENABLE_MANGLER_VECTOR_SUBSTITUTION 1 |
| 26 |
|
| 27 |
namespace SPIR { |
| 28 |
|
| 29 |
class MangleVisitor : public TypeVisitor { |
| 30 |
public: |
| 31 |
MangleVisitor(SPIRversion Ver, std::stringstream &S) |
| 32 |
: TypeVisitor(Ver), Stream(S), SeqId(0) {} |
| 33 |
|
| 34 |
// |
| 35 |
// mangle substitution methods |
| 36 |
// |
| 37 |
void mangleSequenceID(unsigned SeqID) { |
| 38 |
if (SeqID == 1) |
| 39 |
Stream << '0'; |
| 40 |
else if (SeqID > 1) { |
| 41 |
std::string Bstr; |
| 42 |
std::string Charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
| 43 |
SeqID--; |
| 44 |
Bstr.reserve(7); |
| 45 |
for (; SeqID != 0; SeqID /= 36) |
| 46 |
Bstr += Charset.substr(SeqID % 36, 1); |
| 47 |
std::reverse(Bstr.begin(), Bstr.end()); |
| 48 |
Stream << Bstr; |
| 49 |
} |
| 50 |
Stream << '_'; |
| 51 |
} |
| 52 |
|
| 53 |
bool mangleSubstitution(const ParamType *Type, std::string TypeStr) { |
| 54 |
size_t Fpos; |
| 55 |
std::stringstream ThistypeStr; |
| 56 |
ThistypeStr << TypeStr; |
| 57 |
if ((Fpos = Stream.str().find(TypeStr)) != std::string::npos) { |
| 58 |
const char *NType; |
| 59 |
if (const PointerType *P = SPIR::dynCast<PointerType>(Type)) { |
| 60 |
ThistypeStr << getPointeeMangling(P->getPointee()); |
| 61 |
} |
| 62 |
#if defined(ENABLE_MANGLER_VECTOR_SUBSTITUTION) |
| 63 |
else if (const VectorType *PVec = SPIR::dynCast<VectorType>(Type)) { |
| 64 |
if ((NType = mangledPrimitiveStringfromName( |
| 65 |
PVec->getScalarType()->toString()))) |
| 66 |
ThistypeStr << NType; |
| 67 |
} |
| 68 |
#endif |
| 69 |
std::map<std::string, unsigned>::iterator I = |
| 70 |
Substitutions.find(ThistypeStr.str()); |
| 71 |
if (I == Substitutions.end()) |
| 72 |
return false; |
| 73 |
|
| 74 |
unsigned SeqID = I->second; |
| 75 |
Stream << 'S'; |
| 76 |
mangleSequenceID(SeqID); |
| 77 |
return true; |
| 78 |
} |
| 79 |
return false; |
| 80 |
} |
| 81 |
|
| 82 |
// |
| 83 |
// Visit methods |
| 84 |
// |
| 85 |
MangleError visit(const PrimitiveType *T) override { |
| 86 |
MangleError Me = MANGLE_SUCCESS; |
| 87 |
std::string MangledPrimitive = |
| 88 |
std::string(mangledPrimitiveString(T->getPrimitive())); |
| 89 |
#if defined(SPIRV_SPIR20_MANGLING_REQUIREMENTS) |
| 90 |
Stream << MangledPrimitive; |
| 91 |
#else |
| 92 |
// Builtin primitives such as int are not substitution candidates, but |
| 93 |
// all other primitives are. Even though most of these do not appear |
| 94 |
// repeatedly in builtin function signatures, we need to track them in |
| 95 |
// the substitution map. |
| 96 |
if (T->getPrimitive() >= PRIMITIVE_STRUCT_FIRST) { |
| 97 |
if (!mangleSubstitution(T, MangledPrimitive)) { |
| 98 |
size_t Index = Stream.str().size(); |
| 99 |
Stream << MangledPrimitive; |
| 100 |
recordSubstitution(Stream.str().substr(Index)); |
| 101 |
} |
| 102 |
} else { |
| 103 |
Stream << MangledPrimitive; |
| 104 |
} |
| 105 |
#endif |
| 106 |
return Me; |
| 107 |
} |
| 108 |
|
| 109 |
MangleError visit(const PointerType *P) override { |
| 110 |
size_t Fpos = Stream.str().size(); |
| 111 |
MangleError Me = MANGLE_SUCCESS; |
| 112 |
std::string AttrMangling = getPointerAttributesMangling(P); |
| 113 |
if (!mangleSubstitution(P, "P" + AttrMangling)) { |
| 114 |
// A pointee type is substituted when it is a user type, a vector type |
| 115 |
// (but see a comment in the beginning of this file), a pointer type, |
| 116 |
// or a primitive type with qualifiers (addr. space and/or CV qualifiers). |
| 117 |
// So, stream "P", type qualifiers |
| 118 |
Stream << "P" << AttrMangling; |
| 119 |
// and the pointee type itself. |
| 120 |
Me = P->getPointee()->accept(this); |
| 121 |
// The type qualifiers plus a pointee type is a substitutable entity, but |
| 122 |
// only when there are qualifiers in the first place. |
| 123 |
if (!AttrMangling.empty()) |
| 124 |
recordSubstitution(Stream.str().substr(Fpos + 1)); |
| 125 |
// The complete pointer type is substitutable as well |
| 126 |
recordSubstitution(Stream.str().substr(Fpos)); |
| 127 |
} |
| 128 |
return Me; |
| 129 |
} |
| 130 |
|
| 131 |
MangleError visit(const VectorType *V) override { |
| 132 |
size_t Index = Stream.str().size(); |
| 133 |
std::stringstream TypeStr; |
| 134 |
TypeStr << "Dv" << V->getLength() << "_"; |
| 135 |
MangleError Me = MANGLE_SUCCESS; |
| 136 |
#if defined(ENABLE_MANGLER_VECTOR_SUBSTITUTION) |
| 137 |
if (!mangleSubstitution(V, TypeStr.str())) |
| 138 |
#endif |
| 139 |
{ |
| 140 |
Stream << TypeStr.str(); |
| 141 |
Me = V->getScalarType()->accept(this); |
| 142 |
recordSubstitution(Stream.str().substr(Index)); |
| 143 |
} |
| 144 |
return Me; |
| 145 |
} |
| 146 |
|
| 147 |
MangleError visit(const AtomicType *P) override { |
| 148 |
MangleError Me = MANGLE_SUCCESS; |
| 149 |
size_t Index = Stream.str().size(); |
| 150 |
const char *TypeStr = "U7_Atomic"; |
| 151 |
if (!mangleSubstitution(P, TypeStr)) { |
| 152 |
Stream << TypeStr; |
| 153 |
Me = P->getBaseType()->accept(this); |
| 154 |
recordSubstitution(Stream.str().substr(Index)); |
| 155 |
} |
| 156 |
return Me; |
| 157 |
} |
| 158 |
|
| 159 |
MangleError visit(const BlockType *P) override { |
| 160 |
Stream << "U" |
| 161 |
<< "13block_pointerFv"; |
| 162 |
if (P->getNumOfParams() == 0) |
| 163 |
Stream << "v"; |
| 164 |
else |
| 165 |
for (unsigned int I = 0; I < P->getNumOfParams(); ++I) { |
| 166 |
MangleError Err = P->getParam(I)->accept(this); |
| 167 |
if (Err != MANGLE_SUCCESS) { |
| 168 |
return Err; |
| 169 |
} |
| 170 |
} |
| 171 |
Stream << "E"; |
| 172 |
// "Add" the function type (FvvE) and U13block_pointerFvvE to the |
| 173 |
// substitution table. We don't actually substitute this if it's present, |
| 174 |
// but since the block type only occurs at most once in any function we care |
| 175 |
// about, this should be sufficient. |
| 176 |
SeqId += 2; |
| 177 |
return MANGLE_SUCCESS; |
| 178 |
} |
| 179 |
|
| 180 |
MangleError visit(const UserDefinedType *PTy) override { |
| 181 |
size_t Index = Stream.str().size(); |
| 182 |
std::string Name = PTy->toString(); |
| 183 |
if (!mangleSubstitution(PTy, Name)) { |
| 184 |
Stream << Name.size() << Name; |
| 185 |
recordSubstitution(Stream.str().substr(Index)); |
| 186 |
} |
| 187 |
return MANGLE_SUCCESS; |
| 188 |
} |
| 189 |
|
| 190 |
private: |
| 191 |
void recordSubstitution(const std::string &Str) { |
| 192 |
Substitutions[Str] = SeqId++; |
| 193 |
} |
| 194 |
|
| 195 |
// Holds the mangled string representing the prototype of the function. |
| 196 |
std::stringstream &Stream; |
| 197 |
unsigned SeqId; |
| 198 |
std::map<std::string, unsigned> Substitutions; |
| 199 |
}; |
| 200 |
|
| 201 |
// |
| 202 |
// NameMangler |
| 203 |
// |
| 204 |
NameMangler::NameMangler(SPIRversion Version) : SpirVersion(Version) {} |
| 205 |
|
| 206 |
MangleError NameMangler::mangle(const FunctionDescriptor &Fd, |
| 207 |
std::string &MangledName) { |
| 208 |
if (Fd.isNull()) { |
| 209 |
MangledName.assign(FunctionDescriptor::nullString()); |
| 210 |
return MANGLE_NULL_FUNC_DESCRIPTOR; |
| 211 |
} |
| 212 |
std::stringstream Ret; |
| 213 |
Ret << "_Z" << Fd.Name.length() << Fd.Name; |
| 214 |
MangleVisitor Visitor(SpirVersion, Ret); |
| 215 |
for (unsigned int I = 0; I < Fd.Parameters.size(); ++I) { |
| 216 |
MangleError Err = Fd.Parameters[I]->accept(&Visitor); |
| 217 |
if (Err == MANGLE_TYPE_NOT_SUPPORTED) { |
| 218 |
MangledName.assign("Type "); |
| 219 |
MangledName.append(Fd.Parameters[I]->toString()); |
| 220 |
MangledName.append(" is not supported in "); |
| 221 |
std::string Ver = getSPIRVersionAsString(SpirVersion); |
| 222 |
MangledName.append(Ver); |
| 223 |
return Err; |
| 224 |
} |
| 225 |
} |
| 226 |
MangledName.assign(Ret.str()); |
| 227 |
return MANGLE_SUCCESS; |
| 228 |
} |
| 229 |
|
| 230 |
} // namespace SPIR |