blob: c067f0f3923d0f440d17555c494a3453b5aff682 [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
Chris Lattner33535b32010-01-13 07:01:09 +000023/// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
24/// and the specified name as the global variable name. GVName must not be
25/// empty.
26void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
Rafael Espindolad0ed7302013-11-27 02:25:20 +000027 const Twine &GVName, ManglerPrefixTy PrefixTy) {
Chris Lattner33535b32010-01-13 07:01:09 +000028 SmallString<256> TmpData;
Benjamin Kramer2e06b932010-01-13 12:45:23 +000029 StringRef Name = GVName.toStringRef(TmpData);
Chris Lattner33535b32010-01-13 07:01:09 +000030 assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
Rafael Espindola58873562014-01-03 19:21:54 +000031
Chris Lattner33535b32010-01-13 07:01:09 +000032 // If the global name is not led with \1, add the appropriate prefixes.
Chris Lattner83e872e2010-01-17 19:23:46 +000033 if (Name[0] == '\1') {
34 Name = Name.substr(1);
35 } else {
Chris Lattnerb4ffc892010-01-17 18:22:35 +000036 if (PrefixTy == Mangler::Private) {
Rafael Espindola58873562014-01-03 19:21:54 +000037 const char *Prefix = DL->getPrivateGlobalPrefix();
Chris Lattnerb4ffc892010-01-17 18:22:35 +000038 OutName.append(Prefix, Prefix+strlen(Prefix));
39 } else if (PrefixTy == Mangler::LinkerPrivate) {
Rafael Espindola58873562014-01-03 19:21:54 +000040 const char *Prefix = DL->getLinkerPrivateGlobalPrefix();
Chris Lattnerb4ffc892010-01-17 18:22:35 +000041 OutName.append(Prefix, Prefix+strlen(Prefix));
42 }
43
Rafael Espindola58873562014-01-03 19:21:54 +000044 char Prefix = DL->getGlobalPrefix();
Rafael Espindola848493d2013-11-28 17:00:49 +000045 if (Prefix != '\0')
46 OutName.push_back(Prefix);
Chris Lattner33535b32010-01-13 07:01:09 +000047 }
Rafael Espindolafdc88132013-11-13 14:01:59 +000048
Chris Lattner83e872e2010-01-17 19:23:46 +000049 // If this is a simple string that doesn't need escaping, just append it.
Rafael Espindolafe4e0882013-11-14 06:05:49 +000050 OutName.append(Name.begin(), Name.end());
Chris Lattner33535b32010-01-13 07:01:09 +000051}
52
Chris Lattner8d99c762010-03-12 21:03:47 +000053/// AddFastCallStdCallSuffix - Microsoft fastcall and stdcall functions require
54/// a suffix on their name indicating the number of words of arguments they
55/// take.
56static void AddFastCallStdCallSuffix(SmallVectorImpl<char> &OutName,
Micah Villmowcdfe20b2012-10-08 16:38:25 +000057 const Function *F, const DataLayout &TD) {
Chris Lattner8d99c762010-03-12 21:03:47 +000058 // Calculate arguments size total.
59 unsigned ArgWords = 0;
60 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
61 AI != AE; ++AI) {
Chris Lattner229907c2011-07-18 04:54:35 +000062 Type *Ty = AI->getType();
Chris Lattner8d99c762010-03-12 21:03:47 +000063 // 'Dereference' type in case of byval parameter attribute
64 if (AI->hasByValAttr())
65 Ty = cast<PointerType>(Ty)->getElementType();
66 // Size should be aligned to DWORD boundary
67 ArgWords += ((TD.getTypeAllocSize(Ty) + 3)/4)*4;
68 }
69
70 raw_svector_ostream(OutName) << '@' << ArgWords;
71}
72
Chris Lattner840c8d72009-09-11 05:40:42 +000073
74/// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
75/// and the specified global variable's name. If the global variable doesn't
76/// have a name, this fills in a unique name for the global.
77void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
Rafael Espindola117b20c2013-12-05 05:53:12 +000078 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
85 size_t NameBegin = OutName.size();
86
Chris Lattner33535b32010-01-13 07:01:09 +000087 // If this global has a name, handle it simply.
Chris Lattner8d99c762010-03-12 21:03:47 +000088 if (GV->hasName()) {
Anton Korobeynikov9c0df162013-04-19 21:20:56 +000089 StringRef Name = GV->getName();
Rafael Espindolad0ed7302013-11-27 02:25:20 +000090 getNameWithPrefix(OutName, Name, PrefixTy);
Anton Korobeynikov9c0df162013-04-19 21:20:56 +000091 // No need to do anything else if the global has the special "do not mangle"
92 // flag in the name.
93 if (Name[0] == 1)
94 return;
Chris Lattner8d99c762010-03-12 21:03:47 +000095 } else {
96 // Get the ID for the global, assigning a new one if we haven't got one
97 // already.
98 unsigned &ID = AnonGlobalIDs[GV];
99 if (ID == 0) ID = NextAnonGlobalID++;
Chris Lattner33535b32010-01-13 07:01:09 +0000100
Chris Lattner8d99c762010-03-12 21:03:47 +0000101 // Must mangle the global into a unique ID.
Rafael Espindolad0ed7302013-11-27 02:25:20 +0000102 getNameWithPrefix(OutName, "__unnamed_" + Twine(ID), PrefixTy);
Chris Lattner8d99c762010-03-12 21:03:47 +0000103 }
Rafael Espindola58873562014-01-03 19:21:54 +0000104
Chris Lattner8d99c762010-03-12 21:03:47 +0000105 // If we are supposed to add a microsoft-style suffix for stdcall/fastcall,
106 // add it.
Rafael Espindola58873562014-01-03 19:21:54 +0000107 if (DL->hasMicrosoftFastStdCallMangling()) {
Chris Lattner8d99c762010-03-12 21:03:47 +0000108 if (const Function *F = dyn_cast<Function>(GV)) {
109 CallingConv::ID CC = F->getCallingConv();
110
Nico Rieckda881a22014-01-14 11:53:26 +0000111 // fastcall functions need to start with @ instead of _.
Chris Lattner8d99c762010-03-12 21:03:47 +0000112 if (CC == CallingConv::X86_FastCall) {
Nico Rieckda881a22014-01-14 11:53:26 +0000113 assert(OutName[NameBegin] == '_' && DL->getGlobalPrefix() == '_');
114 OutName[NameBegin] = '@';
Chris Lattner8d99c762010-03-12 21:03:47 +0000115 }
116
117 // fastcall and stdcall functions usually need @42 at the end to specify
118 // the argument info.
Chris Lattner229907c2011-07-18 04:54:35 +0000119 FunctionType *FT = F->getFunctionType();
Chris Lattner8d99c762010-03-12 21:03:47 +0000120 if ((CC == CallingConv::X86_FastCall || CC == CallingConv::X86_StdCall) &&
121 // "Pure" variadic functions do not receive @0 suffix.
122 (!FT->isVarArg() || FT->getNumParams() == 0 ||
123 (FT->getNumParams() == 1 && F->hasStructRetAttr())))
Rafael Espindola58873562014-01-03 19:21:54 +0000124 AddFastCallStdCallSuffix(OutName, F, *DL);
Chris Lattner8d99c762010-03-12 21:03:47 +0000125 }
126 }
Chris Lattner840c8d72009-09-11 05:40:42 +0000127}