blob: bfed3e39f4edb8448001a3d538c79554ae75778b [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,
25 const DataLayout &DL, bool UseAt) {
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
Rafael Espindola310f5012014-01-29 02:30:38 +000042 if (UseAt) {
43 OS << '@';
44 } else {
45 char Prefix = DL.getGlobalPrefix();
Rafael Espindola848493d2013-11-28 17:00:49 +000046 if (Prefix != '\0')
Rafael Espindola310f5012014-01-29 02:30:38 +000047 OS << Prefix;
Chris Lattner33535b32010-01-13 07:01:09 +000048 }
Rafael Espindolafdc88132013-11-13 14:01:59 +000049
Chris Lattner83e872e2010-01-17 19:23:46 +000050 // If this is a simple string that doesn't need escaping, just append it.
Rafael Espindola310f5012014-01-29 02:30:38 +000051 OS << Name;
52}
53
Rafael Espindolaefedd3a2014-02-10 21:25:13 +000054void Mangler::getNameWithPrefix(raw_ostream &OS, const Twine &GVName,
55 ManglerPrefixTy PrefixTy) const {
Rafael Espindola310f5012014-01-29 02:30:38 +000056 return getNameWithPrefixx(OS, GVName, PrefixTy, *DL, false);
57}
58
59void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
Rafael Espindolaefedd3a2014-02-10 21:25:13 +000060 const Twine &GVName,
61 ManglerPrefixTy PrefixTy) const {
Rafael Espindola310f5012014-01-29 02:30:38 +000062 raw_svector_ostream OS(OutName);
63 return getNameWithPrefix(OS, GVName, PrefixTy);
Chris Lattner33535b32010-01-13 07:01:09 +000064}
65
Chris Lattner8d99c762010-03-12 21:03:47 +000066/// AddFastCallStdCallSuffix - Microsoft fastcall and stdcall functions require
67/// a suffix on their name indicating the number of words of arguments they
68/// take.
Rafael Espindola310f5012014-01-29 02:30:38 +000069static void AddFastCallStdCallSuffix(raw_ostream &OS, const Function *F,
70 const DataLayout &TD) {
Chris Lattner8d99c762010-03-12 21:03:47 +000071 // Calculate arguments size total.
72 unsigned ArgWords = 0;
73 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
74 AI != AE; ++AI) {
Chris Lattner229907c2011-07-18 04:54:35 +000075 Type *Ty = AI->getType();
Reid Klecknerf5b76512014-01-31 23:50:57 +000076 // 'Dereference' type in case of byval or inalloca parameter attribute.
77 if (AI->hasByValOrInAllocaAttr())
Chris Lattner8d99c762010-03-12 21:03:47 +000078 Ty = cast<PointerType>(Ty)->getElementType();
79 // Size should be aligned to DWORD boundary
80 ArgWords += ((TD.getTypeAllocSize(Ty) + 3)/4)*4;
81 }
Rafael Espindola310f5012014-01-29 02:30:38 +000082
83 OS << '@' << ArgWords;
Chris Lattner8d99c762010-03-12 21:03:47 +000084}
85
Rafael Espindoladaeafb42014-02-19 17:23:20 +000086void Mangler::getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV,
87 bool CannotUsePrivateLabel) const {
Chris Lattnerc25475e2010-01-17 18:52:16 +000088 ManglerPrefixTy PrefixTy = Mangler::Default;
Rafael Espindoladaeafb42014-02-19 17:23:20 +000089 if (GV->hasPrivateLinkage()) {
90 if (CannotUsePrivateLabel)
91 PrefixTy = Mangler::LinkerPrivate;
92 else
93 PrefixTy = Mangler::Private;
Rafael Espindoladaeafb42014-02-19 17:23:20 +000094 }
Nico Rieckda881a22014-01-14 11:53:26 +000095
Rafael Espindola310f5012014-01-29 02:30:38 +000096 if (!GV->hasName()) {
Chris Lattner8d99c762010-03-12 21:03:47 +000097 // Get the ID for the global, assigning a new one if we haven't got one
98 // already.
99 unsigned &ID = AnonGlobalIDs[GV];
Rafael Espindola310f5012014-01-29 02:30:38 +0000100 if (ID == 0)
101 ID = NextAnonGlobalID++;
102
Chris Lattner8d99c762010-03-12 21:03:47 +0000103 // Must mangle the global into a unique ID.
Rafael Espindola310f5012014-01-29 02:30:38 +0000104 getNameWithPrefix(OS, "__unnamed_" + Twine(ID), PrefixTy);
105 return;
Chris Lattner8d99c762010-03-12 21:03:47 +0000106 }
Rafael Espindola58873562014-01-03 19:21:54 +0000107
Rafael Espindola310f5012014-01-29 02:30:38 +0000108 StringRef Name = GV->getName();
109
Rafael Espindola310f5012014-01-29 02:30:38 +0000110 bool UseAt = false;
Craig Topperc6207612014-04-09 06:08:46 +0000111 const Function *MSFunc = nullptr;
Rafael Espindola310f5012014-01-29 02:30:38 +0000112 CallingConv::ID CC;
Rafael Espindola5d457de2014-08-01 14:16:40 +0000113 if (Name[0] != '\1' && DL->hasMicrosoftFastStdCallMangling()) {
Rafael Espindola310f5012014-01-29 02:30:38 +0000114 if ((MSFunc = dyn_cast<Function>(GV))) {
115 CC = MSFunc->getCallingConv();
116 // fastcall functions need to start with @ instead of _.
117 if (CC == CallingConv::X86_FastCall)
118 UseAt = true;
119 }
120 }
121
122 getNameWithPrefixx(OS, Name, PrefixTy, *DL, UseAt);
123
124 if (!MSFunc)
125 return;
126
Chris Lattner8d99c762010-03-12 21:03:47 +0000127 // If we are supposed to add a microsoft-style suffix for stdcall/fastcall,
128 // add it.
Rafael Espindola310f5012014-01-29 02:30:38 +0000129 // fastcall and stdcall functions usually need @42 at the end to specify
130 // the argument info.
131 FunctionType *FT = MSFunc->getFunctionType();
132 if ((CC == CallingConv::X86_FastCall || CC == CallingConv::X86_StdCall) &&
133 // "Pure" variadic functions do not receive @0 suffix.
134 (!FT->isVarArg() || FT->getNumParams() == 0 ||
135 (FT->getNumParams() == 1 && MSFunc->hasStructRetAttr())))
136 AddFastCallStdCallSuffix(OS, MSFunc, *DL);
137}
138
139void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000140 const GlobalValue *GV,
141 bool CannotUsePrivateLabel) const {
Rafael Espindola310f5012014-01-29 02:30:38 +0000142 raw_svector_ostream OS(OutName);
Rafael Espindoladaeafb42014-02-19 17:23:20 +0000143 getNameWithPrefix(OS, GV, CannotUsePrivateLabel);
Chris Lattner840c8d72009-09-11 05:40:42 +0000144}