blob: 2ba5998c8052006f0869c6eb51b0310a09362153 [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,
Mehdi Aminia28d91d2015-03-10 02:37:25 +000076 const DataLayout &DL) {
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.
Mehdi Aminia28d91d2015-03-10 02:37:25 +000086 unsigned PtrSize = DL.getPointerSize();
87 ArgWords += RoundUpToAlignment(DL.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,
David Majnemer564404c2015-03-17 20:39:40 +000094 bool CannotUsePrivateLabel,
95 bool ForceNonPrivate) const {
Chris Lattnerc25475e2010-01-17 18:52:16 +000096 ManglerPrefixTy PrefixTy = Mangler::Default;
David Majnemer564404c2015-03-17 20:39:40 +000097 if (GV->hasPrivateLinkage() && !ForceNonPrivate) {
Rafael Espindoladaeafb42014-02-19 17:23:20 +000098 if (CannotUsePrivateLabel)
99 PrefixTy = Mangler::LinkerPrivate;
100 else
101 PrefixTy = Mangler::Private;
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000102 }
Nico Rieckda881a22014-01-14 11:53:26 +0000103
Rafael Espindola310f5012014-01-29 02:30:38 +0000104 if (!GV->hasName()) {
Chris Lattner8d99c762010-03-12 21:03:47 +0000105 // Get the ID for the global, assigning a new one if we haven't got one
106 // already.
107 unsigned &ID = AnonGlobalIDs[GV];
Rafael Espindola310f5012014-01-29 02:30:38 +0000108 if (ID == 0)
109 ID = NextAnonGlobalID++;
110
Chris Lattner8d99c762010-03-12 21:03:47 +0000111 // Must mangle the global into a unique ID.
Rafael Espindola310f5012014-01-29 02:30:38 +0000112 getNameWithPrefix(OS, "__unnamed_" + Twine(ID), PrefixTy);
113 return;
Chris Lattner8d99c762010-03-12 21:03:47 +0000114 }
Rafael Espindola58873562014-01-03 19:21:54 +0000115
Rafael Espindola310f5012014-01-29 02:30:38 +0000116 StringRef Name = GV->getName();
Reid Kleckner9ccce992014-10-28 01:29:26 +0000117 char Prefix = DL->getGlobalPrefix();
Rafael Espindola310f5012014-01-29 02:30:38 +0000118
Reid Kleckner9ccce992014-10-28 01:29:26 +0000119 // Mangle functions with Microsoft calling conventions specially. Only do
120 // this mangling for x86_64 vectorcall and 32-bit x86.
121 const Function *MSFunc = dyn_cast<Function>(GV);
122 if (Name.startswith("\01"))
123 MSFunc = nullptr; // Don't mangle when \01 is present.
Aaron Ballman5af8ba42014-10-28 13:12:13 +0000124 CallingConv::ID CC =
125 MSFunc ? MSFunc->getCallingConv() : (unsigned)CallingConv::C;
Reid Kleckner9ccce992014-10-28 01:29:26 +0000126 if (!DL->hasMicrosoftFastStdCallMangling() &&
127 CC != CallingConv::X86_VectorCall)
128 MSFunc = nullptr;
129 if (MSFunc) {
130 if (CC == CallingConv::X86_FastCall)
131 Prefix = '@'; // fastcall functions have an @ prefix instead of _.
132 else if (CC == CallingConv::X86_VectorCall)
133 Prefix = '\0'; // vectorcall functions have no prefix.
Rafael Espindola310f5012014-01-29 02:30:38 +0000134 }
135
Reid Kleckner9ccce992014-10-28 01:29:26 +0000136 getNameWithPrefixx(OS, Name, PrefixTy, *DL, Prefix);
Rafael Espindola310f5012014-01-29 02:30:38 +0000137
138 if (!MSFunc)
139 return;
140
Reid Kleckner9ccce992014-10-28 01:29:26 +0000141 // If we are supposed to add a microsoft-style suffix for stdcall, fastcall,
142 // or vectorcall, add it. These functions have a suffix of @N where N is the
143 // cumulative byte size of all of the parameters to the function in decimal.
144 if (CC == CallingConv::X86_VectorCall)
145 OS << '@'; // vectorcall functions use a double @ suffix.
Rafael Espindola310f5012014-01-29 02:30:38 +0000146 FunctionType *FT = MSFunc->getFunctionType();
Reid Kleckner9ccce992014-10-28 01:29:26 +0000147 if (hasByteCountSuffix(CC) &&
Rafael Espindola310f5012014-01-29 02:30:38 +0000148 // "Pure" variadic functions do not receive @0 suffix.
149 (!FT->isVarArg() || FT->getNumParams() == 0 ||
150 (FT->getNumParams() == 1 && MSFunc->hasStructRetAttr())))
Reid Kleckner9ccce992014-10-28 01:29:26 +0000151 addByteCountSuffix(OS, MSFunc, *DL);
Rafael Espindola310f5012014-01-29 02:30:38 +0000152}
153
154void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000155 const GlobalValue *GV,
David Majnemer564404c2015-03-17 20:39:40 +0000156 bool CannotUsePrivateLabel,
157 bool ForceNonPrivate) const {
Rafael Espindola310f5012014-01-29 02:30:38 +0000158 raw_svector_ostream OS(OutName);
David Majnemer564404c2015-03-17 20:39:40 +0000159 getNameWithPrefix(OS, GV, CannotUsePrivateLabel, ForceNonPrivate);
Chris Lattner840c8d72009-09-11 05:40:42 +0000160}