blob: faa4c8fe71ee0ee39e253e2a16f168a57fbea38b [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 Lattner2eff5052010-03-12 18:44:54 +000020#include "llvm/MC/MCContext.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
Chris Lattner33535b32010-01-13 07:01:09 +000024/// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
25/// and the specified name as the global variable name. GVName must not be
26/// empty.
27void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
Rafael Espindolad0ed7302013-11-27 02:25:20 +000028 const Twine &GVName, ManglerPrefixTy PrefixTy) {
Chris Lattner33535b32010-01-13 07:01:09 +000029 SmallString<256> TmpData;
Benjamin Kramer2e06b932010-01-13 12:45:23 +000030 StringRef Name = GVName.toStringRef(TmpData);
Chris Lattner33535b32010-01-13 07:01:09 +000031 assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
Rafael Espindola58873562014-01-03 19:21:54 +000032
Chris Lattner33535b32010-01-13 07:01:09 +000033 // If the global name is not led with \1, add the appropriate prefixes.
Chris Lattner83e872e2010-01-17 19:23:46 +000034 if (Name[0] == '\1') {
35 Name = Name.substr(1);
36 } else {
Chris Lattnerb4ffc892010-01-17 18:22:35 +000037 if (PrefixTy == Mangler::Private) {
Rafael Espindola58873562014-01-03 19:21:54 +000038 const char *Prefix = DL->getPrivateGlobalPrefix();
Chris Lattnerb4ffc892010-01-17 18:22:35 +000039 OutName.append(Prefix, Prefix+strlen(Prefix));
40 } else if (PrefixTy == Mangler::LinkerPrivate) {
Rafael Espindola58873562014-01-03 19:21:54 +000041 const char *Prefix = DL->getLinkerPrivateGlobalPrefix();
Chris Lattnerb4ffc892010-01-17 18:22:35 +000042 OutName.append(Prefix, Prefix+strlen(Prefix));
43 }
44
Rafael Espindola58873562014-01-03 19:21:54 +000045 char Prefix = DL->getGlobalPrefix();
Rafael Espindola848493d2013-11-28 17:00:49 +000046 if (Prefix != '\0')
47 OutName.push_back(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 Espindolafe4e0882013-11-14 06:05:49 +000051 OutName.append(Name.begin(), Name.end());
Chris Lattner33535b32010-01-13 07:01:09 +000052}
53
Chris Lattner8d99c762010-03-12 21:03:47 +000054/// AddFastCallStdCallSuffix - Microsoft fastcall and stdcall functions require
55/// a suffix on their name indicating the number of words of arguments they
56/// take.
57static void AddFastCallStdCallSuffix(SmallVectorImpl<char> &OutName,
Micah Villmowcdfe20b2012-10-08 16:38:25 +000058 const Function *F, const DataLayout &TD) {
Chris Lattner8d99c762010-03-12 21:03:47 +000059 // Calculate arguments size total.
60 unsigned ArgWords = 0;
61 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
62 AI != AE; ++AI) {
Chris Lattner229907c2011-07-18 04:54:35 +000063 Type *Ty = AI->getType();
Chris Lattner8d99c762010-03-12 21:03:47 +000064 // 'Dereference' type in case of byval parameter attribute
65 if (AI->hasByValAttr())
66 Ty = cast<PointerType>(Ty)->getElementType();
67 // Size should be aligned to DWORD boundary
68 ArgWords += ((TD.getTypeAllocSize(Ty) + 3)/4)*4;
69 }
70
71 raw_svector_ostream(OutName) << '@' << ArgWords;
72}
73
Chris Lattner840c8d72009-09-11 05:40:42 +000074
75/// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
76/// and the specified global variable's name. If the global variable doesn't
77/// have a name, this fills in a unique name for the global.
78void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
Rafael Espindola117b20c2013-12-05 05:53:12 +000079 const GlobalValue *GV) {
Chris Lattnerc25475e2010-01-17 18:52:16 +000080 ManglerPrefixTy PrefixTy = Mangler::Default;
Rafael Espindola117b20c2013-12-05 05:53:12 +000081 if (GV->hasPrivateLinkage())
Chris Lattnerc25475e2010-01-17 18:52:16 +000082 PrefixTy = Mangler::Private;
Bill Wendling34bc34e2012-08-17 18:33:14 +000083 else if (GV->hasLinkerPrivateLinkage() || GV->hasLinkerPrivateWeakLinkage())
Chris Lattnerc25475e2010-01-17 18:52:16 +000084 PrefixTy = Mangler::LinkerPrivate;
85
Chris Lattner33535b32010-01-13 07:01:09 +000086 // If this global has a name, handle it simply.
Chris Lattner8d99c762010-03-12 21:03:47 +000087 if (GV->hasName()) {
Anton Korobeynikov9c0df162013-04-19 21:20:56 +000088 StringRef Name = GV->getName();
Rafael Espindolad0ed7302013-11-27 02:25:20 +000089 getNameWithPrefix(OutName, Name, PrefixTy);
Anton Korobeynikov9c0df162013-04-19 21:20:56 +000090 // No need to do anything else if the global has the special "do not mangle"
91 // flag in the name.
92 if (Name[0] == 1)
93 return;
Chris Lattner8d99c762010-03-12 21:03:47 +000094 } else {
95 // Get the ID for the global, assigning a new one if we haven't got one
96 // already.
97 unsigned &ID = AnonGlobalIDs[GV];
98 if (ID == 0) ID = NextAnonGlobalID++;
Chris Lattner33535b32010-01-13 07:01:09 +000099
Chris Lattner8d99c762010-03-12 21:03:47 +0000100 // Must mangle the global into a unique ID.
Rafael Espindolad0ed7302013-11-27 02:25:20 +0000101 getNameWithPrefix(OutName, "__unnamed_" + Twine(ID), PrefixTy);
Chris Lattner8d99c762010-03-12 21:03:47 +0000102 }
Rafael Espindola58873562014-01-03 19:21:54 +0000103
Chris Lattner8d99c762010-03-12 21:03:47 +0000104 // If we are supposed to add a microsoft-style suffix for stdcall/fastcall,
105 // add it.
Rafael Espindola58873562014-01-03 19:21:54 +0000106 if (DL->hasMicrosoftFastStdCallMangling()) {
Chris Lattner8d99c762010-03-12 21:03:47 +0000107 if (const Function *F = dyn_cast<Function>(GV)) {
108 CallingConv::ID CC = F->getCallingConv();
109
110 // fastcall functions need to start with @.
111 // FIXME: This logic seems unlikely to be right.
112 if (CC == CallingConv::X86_FastCall) {
113 if (OutName[0] == '_')
114 OutName[0] = '@';
115 else
116 OutName.insert(OutName.begin(), '@');
117 }
118
119 // fastcall and stdcall functions usually need @42 at the end to specify
120 // the argument info.
Chris Lattner229907c2011-07-18 04:54:35 +0000121 FunctionType *FT = F->getFunctionType();
Chris Lattner8d99c762010-03-12 21:03:47 +0000122 if ((CC == CallingConv::X86_FastCall || CC == CallingConv::X86_StdCall) &&
123 // "Pure" variadic functions do not receive @0 suffix.
124 (!FT->isVarArg() || FT->getNumParams() == 0 ||
125 (FT->getNumParams() == 1 && F->hasStructRetAttr())))
Rafael Espindola58873562014-01-03 19:21:54 +0000126 AddFastCallStdCallSuffix(OutName, F, *DL);
Chris Lattner8d99c762010-03-12 21:03:47 +0000127 }
128 }
Chris Lattner840c8d72009-09-11 05:40:42 +0000129}