blob: db9b5e59c6abec3ccdf5aacaab9d520aca4ba672 [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 Espindolace4c2bc2015-06-23 12:21:54 +000023static void getNameWithPrefixImpl(raw_ostream &OS, const Twine &GVName,
24 Mangler::ManglerPrefixTy PrefixTy,
25 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();
Rafael Espindolace4c2bc2015-06-23 12:21:54 +000052 return getNameWithPrefixImpl(OS, GVName, PrefixTy, *DL, Prefix);
53}
54
55void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
56 const Twine &GVName, const DataLayout &DL) {
57 raw_svector_ostream OS(OutName);
58 char Prefix = DL.getGlobalPrefix();
59 return getNameWithPrefixImpl(OS, GVName, Mangler::Default, DL, Prefix);
Rafael Espindola310f5012014-01-29 02:30:38 +000060}
61
62void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
Rafael Espindolaefedd3a2014-02-10 21:25:13 +000063 const Twine &GVName,
64 ManglerPrefixTy PrefixTy) const {
Rafael Espindola310f5012014-01-29 02:30:38 +000065 raw_svector_ostream OS(OutName);
Rafael Espindolace4c2bc2015-06-23 12:21:54 +000066 char Prefix = DL->getGlobalPrefix();
67 return getNameWithPrefixImpl(OS, GVName, PrefixTy, *DL, Prefix);
Chris Lattner33535b32010-01-13 07:01:09 +000068}
69
Reid Kleckner9ccce992014-10-28 01:29:26 +000070static bool hasByteCountSuffix(CallingConv::ID CC) {
71 switch (CC) {
72 case CallingConv::X86_FastCall:
73 case CallingConv::X86_StdCall:
74 case CallingConv::X86_VectorCall:
75 return true;
76 default:
77 return false;
78 }
79}
80
81/// Microsoft fastcall and stdcall functions require a suffix on their name
82/// indicating the number of words of arguments they take.
83static void addByteCountSuffix(raw_ostream &OS, const Function *F,
Mehdi Aminia28d91d2015-03-10 02:37:25 +000084 const DataLayout &DL) {
Chris Lattner8d99c762010-03-12 21:03:47 +000085 // Calculate arguments size total.
86 unsigned ArgWords = 0;
87 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
88 AI != AE; ++AI) {
Chris Lattner229907c2011-07-18 04:54:35 +000089 Type *Ty = AI->getType();
Reid Klecknerf5b76512014-01-31 23:50:57 +000090 // 'Dereference' type in case of byval or inalloca parameter attribute.
91 if (AI->hasByValOrInAllocaAttr())
Chris Lattner8d99c762010-03-12 21:03:47 +000092 Ty = cast<PointerType>(Ty)->getElementType();
Reid Kleckner9ccce992014-10-28 01:29:26 +000093 // Size should be aligned to pointer size.
Mehdi Aminia28d91d2015-03-10 02:37:25 +000094 unsigned PtrSize = DL.getPointerSize();
95 ArgWords += RoundUpToAlignment(DL.getTypeAllocSize(Ty), PtrSize);
Chris Lattner8d99c762010-03-12 21:03:47 +000096 }
Rafael Espindola310f5012014-01-29 02:30:38 +000097
98 OS << '@' << ArgWords;
Chris Lattner8d99c762010-03-12 21:03:47 +000099}
100
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000101void Mangler::getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV,
David Majnemer21fecf92015-03-17 20:40:21 +0000102 bool CannotUsePrivateLabel) const {
Chris Lattnerc25475e2010-01-17 18:52:16 +0000103 ManglerPrefixTy PrefixTy = Mangler::Default;
David Majnemer21fecf92015-03-17 20:40:21 +0000104 if (GV->hasPrivateLinkage()) {
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000105 if (CannotUsePrivateLabel)
106 PrefixTy = Mangler::LinkerPrivate;
107 else
108 PrefixTy = Mangler::Private;
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000109 }
Nico Rieckda881a22014-01-14 11:53:26 +0000110
Rafael Espindola310f5012014-01-29 02:30:38 +0000111 if (!GV->hasName()) {
Chris Lattner8d99c762010-03-12 21:03:47 +0000112 // Get the ID for the global, assigning a new one if we haven't got one
113 // already.
114 unsigned &ID = AnonGlobalIDs[GV];
Rafael Espindola310f5012014-01-29 02:30:38 +0000115 if (ID == 0)
116 ID = NextAnonGlobalID++;
117
Chris Lattner8d99c762010-03-12 21:03:47 +0000118 // Must mangle the global into a unique ID.
Rafael Espindola310f5012014-01-29 02:30:38 +0000119 getNameWithPrefix(OS, "__unnamed_" + Twine(ID), PrefixTy);
120 return;
Chris Lattner8d99c762010-03-12 21:03:47 +0000121 }
Rafael Espindola58873562014-01-03 19:21:54 +0000122
Rafael Espindola310f5012014-01-29 02:30:38 +0000123 StringRef Name = GV->getName();
Reid Kleckner9ccce992014-10-28 01:29:26 +0000124 char Prefix = DL->getGlobalPrefix();
Rafael Espindola310f5012014-01-29 02:30:38 +0000125
Reid Kleckner9ccce992014-10-28 01:29:26 +0000126 // Mangle functions with Microsoft calling conventions specially. Only do
127 // this mangling for x86_64 vectorcall and 32-bit x86.
128 const Function *MSFunc = dyn_cast<Function>(GV);
129 if (Name.startswith("\01"))
130 MSFunc = nullptr; // Don't mangle when \01 is present.
Aaron Ballman5af8ba42014-10-28 13:12:13 +0000131 CallingConv::ID CC =
132 MSFunc ? MSFunc->getCallingConv() : (unsigned)CallingConv::C;
Reid Kleckner9ccce992014-10-28 01:29:26 +0000133 if (!DL->hasMicrosoftFastStdCallMangling() &&
134 CC != CallingConv::X86_VectorCall)
135 MSFunc = nullptr;
136 if (MSFunc) {
137 if (CC == CallingConv::X86_FastCall)
138 Prefix = '@'; // fastcall functions have an @ prefix instead of _.
139 else if (CC == CallingConv::X86_VectorCall)
140 Prefix = '\0'; // vectorcall functions have no prefix.
Rafael Espindola310f5012014-01-29 02:30:38 +0000141 }
142
Rafael Espindolace4c2bc2015-06-23 12:21:54 +0000143 getNameWithPrefixImpl(OS, Name, PrefixTy, *DL, Prefix);
Rafael Espindola310f5012014-01-29 02:30:38 +0000144
145 if (!MSFunc)
146 return;
147
Reid Kleckner9ccce992014-10-28 01:29:26 +0000148 // If we are supposed to add a microsoft-style suffix for stdcall, fastcall,
149 // or vectorcall, add it. These functions have a suffix of @N where N is the
150 // cumulative byte size of all of the parameters to the function in decimal.
151 if (CC == CallingConv::X86_VectorCall)
152 OS << '@'; // vectorcall functions use a double @ suffix.
Rafael Espindola310f5012014-01-29 02:30:38 +0000153 FunctionType *FT = MSFunc->getFunctionType();
Reid Kleckner9ccce992014-10-28 01:29:26 +0000154 if (hasByteCountSuffix(CC) &&
Rafael Espindola310f5012014-01-29 02:30:38 +0000155 // "Pure" variadic functions do not receive @0 suffix.
156 (!FT->isVarArg() || FT->getNumParams() == 0 ||
157 (FT->getNumParams() == 1 && MSFunc->hasStructRetAttr())))
Reid Kleckner9ccce992014-10-28 01:29:26 +0000158 addByteCountSuffix(OS, MSFunc, *DL);
Rafael Espindola310f5012014-01-29 02:30:38 +0000159}
160
161void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000162 const GlobalValue *GV,
David Majnemer21fecf92015-03-17 20:40:21 +0000163 bool CannotUsePrivateLabel) const {
Rafael Espindola310f5012014-01-29 02:30:38 +0000164 raw_svector_ostream OS(OutName);
David Majnemer21fecf92015-03-17 20:40:21 +0000165 getNameWithPrefix(OS, GV, CannotUsePrivateLabel);
Chris Lattner840c8d72009-09-11 05:40:42 +0000166}