blob: aac87ce0bb0f4c3097e2f0ed4720fd2012b57c54 [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 Espindolace4c2bc2015-06-23 12:21:54 +000024static void getNameWithPrefixImpl(raw_ostream &OS, const Twine &GVName,
25 Mangler::ManglerPrefixTy PrefixTy,
26 const DataLayout &DL, char Prefix) {
Chris Lattner33535b32010-01-13 07:01:09 +000027 SmallString<256> TmpData;
Benjamin Kramer2e06b932010-01-13 12:45:23 +000028 StringRef Name = GVName.toStringRef(TmpData);
Chris Lattner33535b32010-01-13 07:01:09 +000029 assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
Rafael Espindola58873562014-01-03 19:21:54 +000030
Rafael Espindola5d457de2014-08-01 14:16:40 +000031 // No need to do anything special if the global has the special "do not
32 // mangle" flag in the name.
33 if (Name[0] == '\1') {
34 OS << Name.substr(1);
35 return;
36 }
37
Rafael Espindola310f5012014-01-29 02:30:38 +000038 if (PrefixTy == Mangler::Private)
39 OS << DL.getPrivateGlobalPrefix();
40 else if (PrefixTy == Mangler::LinkerPrivate)
41 OS << DL.getLinkerPrivateGlobalPrefix();
Chris Lattnerb4ffc892010-01-17 18:22:35 +000042
Reid Kleckner9ccce992014-10-28 01:29:26 +000043 if (Prefix != '\0')
44 OS << Prefix;
Rafael Espindolafdc88132013-11-13 14:01:59 +000045
Chris Lattner83e872e2010-01-17 19:23:46 +000046 // If this is a simple string that doesn't need escaping, just append it.
Rafael Espindola310f5012014-01-29 02:30:38 +000047 OS << Name;
48}
49
Rafael Espindolaefedd3a2014-02-10 21:25:13 +000050void Mangler::getNameWithPrefix(raw_ostream &OS, const Twine &GVName,
Rafael Espindolac233f742015-06-23 13:59:29 +000051 const DataLayout &DL,
52 ManglerPrefixTy PrefixTy) {
53 char Prefix = DL.getGlobalPrefix();
54 return getNameWithPrefixImpl(OS, GVName, PrefixTy, DL, Prefix);
Rafael Espindolace4c2bc2015-06-23 12:21:54 +000055}
56
57void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
Rafael Espindolac233f742015-06-23 13:59:29 +000058 const Twine &GVName, const DataLayout &DL,
59 ManglerPrefixTy PrefixTy) {
Rafael Espindolace4c2bc2015-06-23 12:21:54 +000060 raw_svector_ostream OS(OutName);
61 char Prefix = DL.getGlobalPrefix();
Rafael Espindolac233f742015-06-23 13:59:29 +000062 return getNameWithPrefixImpl(OS, GVName, PrefixTy, DL, Prefix);
Chris Lattner33535b32010-01-13 07:01:09 +000063}
64
Reid Kleckner9ccce992014-10-28 01:29:26 +000065static bool hasByteCountSuffix(CallingConv::ID CC) {
66 switch (CC) {
67 case CallingConv::X86_FastCall:
68 case CallingConv::X86_StdCall:
69 case CallingConv::X86_VectorCall:
70 return true;
71 default:
72 return false;
73 }
74}
75
76/// Microsoft fastcall and stdcall functions require a suffix on their name
77/// indicating the number of words of arguments they take.
78static void addByteCountSuffix(raw_ostream &OS, const Function *F,
Mehdi Aminia28d91d2015-03-10 02:37:25 +000079 const DataLayout &DL) {
Chris Lattner8d99c762010-03-12 21:03:47 +000080 // 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) {
Chris Lattner229907c2011-07-18 04:54:35 +000084 Type *Ty = AI->getType();
Reid Klecknerf5b76512014-01-31 23:50:57 +000085 // 'Dereference' type in case of byval or inalloca parameter attribute.
86 if (AI->hasByValOrInAllocaAttr())
Chris Lattner8d99c762010-03-12 21:03:47 +000087 Ty = cast<PointerType>(Ty)->getElementType();
Reid Kleckner9ccce992014-10-28 01:29:26 +000088 // Size should be aligned to pointer size.
Mehdi Aminia28d91d2015-03-10 02:37:25 +000089 unsigned PtrSize = DL.getPointerSize();
90 ArgWords += RoundUpToAlignment(DL.getTypeAllocSize(Ty), PtrSize);
Chris Lattner8d99c762010-03-12 21:03:47 +000091 }
Rafael Espindola310f5012014-01-29 02:30:38 +000092
93 OS << '@' << ArgWords;
Chris Lattner8d99c762010-03-12 21:03:47 +000094}
95
Rafael Espindoladaeafb42014-02-19 17:23:20 +000096void Mangler::getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV,
David Majnemer21fecf92015-03-17 20:40:21 +000097 bool CannotUsePrivateLabel) const {
Chris Lattnerc25475e2010-01-17 18:52:16 +000098 ManglerPrefixTy PrefixTy = Mangler::Default;
David Majnemer21fecf92015-03-17 20:40:21 +000099 if (GV->hasPrivateLinkage()) {
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000100 if (CannotUsePrivateLabel)
101 PrefixTy = Mangler::LinkerPrivate;
102 else
103 PrefixTy = Mangler::Private;
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000104 }
Nico Rieckda881a22014-01-14 11:53:26 +0000105
Rafael Espindolac233f742015-06-23 13:59:29 +0000106 const DataLayout &DL = GV->getParent()->getDataLayout();
Rafael Espindola310f5012014-01-29 02:30:38 +0000107 if (!GV->hasName()) {
Chris Lattner8d99c762010-03-12 21:03:47 +0000108 // Get the ID for the global, assigning a new one if we haven't got one
109 // already.
110 unsigned &ID = AnonGlobalIDs[GV];
Rafael Espindola310f5012014-01-29 02:30:38 +0000111 if (ID == 0)
112 ID = NextAnonGlobalID++;
113
Chris Lattner8d99c762010-03-12 21:03:47 +0000114 // Must mangle the global into a unique ID.
Rafael Espindolac233f742015-06-23 13:59:29 +0000115 getNameWithPrefix(OS, "__unnamed_" + Twine(ID), DL, PrefixTy);
Rafael Espindola310f5012014-01-29 02:30:38 +0000116 return;
Chris Lattner8d99c762010-03-12 21:03:47 +0000117 }
Rafael Espindola58873562014-01-03 19:21:54 +0000118
Rafael Espindola310f5012014-01-29 02:30:38 +0000119 StringRef Name = GV->getName();
Rafael Espindolac233f742015-06-23 13:59:29 +0000120 char Prefix = DL.getGlobalPrefix();
Rafael Espindola310f5012014-01-29 02:30:38 +0000121
Reid Kleckner9ccce992014-10-28 01:29:26 +0000122 // Mangle functions with Microsoft calling conventions specially. Only do
123 // this mangling for x86_64 vectorcall and 32-bit x86.
124 const Function *MSFunc = dyn_cast<Function>(GV);
125 if (Name.startswith("\01"))
126 MSFunc = nullptr; // Don't mangle when \01 is present.
Aaron Ballman5af8ba42014-10-28 13:12:13 +0000127 CallingConv::ID CC =
128 MSFunc ? MSFunc->getCallingConv() : (unsigned)CallingConv::C;
Rafael Espindolac233f742015-06-23 13:59:29 +0000129 if (!DL.hasMicrosoftFastStdCallMangling() &&
Reid Kleckner9ccce992014-10-28 01:29:26 +0000130 CC != CallingConv::X86_VectorCall)
131 MSFunc = nullptr;
132 if (MSFunc) {
133 if (CC == CallingConv::X86_FastCall)
134 Prefix = '@'; // fastcall functions have an @ prefix instead of _.
135 else if (CC == CallingConv::X86_VectorCall)
136 Prefix = '\0'; // vectorcall functions have no prefix.
Rafael Espindola310f5012014-01-29 02:30:38 +0000137 }
138
Rafael Espindolac233f742015-06-23 13:59:29 +0000139 getNameWithPrefixImpl(OS, Name, PrefixTy, DL, Prefix);
Rafael Espindola310f5012014-01-29 02:30:38 +0000140
141 if (!MSFunc)
142 return;
143
Reid Kleckner9ccce992014-10-28 01:29:26 +0000144 // If we are supposed to add a microsoft-style suffix for stdcall, fastcall,
145 // or vectorcall, add it. These functions have a suffix of @N where N is the
146 // cumulative byte size of all of the parameters to the function in decimal.
147 if (CC == CallingConv::X86_VectorCall)
148 OS << '@'; // vectorcall functions use a double @ suffix.
Rafael Espindola310f5012014-01-29 02:30:38 +0000149 FunctionType *FT = MSFunc->getFunctionType();
Reid Kleckner9ccce992014-10-28 01:29:26 +0000150 if (hasByteCountSuffix(CC) &&
Rafael Espindola310f5012014-01-29 02:30:38 +0000151 // "Pure" variadic functions do not receive @0 suffix.
152 (!FT->isVarArg() || FT->getNumParams() == 0 ||
153 (FT->getNumParams() == 1 && MSFunc->hasStructRetAttr())))
Rafael Espindolac233f742015-06-23 13:59:29 +0000154 addByteCountSuffix(OS, MSFunc, DL);
Rafael Espindola310f5012014-01-29 02:30:38 +0000155}
156
157void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000158 const GlobalValue *GV,
David Majnemer21fecf92015-03-17 20:40:21 +0000159 bool CannotUsePrivateLabel) const {
Rafael Espindola310f5012014-01-29 02:30:38 +0000160 raw_svector_ostream OS(OutName);
David Majnemer21fecf92015-03-17 20:40:21 +0000161 getNameWithPrefix(OS, GV, CannotUsePrivateLabel);
Chris Lattner840c8d72009-09-11 05:40:42 +0000162}