blob: 5eeb7978e2f3c369da86ff88f8e2e5e8c1200615 [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"
Chris Lattner8d99c762010-03-12 21:03:47 +000020#include "llvm/Support/raw_ostream.h"
Chris Lattnerc9499b62003-12-14 21:35:53 +000021using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000022
Rafael Espindola310f5012014-01-29 02:30:38 +000023static void getNameWithPrefixx(raw_ostream &OS, const Twine &GVName,
24 Mangler::ManglerPrefixTy PrefixTy,
Reid Kleckner9ccce992014-10-28 01:29:26 +000025 const DataLayout &DL, char Prefix) {
Chris Lattner33535b32010-01-13 07:01:09 +000026 SmallString<256> TmpData;
Benjamin Kramer2e06b932010-01-13 12:45:23 +000027 StringRef Name = GVName.toStringRef(TmpData);
Chris Lattner33535b32010-01-13 07:01:09 +000028 assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
Rafael Espindola58873562014-01-03 19:21:54 +000029
Rafael Espindola5d457de2014-08-01 14:16:40 +000030 // No need to do anything special if the global has the special "do not
31 // mangle" flag in the name.
32 if (Name[0] == '\1') {
33 OS << Name.substr(1);
34 return;
35 }
36
Rafael Espindola310f5012014-01-29 02:30:38 +000037 if (PrefixTy == Mangler::Private)
38 OS << DL.getPrivateGlobalPrefix();
39 else if (PrefixTy == Mangler::LinkerPrivate)
40 OS << DL.getLinkerPrivateGlobalPrefix();
Chris Lattnerb4ffc892010-01-17 18:22:35 +000041
Reid Kleckner9ccce992014-10-28 01:29:26 +000042 if (Prefix != '\0')
43 OS << Prefix;
Rafael Espindolafdc88132013-11-13 14:01:59 +000044
Chris Lattner83e872e2010-01-17 19:23:46 +000045 // If this is a simple string that doesn't need escaping, just append it.
Rafael Espindola310f5012014-01-29 02:30:38 +000046 OS << Name;
47}
48
Rafael Espindolaefedd3a2014-02-10 21:25:13 +000049void Mangler::getNameWithPrefix(raw_ostream &OS, const Twine &GVName,
50 ManglerPrefixTy PrefixTy) const {
Reid Kleckner9ccce992014-10-28 01:29:26 +000051 char Prefix = DL->getGlobalPrefix();
52 return getNameWithPrefixx(OS, GVName, PrefixTy, *DL, Prefix);
Rafael Espindola310f5012014-01-29 02:30:38 +000053}
54
55void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
Rafael Espindolaefedd3a2014-02-10 21:25:13 +000056 const Twine &GVName,
57 ManglerPrefixTy PrefixTy) const {
Rafael Espindola310f5012014-01-29 02:30:38 +000058 raw_svector_ostream OS(OutName);
59 return getNameWithPrefix(OS, GVName, PrefixTy);
Chris Lattner33535b32010-01-13 07:01:09 +000060}
61
Reid Kleckner9ccce992014-10-28 01:29:26 +000062static bool hasByteCountSuffix(CallingConv::ID CC) {
63 switch (CC) {
64 case CallingConv::X86_FastCall:
65 case CallingConv::X86_StdCall:
66 case CallingConv::X86_VectorCall:
67 return true;
68 default:
69 return false;
70 }
71}
72
73/// Microsoft fastcall and stdcall functions require a suffix on their name
74/// indicating the number of words of arguments they take.
75static void addByteCountSuffix(raw_ostream &OS, const Function *F,
76 const DataLayout &TD) {
Chris Lattner8d99c762010-03-12 21:03:47 +000077 // Calculate arguments size total.
78 unsigned ArgWords = 0;
79 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
80 AI != AE; ++AI) {
Chris Lattner229907c2011-07-18 04:54:35 +000081 Type *Ty = AI->getType();
Reid Klecknerf5b76512014-01-31 23:50:57 +000082 // 'Dereference' type in case of byval or inalloca parameter attribute.
83 if (AI->hasByValOrInAllocaAttr())
Chris Lattner8d99c762010-03-12 21:03:47 +000084 Ty = cast<PointerType>(Ty)->getElementType();
Reid Kleckner9ccce992014-10-28 01:29:26 +000085 // Size should be aligned to pointer size.
86 unsigned PtrSize = TD.getPointerSize();
87 ArgWords += RoundUpToAlignment(TD.getTypeAllocSize(Ty), PtrSize);
Chris Lattner8d99c762010-03-12 21:03:47 +000088 }
Rafael Espindola310f5012014-01-29 02:30:38 +000089
90 OS << '@' << ArgWords;
Chris Lattner8d99c762010-03-12 21:03:47 +000091}
92
Rafael Espindoladaeafb42014-02-19 17:23:20 +000093void Mangler::getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV,
94 bool CannotUsePrivateLabel) const {
Chris Lattnerc25475e2010-01-17 18:52:16 +000095 ManglerPrefixTy PrefixTy = Mangler::Default;
Rafael Espindoladaeafb42014-02-19 17:23:20 +000096 if (GV->hasPrivateLinkage()) {
97 if (CannotUsePrivateLabel)
98 PrefixTy = Mangler::LinkerPrivate;
99 else
100 PrefixTy = Mangler::Private;
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000101 }
Nico Rieckda881a22014-01-14 11:53:26 +0000102
Rafael Espindola310f5012014-01-29 02:30:38 +0000103 if (!GV->hasName()) {
Chris Lattner8d99c762010-03-12 21:03:47 +0000104 // Get the ID for the global, assigning a new one if we haven't got one
105 // already.
106 unsigned &ID = AnonGlobalIDs[GV];
Rafael Espindola310f5012014-01-29 02:30:38 +0000107 if (ID == 0)
108 ID = NextAnonGlobalID++;
109
Chris Lattner8d99c762010-03-12 21:03:47 +0000110 // Must mangle the global into a unique ID.
Rafael Espindola310f5012014-01-29 02:30:38 +0000111 getNameWithPrefix(OS, "__unnamed_" + Twine(ID), PrefixTy);
112 return;
Chris Lattner8d99c762010-03-12 21:03:47 +0000113 }
Rafael Espindola58873562014-01-03 19:21:54 +0000114
Rafael Espindola310f5012014-01-29 02:30:38 +0000115 StringRef Name = GV->getName();
Reid Kleckner9ccce992014-10-28 01:29:26 +0000116 char Prefix = DL->getGlobalPrefix();
Rafael Espindola310f5012014-01-29 02:30:38 +0000117
Reid Kleckner9ccce992014-10-28 01:29:26 +0000118 // Mangle functions with Microsoft calling conventions specially. Only do
119 // this mangling for x86_64 vectorcall and 32-bit x86.
120 const Function *MSFunc = dyn_cast<Function>(GV);
121 if (Name.startswith("\01"))
122 MSFunc = nullptr; // Don't mangle when \01 is present.
Aaron Ballman5af8ba42014-10-28 13:12:13 +0000123 CallingConv::ID CC =
124 MSFunc ? MSFunc->getCallingConv() : (unsigned)CallingConv::C;
Reid Kleckner9ccce992014-10-28 01:29:26 +0000125 if (!DL->hasMicrosoftFastStdCallMangling() &&
126 CC != CallingConv::X86_VectorCall)
127 MSFunc = nullptr;
128 if (MSFunc) {
129 if (CC == CallingConv::X86_FastCall)
130 Prefix = '@'; // fastcall functions have an @ prefix instead of _.
131 else if (CC == CallingConv::X86_VectorCall)
132 Prefix = '\0'; // vectorcall functions have no prefix.
Rafael Espindola310f5012014-01-29 02:30:38 +0000133 }
134
Reid Kleckner9ccce992014-10-28 01:29:26 +0000135 getNameWithPrefixx(OS, Name, PrefixTy, *DL, Prefix);
Rafael Espindola310f5012014-01-29 02:30:38 +0000136
137 if (!MSFunc)
138 return;
139
Reid Kleckner9ccce992014-10-28 01:29:26 +0000140 // If we are supposed to add a microsoft-style suffix for stdcall, fastcall,
141 // or vectorcall, add it. These functions have a suffix of @N where N is the
142 // cumulative byte size of all of the parameters to the function in decimal.
143 if (CC == CallingConv::X86_VectorCall)
144 OS << '@'; // vectorcall functions use a double @ suffix.
Rafael Espindola310f5012014-01-29 02:30:38 +0000145 FunctionType *FT = MSFunc->getFunctionType();
Reid Kleckner9ccce992014-10-28 01:29:26 +0000146 if (hasByteCountSuffix(CC) &&
Rafael Espindola310f5012014-01-29 02:30:38 +0000147 // "Pure" variadic functions do not receive @0 suffix.
148 (!FT->isVarArg() || FT->getNumParams() == 0 ||
149 (FT->getNumParams() == 1 && MSFunc->hasStructRetAttr())))
Reid Kleckner9ccce992014-10-28 01:29:26 +0000150 addByteCountSuffix(OS, MSFunc, *DL);
Rafael Espindola310f5012014-01-29 02:30:38 +0000151}
152
153void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000154 const GlobalValue *GV,
155 bool CannotUsePrivateLabel) const {
Rafael Espindola310f5012014-01-29 02:30:38 +0000156 raw_svector_ostream OS(OutName);
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000157 getNameWithPrefix(OS, GV, CannotUsePrivateLabel);
Chris Lattner840c8d72009-09-11 05:40:42 +0000158}