blob: 5a099738d55a12f7ab104dc4ce830688b78aebc6 [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 Espindola310f5012014-01-29 02:30:38 +000030 if (PrefixTy == Mangler::Private)
31 OS << DL.getPrivateGlobalPrefix();
32 else if (PrefixTy == Mangler::LinkerPrivate)
33 OS << DL.getLinkerPrivateGlobalPrefix();
Chris Lattnerb4ffc892010-01-17 18:22:35 +000034
Rafael Espindola310f5012014-01-29 02:30:38 +000035 if (UseAt) {
36 OS << '@';
37 } else {
38 char Prefix = DL.getGlobalPrefix();
Rafael Espindola848493d2013-11-28 17:00:49 +000039 if (Prefix != '\0')
Rafael Espindola310f5012014-01-29 02:30:38 +000040 OS << Prefix;
Chris Lattner33535b32010-01-13 07:01:09 +000041 }
Rafael Espindolafdc88132013-11-13 14:01:59 +000042
Chris Lattner83e872e2010-01-17 19:23:46 +000043 // If this is a simple string that doesn't need escaping, just append it.
Rafael Espindola310f5012014-01-29 02:30:38 +000044 OS << Name;
45}
46
47void Mangler::getNameWithPrefix(raw_ostream &OS,
48 const Twine &GVName, ManglerPrefixTy PrefixTy) {
49 return getNameWithPrefixx(OS, GVName, PrefixTy, *DL, false);
50}
51
52void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
53 const Twine &GVName, ManglerPrefixTy PrefixTy) {
54 raw_svector_ostream OS(OutName);
55 return getNameWithPrefix(OS, GVName, PrefixTy);
Chris Lattner33535b32010-01-13 07:01:09 +000056}
57
Chris Lattner8d99c762010-03-12 21:03:47 +000058/// AddFastCallStdCallSuffix - Microsoft fastcall and stdcall functions require
59/// a suffix on their name indicating the number of words of arguments they
60/// take.
Rafael Espindola310f5012014-01-29 02:30:38 +000061static void AddFastCallStdCallSuffix(raw_ostream &OS, const Function *F,
62 const DataLayout &TD) {
Chris Lattner8d99c762010-03-12 21:03:47 +000063 // Calculate arguments size total.
64 unsigned ArgWords = 0;
65 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
66 AI != AE; ++AI) {
Chris Lattner229907c2011-07-18 04:54:35 +000067 Type *Ty = AI->getType();
Chris Lattner8d99c762010-03-12 21:03:47 +000068 // 'Dereference' type in case of byval parameter attribute
69 if (AI->hasByValAttr())
70 Ty = cast<PointerType>(Ty)->getElementType();
71 // Size should be aligned to DWORD boundary
72 ArgWords += ((TD.getTypeAllocSize(Ty) + 3)/4)*4;
73 }
Rafael Espindola310f5012014-01-29 02:30:38 +000074
75 OS << '@' << ArgWords;
Chris Lattner8d99c762010-03-12 21:03:47 +000076}
77
Rafael Espindola310f5012014-01-29 02:30:38 +000078void Mangler::getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV) {
Chris Lattnerc25475e2010-01-17 18:52:16 +000079 ManglerPrefixTy PrefixTy = Mangler::Default;
Rafael Espindola117b20c2013-12-05 05:53:12 +000080 if (GV->hasPrivateLinkage())
Chris Lattnerc25475e2010-01-17 18:52:16 +000081 PrefixTy = Mangler::Private;
Bill Wendling34bc34e2012-08-17 18:33:14 +000082 else if (GV->hasLinkerPrivateLinkage() || GV->hasLinkerPrivateWeakLinkage())
Chris Lattnerc25475e2010-01-17 18:52:16 +000083 PrefixTy = Mangler::LinkerPrivate;
Nico Rieckda881a22014-01-14 11:53:26 +000084
Rafael Espindola310f5012014-01-29 02:30:38 +000085 if (!GV->hasName()) {
Chris Lattner8d99c762010-03-12 21:03:47 +000086 // Get the ID for the global, assigning a new one if we haven't got one
87 // already.
88 unsigned &ID = AnonGlobalIDs[GV];
Rafael Espindola310f5012014-01-29 02:30:38 +000089 if (ID == 0)
90 ID = NextAnonGlobalID++;
91
Chris Lattner8d99c762010-03-12 21:03:47 +000092 // Must mangle the global into a unique ID.
Rafael Espindola310f5012014-01-29 02:30:38 +000093 getNameWithPrefix(OS, "__unnamed_" + Twine(ID), PrefixTy);
94 return;
Chris Lattner8d99c762010-03-12 21:03:47 +000095 }
Rafael Espindola58873562014-01-03 19:21:54 +000096
Rafael Espindola310f5012014-01-29 02:30:38 +000097 StringRef Name = GV->getName();
98
99 // No need to do anything special if the global has the special "do not
100 // mangle" flag in the name.
101 if (Name[0] == '\1') {
102 OS << Name.substr(1);
103 return;
104 }
105
106 bool UseAt = false;
107 const Function *MSFunc = NULL;
108 CallingConv::ID CC;
109 if (DL->hasMicrosoftFastStdCallMangling()) {
110 if ((MSFunc = dyn_cast<Function>(GV))) {
111 CC = MSFunc->getCallingConv();
112 // fastcall functions need to start with @ instead of _.
113 if (CC == CallingConv::X86_FastCall)
114 UseAt = true;
115 }
116 }
117
118 getNameWithPrefixx(OS, Name, PrefixTy, *DL, UseAt);
119
120 if (!MSFunc)
121 return;
122
Chris Lattner8d99c762010-03-12 21:03:47 +0000123 // If we are supposed to add a microsoft-style suffix for stdcall/fastcall,
124 // add it.
Rafael Espindola310f5012014-01-29 02:30:38 +0000125 // fastcall and stdcall functions usually need @42 at the end to specify
126 // the argument info.
127 FunctionType *FT = MSFunc->getFunctionType();
128 if ((CC == CallingConv::X86_FastCall || CC == CallingConv::X86_StdCall) &&
129 // "Pure" variadic functions do not receive @0 suffix.
130 (!FT->isVarArg() || FT->getNumParams() == 0 ||
131 (FT->getNumParams() == 1 && MSFunc->hasStructRetAttr())))
132 AddFastCallStdCallSuffix(OS, MSFunc, *DL);
133}
134
135void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
136 const GlobalValue *GV) {
137 raw_svector_ostream OS(OutName);
138 getNameWithPrefix(OS, GV);
Chris Lattner840c8d72009-09-11 05:40:42 +0000139}