Mangler.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // Unified name mangler for assembly backends.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/IR/Mangler.h"
  14. #include "llvm/ADT/SmallString.h"
  15. #include "llvm/ADT/Twine.h"
  16. #include "llvm/IR/DataLayout.h"
  17. #include "llvm/IR/DerivedTypes.h"
  18. #include "llvm/IR/Function.h"
  19. #include "llvm/IR/Module.h"
  20. #include "llvm/Support/raw_ostream.h"
  21. using namespace llvm;
  22. namespace {
  23. enum ManglerPrefixTy {
  24. Default, ///< Emit default string before each symbol.
  25. Private, ///< Emit "private" prefix before each symbol.
  26. LinkerPrivate ///< Emit "linker private" prefix before each symbol.
  27. };
  28. }
  29. static void getNameWithPrefixImpl(raw_ostream &OS, const Twine &GVName,
  30. ManglerPrefixTy PrefixTy,
  31. const DataLayout &DL, char Prefix) {
  32. SmallString<256> TmpData;
  33. StringRef Name = GVName.toStringRef(TmpData);
  34. assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
  35. // No need to do anything special if the global has the special "do not
  36. // mangle" flag in the name.
  37. if (Name[0] == '\1') {
  38. OS << Name.substr(1);
  39. return;
  40. }
  41. if (PrefixTy == Private)
  42. OS << DL.getPrivateGlobalPrefix();
  43. else if (PrefixTy == LinkerPrivate)
  44. OS << DL.getLinkerPrivateGlobalPrefix();
  45. if (Prefix != '\0')
  46. OS << Prefix;
  47. // If this is a simple string that doesn't need escaping, just append it.
  48. OS << Name;
  49. }
  50. static void getNameWithPrefixImpl(raw_ostream &OS, const Twine &GVName,
  51. const DataLayout &DL,
  52. ManglerPrefixTy PrefixTy) {
  53. char Prefix = DL.getGlobalPrefix();
  54. return getNameWithPrefixImpl(OS, GVName, PrefixTy, DL, Prefix);
  55. }
  56. void Mangler::getNameWithPrefix(raw_ostream &OS, const Twine &GVName,
  57. const DataLayout &DL) {
  58. return getNameWithPrefixImpl(OS, GVName, DL, Default);
  59. }
  60. void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
  61. const Twine &GVName, const DataLayout &DL) {
  62. raw_svector_ostream OS(OutName);
  63. char Prefix = DL.getGlobalPrefix();
  64. return getNameWithPrefixImpl(OS, GVName, Default, DL, Prefix);
  65. }
  66. static bool hasByteCountSuffix(CallingConv::ID CC) {
  67. switch (CC) {
  68. case CallingConv::X86_FastCall:
  69. case CallingConv::X86_StdCall:
  70. case CallingConv::X86_VectorCall:
  71. return true;
  72. default:
  73. return false;
  74. }
  75. }
  76. /// Microsoft fastcall and stdcall functions require a suffix on their name
  77. /// indicating the number of words of arguments they take.
  78. static void addByteCountSuffix(raw_ostream &OS, const Function *F,
  79. const DataLayout &DL) {
  80. // Calculate arguments size total.
  81. unsigned ArgWords = 0;
  82. for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
  83. AI != AE; ++AI) {
  84. Type *Ty = AI->getType();
  85. // 'Dereference' type in case of byval or inalloca parameter attribute.
  86. if (AI->hasByValOrInAllocaAttr())
  87. Ty = cast<PointerType>(Ty)->getElementType();
  88. // Size should be aligned to pointer size.
  89. unsigned PtrSize = DL.getPointerSize();
  90. ArgWords += RoundUpToAlignment(DL.getTypeAllocSize(Ty), PtrSize);
  91. }
  92. OS << '@' << ArgWords;
  93. }
  94. void Mangler::getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV,
  95. bool CannotUsePrivateLabel) const {
  96. ManglerPrefixTy PrefixTy = Default;
  97. if (GV->hasPrivateLinkage()) {
  98. if (CannotUsePrivateLabel)
  99. PrefixTy = LinkerPrivate;
  100. else
  101. PrefixTy = Private;
  102. }
  103. const DataLayout &DL = GV->getParent()->getDataLayout();
  104. if (!GV->hasName()) {
  105. // Get the ID for the global, assigning a new one if we haven't got one
  106. // already.
  107. unsigned &ID = AnonGlobalIDs[GV];
  108. if (ID == 0)
  109. ID = NextAnonGlobalID++;
  110. // Must mangle the global into a unique ID.
  111. getNameWithPrefixImpl(OS, "__unnamed_" + Twine(ID), DL, PrefixTy);
  112. return;
  113. }
  114. StringRef Name = GV->getName();
  115. char Prefix = DL.getGlobalPrefix();
  116. // Mangle functions with Microsoft calling conventions specially. Only do
  117. // this mangling for x86_64 vectorcall and 32-bit x86.
  118. const Function *MSFunc = dyn_cast<Function>(GV);
  119. if (Name.startswith("\01"))
  120. MSFunc = nullptr; // Don't mangle when \01 is present.
  121. CallingConv::ID CC =
  122. MSFunc ? MSFunc->getCallingConv() : (unsigned)CallingConv::C;
  123. if (!DL.hasMicrosoftFastStdCallMangling() &&
  124. CC != CallingConv::X86_VectorCall)
  125. MSFunc = nullptr;
  126. if (MSFunc) {
  127. if (CC == CallingConv::X86_FastCall)
  128. Prefix = '@'; // fastcall functions have an @ prefix instead of _.
  129. else if (CC == CallingConv::X86_VectorCall)
  130. Prefix = '\0'; // vectorcall functions have no prefix.
  131. }
  132. getNameWithPrefixImpl(OS, Name, PrefixTy, DL, Prefix);
  133. if (!MSFunc)
  134. return;
  135. // If we are supposed to add a microsoft-style suffix for stdcall, fastcall,
  136. // or vectorcall, add it. These functions have a suffix of @N where N is the
  137. // cumulative byte size of all of the parameters to the function in decimal.
  138. if (CC == CallingConv::X86_VectorCall)
  139. OS << '@'; // vectorcall functions use a double @ suffix.
  140. FunctionType *FT = MSFunc->getFunctionType();
  141. if (hasByteCountSuffix(CC) &&
  142. // "Pure" variadic functions do not receive @0 suffix.
  143. (!FT->isVarArg() || FT->getNumParams() == 0 ||
  144. (FT->getNumParams() == 1 && MSFunc->hasStructRetAttr())))
  145. addByteCountSuffix(OS, MSFunc, DL);
  146. }
  147. void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
  148. const GlobalValue *GV,
  149. bool CannotUsePrivateLabel) const {
  150. raw_svector_ostream OS(OutName);
  151. getNameWithPrefix(OS, GV, CannotUsePrivateLabel);
  152. }