blob: 41e11b3945e40b5c573657dcf294251513e622ac [file] [log] [blame]
Brian Gaeked4dff192003-07-24 20:20:58 +00001//===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Brian Gaeked4dff192003-07-24 20:20:58 +00009//
Chris Lattner1376b022010-01-16 21:08:46 +000010// Unified name mangler for assembly backends.
Brian Gaeked4dff192003-07-24 20:20:58 +000011//
12//===----------------------------------------------------------------------===//
13
Rafael Espindola894843c2014-01-07 21:19:40 +000014#include "llvm/IR/Mangler.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/ADT/SmallString.h"
16#include "llvm/ADT/Twine.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000017#include "llvm/IR/DataLayout.h"
18#include "llvm/IR/DerivedTypes.h"
19#include "llvm/IR/Function.h"
Rafael Espindolac233f742015-06-23 13:59:29 +000020#include "llvm/IR/Module.h"
Chris Lattner8d99c762010-03-12 21:03:47 +000021#include "llvm/Support/raw_ostream.h"
Chris Lattnerc9499b62003-12-14 21:35:53 +000022using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000023
Rafael Espindola8c5f5372015-06-23 14:11:09 +000024namespace {
25enum ManglerPrefixTy {
26 Default, ///< Emit default string before each symbol.
27 Private, ///< Emit "private" prefix before each symbol.
28 LinkerPrivate ///< Emit "linker private" prefix before each symbol.
29};
30}
31
Rafael Espindolace4c2bc2015-06-23 12:21:54 +000032static void getNameWithPrefixImpl(raw_ostream &OS, const Twine &GVName,
Rafael Espindola8c5f5372015-06-23 14:11:09 +000033 ManglerPrefixTy PrefixTy,
Rafael Espindolace4c2bc2015-06-23 12:21:54 +000034 const DataLayout &DL, char Prefix) {
Chris Lattner33535b32010-01-13 07:01:09 +000035 SmallString<256> TmpData;
Benjamin Kramer2e06b932010-01-13 12:45:23 +000036 StringRef Name = GVName.toStringRef(TmpData);
Chris Lattner33535b32010-01-13 07:01:09 +000037 assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
Rafael Espindola58873562014-01-03 19:21:54 +000038
Rafael Espindola5d457de2014-08-01 14:16:40 +000039 // No need to do anything special if the global has the special "do not
40 // mangle" flag in the name.
41 if (Name[0] == '\1') {
42 OS << Name.substr(1);
43 return;
44 }
45
Rafael Espindola8c5f5372015-06-23 14:11:09 +000046 if (PrefixTy == Private)
Rafael Espindola310f5012014-01-29 02:30:38 +000047 OS << DL.getPrivateGlobalPrefix();
Rafael Espindola8c5f5372015-06-23 14:11:09 +000048 else if (PrefixTy == LinkerPrivate)
Rafael Espindola310f5012014-01-29 02:30:38 +000049 OS << DL.getLinkerPrivateGlobalPrefix();
Chris Lattnerb4ffc892010-01-17 18:22:35 +000050
Reid Kleckner9ccce992014-10-28 01:29:26 +000051 if (Prefix != '\0')
52 OS << Prefix;
Rafael Espindolafdc88132013-11-13 14:01:59 +000053
Chris Lattner83e872e2010-01-17 19:23:46 +000054 // If this is a simple string that doesn't need escaping, just append it.
Rafael Espindola310f5012014-01-29 02:30:38 +000055 OS << Name;
56}
57
Rafael Espindola8c5f5372015-06-23 14:11:09 +000058static void getNameWithPrefixImpl(raw_ostream &OS, const Twine &GVName,
59 const DataLayout &DL,
60 ManglerPrefixTy PrefixTy) {
Rafael Espindolac233f742015-06-23 13:59:29 +000061 char Prefix = DL.getGlobalPrefix();
62 return getNameWithPrefixImpl(OS, GVName, PrefixTy, DL, Prefix);
Rafael Espindolace4c2bc2015-06-23 12:21:54 +000063}
64
Rafael Espindola8c5f5372015-06-23 14:11:09 +000065void Mangler::getNameWithPrefix(raw_ostream &OS, const Twine &GVName,
66 const DataLayout &DL) {
67 return getNameWithPrefixImpl(OS, GVName, DL, Default);
68}
69
Rafael Espindolace4c2bc2015-06-23 12:21:54 +000070void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
Rafael Espindola8c5f5372015-06-23 14:11:09 +000071 const Twine &GVName, const DataLayout &DL) {
Rafael Espindolace4c2bc2015-06-23 12:21:54 +000072 raw_svector_ostream OS(OutName);
73 char Prefix = DL.getGlobalPrefix();
Rafael Espindola8c5f5372015-06-23 14:11:09 +000074 return getNameWithPrefixImpl(OS, GVName, Default, DL, Prefix);
Chris Lattner33535b32010-01-13 07:01:09 +000075}
76
Reid Kleckner9ccce992014-10-28 01:29:26 +000077static bool hasByteCountSuffix(CallingConv::ID CC) {
78 switch (CC) {
79 case CallingConv::X86_FastCall:
80 case CallingConv::X86_StdCall:
81 case CallingConv::X86_VectorCall:
82 return true;
83 default:
84 return false;
85 }
86}
87
88/// Microsoft fastcall and stdcall functions require a suffix on their name
89/// indicating the number of words of arguments they take.
90static void addByteCountSuffix(raw_ostream &OS, const Function *F,
Mehdi Aminia28d91d2015-03-10 02:37:25 +000091 const DataLayout &DL) {
Chris Lattner8d99c762010-03-12 21:03:47 +000092 // Calculate arguments size total.
93 unsigned ArgWords = 0;
94 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
95 AI != AE; ++AI) {
Chris Lattner229907c2011-07-18 04:54:35 +000096 Type *Ty = AI->getType();
Reid Klecknerf5b76512014-01-31 23:50:57 +000097 // 'Dereference' type in case of byval or inalloca parameter attribute.
98 if (AI->hasByValOrInAllocaAttr())
Chris Lattner8d99c762010-03-12 21:03:47 +000099 Ty = cast<PointerType>(Ty)->getElementType();
Reid Kleckner9ccce992014-10-28 01:29:26 +0000100 // Size should be aligned to pointer size.
Mehdi Aminia28d91d2015-03-10 02:37:25 +0000101 unsigned PtrSize = DL.getPointerSize();
Rui Ueyamada00f2f2016-01-14 21:06:47 +0000102 ArgWords += alignTo(DL.getTypeAllocSize(Ty), PtrSize);
Chris Lattner8d99c762010-03-12 21:03:47 +0000103 }
Rafael Espindola310f5012014-01-29 02:30:38 +0000104
105 OS << '@' << ArgWords;
Chris Lattner8d99c762010-03-12 21:03:47 +0000106}
107
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000108void Mangler::getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV,
David Majnemer21fecf92015-03-17 20:40:21 +0000109 bool CannotUsePrivateLabel) const {
Rafael Espindola8c5f5372015-06-23 14:11:09 +0000110 ManglerPrefixTy PrefixTy = Default;
David Majnemer21fecf92015-03-17 20:40:21 +0000111 if (GV->hasPrivateLinkage()) {
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000112 if (CannotUsePrivateLabel)
Rafael Espindola8c5f5372015-06-23 14:11:09 +0000113 PrefixTy = LinkerPrivate;
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000114 else
Rafael Espindola8c5f5372015-06-23 14:11:09 +0000115 PrefixTy = Private;
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000116 }
Nico Rieckda881a22014-01-14 11:53:26 +0000117
Rafael Espindolac233f742015-06-23 13:59:29 +0000118 const DataLayout &DL = GV->getParent()->getDataLayout();
Rafael Espindola310f5012014-01-29 02:30:38 +0000119 if (!GV->hasName()) {
Chris Lattner8d99c762010-03-12 21:03:47 +0000120 // Get the ID for the global, assigning a new one if we haven't got one
121 // already.
122 unsigned &ID = AnonGlobalIDs[GV];
Rafael Espindola310f5012014-01-29 02:30:38 +0000123 if (ID == 0)
Eric Christopher1c3bb792016-09-29 02:03:50 +0000124 ID = AnonGlobalIDs.size();
Rafael Espindola310f5012014-01-29 02:30:38 +0000125
Chris Lattner8d99c762010-03-12 21:03:47 +0000126 // Must mangle the global into a unique ID.
Rafael Espindola8c5f5372015-06-23 14:11:09 +0000127 getNameWithPrefixImpl(OS, "__unnamed_" + Twine(ID), DL, PrefixTy);
Rafael Espindola310f5012014-01-29 02:30:38 +0000128 return;
Chris Lattner8d99c762010-03-12 21:03:47 +0000129 }
Rafael Espindola58873562014-01-03 19:21:54 +0000130
Rafael Espindola310f5012014-01-29 02:30:38 +0000131 StringRef Name = GV->getName();
Rafael Espindolac233f742015-06-23 13:59:29 +0000132 char Prefix = DL.getGlobalPrefix();
Rafael Espindola310f5012014-01-29 02:30:38 +0000133
Reid Kleckner9ccce992014-10-28 01:29:26 +0000134 // Mangle functions with Microsoft calling conventions specially. Only do
135 // this mangling for x86_64 vectorcall and 32-bit x86.
136 const Function *MSFunc = dyn_cast<Function>(GV);
137 if (Name.startswith("\01"))
138 MSFunc = nullptr; // Don't mangle when \01 is present.
Aaron Ballman5af8ba42014-10-28 13:12:13 +0000139 CallingConv::ID CC =
140 MSFunc ? MSFunc->getCallingConv() : (unsigned)CallingConv::C;
Rafael Espindolac233f742015-06-23 13:59:29 +0000141 if (!DL.hasMicrosoftFastStdCallMangling() &&
Reid Kleckner9ccce992014-10-28 01:29:26 +0000142 CC != CallingConv::X86_VectorCall)
143 MSFunc = nullptr;
144 if (MSFunc) {
145 if (CC == CallingConv::X86_FastCall)
146 Prefix = '@'; // fastcall functions have an @ prefix instead of _.
147 else if (CC == CallingConv::X86_VectorCall)
148 Prefix = '\0'; // vectorcall functions have no prefix.
Rafael Espindola310f5012014-01-29 02:30:38 +0000149 }
150
Rafael Espindolac233f742015-06-23 13:59:29 +0000151 getNameWithPrefixImpl(OS, Name, PrefixTy, DL, Prefix);
Rafael Espindola310f5012014-01-29 02:30:38 +0000152
153 if (!MSFunc)
154 return;
155
Reid Kleckner9ccce992014-10-28 01:29:26 +0000156 // If we are supposed to add a microsoft-style suffix for stdcall, fastcall,
157 // or vectorcall, add it. These functions have a suffix of @N where N is the
158 // cumulative byte size of all of the parameters to the function in decimal.
159 if (CC == CallingConv::X86_VectorCall)
160 OS << '@'; // vectorcall functions use a double @ suffix.
Rafael Espindola310f5012014-01-29 02:30:38 +0000161 FunctionType *FT = MSFunc->getFunctionType();
Reid Kleckner9ccce992014-10-28 01:29:26 +0000162 if (hasByteCountSuffix(CC) &&
Rafael Espindola310f5012014-01-29 02:30:38 +0000163 // "Pure" variadic functions do not receive @0 suffix.
164 (!FT->isVarArg() || FT->getNumParams() == 0 ||
165 (FT->getNumParams() == 1 && MSFunc->hasStructRetAttr())))
Rafael Espindolac233f742015-06-23 13:59:29 +0000166 addByteCountSuffix(OS, MSFunc, DL);
Rafael Espindola310f5012014-01-29 02:30:38 +0000167}
168
169void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000170 const GlobalValue *GV,
David Majnemer21fecf92015-03-17 20:40:21 +0000171 bool CannotUsePrivateLabel) const {
Rafael Espindola310f5012014-01-29 02:30:38 +0000172 raw_svector_ostream OS(OutName);
David Majnemer21fecf92015-03-17 20:40:21 +0000173 getNameWithPrefix(OS, GV, CannotUsePrivateLabel);
Chris Lattner840c8d72009-09-11 05:40:42 +0000174}