blob: 5430d50945256f2a9efb21ca2a931533612592ee [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
50 const char *Prefix = MAI->getGlobalPrefix();
51 if (Prefix[0] == 0)
52 ; // Common noop, no prefix.
53 else if (Prefix[1] == 0)
54 OutName.push_back(Prefix[0]); // Common, one character prefix.
55 else
56 // Arbitrary length prefix.
57 OutName.append(Prefix, Prefix+strlen(Prefix));
Chris Lattner33535b32010-01-13 07:01:09 +000058 }
Rafael Espindolafdc88132013-11-13 14:01:59 +000059
Chris Lattner83e872e2010-01-17 19:23:46 +000060 // If this is a simple string that doesn't need escaping, just append it.
Rafael Espindolafe4e0882013-11-14 06:05:49 +000061 OutName.append(Name.begin(), Name.end());
Chris Lattner33535b32010-01-13 07:01:09 +000062}
63
Chris Lattner8d99c762010-03-12 21:03:47 +000064/// AddFastCallStdCallSuffix - Microsoft fastcall and stdcall functions require
65/// a suffix on their name indicating the number of words of arguments they
66/// take.
67static void AddFastCallStdCallSuffix(SmallVectorImpl<char> &OutName,
Micah Villmowcdfe20b2012-10-08 16:38:25 +000068 const Function *F, const DataLayout &TD) {
Chris Lattner8d99c762010-03-12 21:03:47 +000069 // Calculate arguments size total.
70 unsigned ArgWords = 0;
71 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
72 AI != AE; ++AI) {
Chris Lattner229907c2011-07-18 04:54:35 +000073 Type *Ty = AI->getType();
Chris Lattner8d99c762010-03-12 21:03:47 +000074 // 'Dereference' type in case of byval parameter attribute
75 if (AI->hasByValAttr())
76 Ty = cast<PointerType>(Ty)->getElementType();
77 // Size should be aligned to DWORD boundary
78 ArgWords += ((TD.getTypeAllocSize(Ty) + 3)/4)*4;
79 }
80
81 raw_svector_ostream(OutName) << '@' << ArgWords;
82}
83
Chris Lattner840c8d72009-09-11 05:40:42 +000084
85/// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
86/// and the specified global variable's name. If the global variable doesn't
87/// have a name, this fills in a unique name for the global.
88void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
Rafael Espindolad0ed7302013-11-27 02:25:20 +000089 const GlobalValue *GV,
90 bool isImplicitlyPrivate) {
Chris Lattnerc25475e2010-01-17 18:52:16 +000091 ManglerPrefixTy PrefixTy = Mangler::Default;
92 if (GV->hasPrivateLinkage() || isImplicitlyPrivate)
93 PrefixTy = Mangler::Private;
Bill Wendling34bc34e2012-08-17 18:33:14 +000094 else if (GV->hasLinkerPrivateLinkage() || GV->hasLinkerPrivateWeakLinkage())
Chris Lattnerc25475e2010-01-17 18:52:16 +000095 PrefixTy = Mangler::LinkerPrivate;
96
Chris Lattner33535b32010-01-13 07:01:09 +000097 // If this global has a name, handle it simply.
Chris Lattner8d99c762010-03-12 21:03:47 +000098 if (GV->hasName()) {
Anton Korobeynikov9c0df162013-04-19 21:20:56 +000099 StringRef Name = GV->getName();
Rafael Espindolad0ed7302013-11-27 02:25:20 +0000100 getNameWithPrefix(OutName, Name, PrefixTy);
Anton Korobeynikov9c0df162013-04-19 21:20:56 +0000101 // No need to do anything else if the global has the special "do not mangle"
102 // flag in the name.
103 if (Name[0] == 1)
104 return;
Chris Lattner8d99c762010-03-12 21:03:47 +0000105 } else {
106 // Get the ID for the global, assigning a new one if we haven't got one
107 // already.
108 unsigned &ID = AnonGlobalIDs[GV];
109 if (ID == 0) ID = NextAnonGlobalID++;
Chris Lattner33535b32010-01-13 07:01:09 +0000110
Chris Lattner8d99c762010-03-12 21:03:47 +0000111 // Must mangle the global into a unique ID.
Rafael Espindolad0ed7302013-11-27 02:25:20 +0000112 getNameWithPrefix(OutName, "__unnamed_" + Twine(ID), PrefixTy);
Chris Lattner8d99c762010-03-12 21:03:47 +0000113 }
Chris Lattner840c8d72009-09-11 05:40:42 +0000114
Chris Lattner8d99c762010-03-12 21:03:47 +0000115 // If we are supposed to add a microsoft-style suffix for stdcall/fastcall,
116 // add it.
Rafael Espindolae133ed82013-10-29 17:28:26 +0000117 if (TM->getMCAsmInfo()->hasMicrosoftFastStdCallMangling()) {
Chris Lattner8d99c762010-03-12 21:03:47 +0000118 if (const Function *F = dyn_cast<Function>(GV)) {
119 CallingConv::ID CC = F->getCallingConv();
120
121 // fastcall functions need to start with @.
122 // FIXME: This logic seems unlikely to be right.
123 if (CC == CallingConv::X86_FastCall) {
124 if (OutName[0] == '_')
125 OutName[0] = '@';
126 else
127 OutName.insert(OutName.begin(), '@');
128 }
129
130 // fastcall and stdcall functions usually need @42 at the end to specify
131 // the argument info.
Chris Lattner229907c2011-07-18 04:54:35 +0000132 FunctionType *FT = F->getFunctionType();
Chris Lattner8d99c762010-03-12 21:03:47 +0000133 if ((CC == CallingConv::X86_FastCall || CC == CallingConv::X86_StdCall) &&
134 // "Pure" variadic functions do not receive @0 suffix.
135 (!FT->isVarArg() || FT->getNumParams() == 0 ||
136 (FT->getNumParams() == 1 && F->hasStructRetAttr())))
Bill Wendling70b14002013-05-29 20:37:19 +0000137 AddFastCallStdCallSuffix(OutName, F, *TM->getDataLayout());
Chris Lattner8d99c762010-03-12 21:03:47 +0000138 }
139 }
Chris Lattner840c8d72009-09-11 05:40:42 +0000140}