blob: f90cd7849a4607c41681c5396ee7391509b3b8d2 [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
Chris Lattnerf62e3ee2010-01-16 21:57:06 +000014#include "llvm/Target/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 Lattnerb4ffc892010-01-17 18:22:35 +000020#include "llvm/MC/MCAsmInfo.h"
Chris Lattner2eff5052010-03-12 18:44:54 +000021#include "llvm/MC/MCContext.h"
Bill Wendling70b14002013-05-29 20:37:19 +000022#include "llvm/Target/TargetMachine.h"
Chris Lattner8d99c762010-03-12 21:03:47 +000023#include "llvm/Support/raw_ostream.h"
Chris Lattnerc9499b62003-12-14 21:35:53 +000024using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000025
Chris Lattner33535b32010-01-13 07:01:09 +000026/// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
27/// and the specified name as the global variable name. GVName must not be
28/// empty.
29void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
Rafael Espindolad0ed7302013-11-27 02:25:20 +000030 const Twine &GVName, ManglerPrefixTy PrefixTy) {
Chris Lattner33535b32010-01-13 07:01:09 +000031 SmallString<256> TmpData;
Benjamin Kramer2e06b932010-01-13 12:45:23 +000032 StringRef Name = GVName.toStringRef(TmpData);
Chris Lattner33535b32010-01-13 07:01:09 +000033 assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
34
Rafael Espindolae133ed82013-10-29 17:28:26 +000035 const MCAsmInfo *MAI = TM->getMCAsmInfo();
Chris Lattner2eff5052010-03-12 18:44:54 +000036
Chris Lattner33535b32010-01-13 07:01:09 +000037 // If the global name is not led with \1, add the appropriate prefixes.
Chris Lattner83e872e2010-01-17 19:23:46 +000038 if (Name[0] == '\1') {
39 Name = Name.substr(1);
40 } else {
Chris Lattnerb4ffc892010-01-17 18:22:35 +000041 if (PrefixTy == Mangler::Private) {
Bill Wendlingbc07a892013-06-18 07:20:20 +000042 const char *Prefix = MAI->getPrivateGlobalPrefix();
Chris Lattnerb4ffc892010-01-17 18:22:35 +000043 OutName.append(Prefix, Prefix+strlen(Prefix));
44 } else if (PrefixTy == Mangler::LinkerPrivate) {
Bill Wendlingbc07a892013-06-18 07:20:20 +000045 const char *Prefix = MAI->getLinkerPrivateGlobalPrefix();
Chris Lattnerb4ffc892010-01-17 18:22:35 +000046 OutName.append(Prefix, Prefix+strlen(Prefix));
47 }
48
Rafael Espindolad0ed7302013-11-27 02:25:20 +000049
Rafael Espindola848493d2013-11-28 17:00:49 +000050 char Prefix = MAI->getGlobalPrefix();
51 if (Prefix != '\0')
52 OutName.push_back(Prefix);
Chris Lattner33535b32010-01-13 07:01:09 +000053 }
Rafael Espindolafdc88132013-11-13 14:01:59 +000054
Chris Lattner83e872e2010-01-17 19:23:46 +000055 // If this is a simple string that doesn't need escaping, just append it.
Rafael Espindolafe4e0882013-11-14 06:05:49 +000056 OutName.append(Name.begin(), Name.end());
Chris Lattner33535b32010-01-13 07:01:09 +000057}
58
Chris Lattner8d99c762010-03-12 21:03:47 +000059/// AddFastCallStdCallSuffix - Microsoft fastcall and stdcall functions require
60/// a suffix on their name indicating the number of words of arguments they
61/// take.
62static void AddFastCallStdCallSuffix(SmallVectorImpl<char> &OutName,
Micah Villmowcdfe20b2012-10-08 16:38:25 +000063 const Function *F, const DataLayout &TD) {
Chris Lattner8d99c762010-03-12 21:03:47 +000064 // Calculate arguments size total.
65 unsigned ArgWords = 0;
66 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
67 AI != AE; ++AI) {
Chris Lattner229907c2011-07-18 04:54:35 +000068 Type *Ty = AI->getType();
Chris Lattner8d99c762010-03-12 21:03:47 +000069 // 'Dereference' type in case of byval parameter attribute
70 if (AI->hasByValAttr())
71 Ty = cast<PointerType>(Ty)->getElementType();
72 // Size should be aligned to DWORD boundary
73 ArgWords += ((TD.getTypeAllocSize(Ty) + 3)/4)*4;
74 }
75
76 raw_svector_ostream(OutName) << '@' << ArgWords;
77}
78
Chris Lattner840c8d72009-09-11 05:40:42 +000079
80/// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
81/// and the specified global variable's name. If the global variable doesn't
82/// have a name, this fills in a unique name for the global.
83void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
Rafael Espindola117b20c2013-12-05 05:53:12 +000084 const GlobalValue *GV) {
Chris Lattnerc25475e2010-01-17 18:52:16 +000085 ManglerPrefixTy PrefixTy = Mangler::Default;
Rafael Espindola117b20c2013-12-05 05:53:12 +000086 if (GV->hasPrivateLinkage())
Chris Lattnerc25475e2010-01-17 18:52:16 +000087 PrefixTy = Mangler::Private;
Bill Wendling34bc34e2012-08-17 18:33:14 +000088 else if (GV->hasLinkerPrivateLinkage() || GV->hasLinkerPrivateWeakLinkage())
Chris Lattnerc25475e2010-01-17 18:52:16 +000089 PrefixTy = Mangler::LinkerPrivate;
90
Chris Lattner33535b32010-01-13 07:01:09 +000091 // If this global has a name, handle it simply.
Chris Lattner8d99c762010-03-12 21:03:47 +000092 if (GV->hasName()) {
Anton Korobeynikov9c0df162013-04-19 21:20:56 +000093 StringRef Name = GV->getName();
Rafael Espindolad0ed7302013-11-27 02:25:20 +000094 getNameWithPrefix(OutName, Name, PrefixTy);
Anton Korobeynikov9c0df162013-04-19 21:20:56 +000095 // No need to do anything else if the global has the special "do not mangle"
96 // flag in the name.
97 if (Name[0] == 1)
98 return;
Chris Lattner8d99c762010-03-12 21:03:47 +000099 } else {
100 // Get the ID for the global, assigning a new one if we haven't got one
101 // already.
102 unsigned &ID = AnonGlobalIDs[GV];
103 if (ID == 0) ID = NextAnonGlobalID++;
Chris Lattner33535b32010-01-13 07:01:09 +0000104
Chris Lattner8d99c762010-03-12 21:03:47 +0000105 // Must mangle the global into a unique ID.
Rafael Espindolad0ed7302013-11-27 02:25:20 +0000106 getNameWithPrefix(OutName, "__unnamed_" + Twine(ID), PrefixTy);
Chris Lattner8d99c762010-03-12 21:03:47 +0000107 }
Chris Lattner840c8d72009-09-11 05:40:42 +0000108
Chris Lattner8d99c762010-03-12 21:03:47 +0000109 // If we are supposed to add a microsoft-style suffix for stdcall/fastcall,
110 // add it.
Rafael Espindolae133ed82013-10-29 17:28:26 +0000111 if (TM->getMCAsmInfo()->hasMicrosoftFastStdCallMangling()) {
Chris Lattner8d99c762010-03-12 21:03:47 +0000112 if (const Function *F = dyn_cast<Function>(GV)) {
113 CallingConv::ID CC = F->getCallingConv();
114
115 // fastcall functions need to start with @.
116 // FIXME: This logic seems unlikely to be right.
117 if (CC == CallingConv::X86_FastCall) {
118 if (OutName[0] == '_')
119 OutName[0] = '@';
120 else
121 OutName.insert(OutName.begin(), '@');
122 }
123
124 // fastcall and stdcall functions usually need @42 at the end to specify
125 // the argument info.
Chris Lattner229907c2011-07-18 04:54:35 +0000126 FunctionType *FT = F->getFunctionType();
Chris Lattner8d99c762010-03-12 21:03:47 +0000127 if ((CC == CallingConv::X86_FastCall || CC == CallingConv::X86_StdCall) &&
128 // "Pure" variadic functions do not receive @0 suffix.
129 (!FT->isVarArg() || FT->getNumParams() == 0 ||
130 (FT->getNumParams() == 1 && F->hasStructRetAttr())))
Bill Wendling70b14002013-05-29 20:37:19 +0000131 AddFastCallStdCallSuffix(OutName, F, *TM->getDataLayout());
Chris Lattner8d99c762010-03-12 21:03:47 +0000132 }
133 }
Chris Lattner840c8d72009-09-11 05:40:42 +0000134}