]> git.proxmox.com Git - rustc.git/blame - src/llvm/lib/IR/Mangler.cpp
Imported Upstream version 1.0.0+dfsg1
[rustc.git] / src / llvm / lib / IR / Mangler.cpp
CommitLineData
1a4d82fc
JJ
1//===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// Unified name mangler for assembly backends.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/IR/Mangler.h"
15#include "llvm/ADT/SmallString.h"
16#include "llvm/ADT/Twine.h"
17#include "llvm/IR/DataLayout.h"
18#include "llvm/IR/DerivedTypes.h"
19#include "llvm/IR/Function.h"
20#include "llvm/Support/raw_ostream.h"
21using namespace llvm;
22
23static void getNameWithPrefixx(raw_ostream &OS, const Twine &GVName,
24 Mangler::ManglerPrefixTy PrefixTy,
85aaf69f 25 const DataLayout &DL, char Prefix) {
1a4d82fc
JJ
26 SmallString<256> TmpData;
27 StringRef Name = GVName.toStringRef(TmpData);
28 assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
29
30 // No need to do anything special if the global has the special "do not
31 // mangle" flag in the name.
32 if (Name[0] == '\1') {
33 OS << Name.substr(1);
34 return;
35 }
36
37 if (PrefixTy == Mangler::Private)
38 OS << DL.getPrivateGlobalPrefix();
39 else if (PrefixTy == Mangler::LinkerPrivate)
40 OS << DL.getLinkerPrivateGlobalPrefix();
41
85aaf69f
SL
42 if (Prefix != '\0')
43 OS << Prefix;
1a4d82fc
JJ
44
45 // If this is a simple string that doesn't need escaping, just append it.
46 OS << Name;
47}
48
49void Mangler::getNameWithPrefix(raw_ostream &OS, const Twine &GVName,
50 ManglerPrefixTy PrefixTy) const {
85aaf69f
SL
51 char Prefix = DL->getGlobalPrefix();
52 return getNameWithPrefixx(OS, GVName, PrefixTy, *DL, Prefix);
1a4d82fc
JJ
53}
54
55void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
56 const Twine &GVName,
57 ManglerPrefixTy PrefixTy) const {
58 raw_svector_ostream OS(OutName);
59 return getNameWithPrefix(OS, GVName, PrefixTy);
60}
61
85aaf69f
SL
62static bool hasByteCountSuffix(CallingConv::ID CC) {
63 switch (CC) {
64 case CallingConv::X86_FastCall:
65 case CallingConv::X86_StdCall:
66 case CallingConv::X86_VectorCall:
67 return true;
68 default:
69 return false;
70 }
71}
72
73/// Microsoft fastcall and stdcall functions require a suffix on their name
74/// indicating the number of words of arguments they take.
75static void addByteCountSuffix(raw_ostream &OS, const Function *F,
76 const DataLayout &TD) {
1a4d82fc
JJ
77 // Calculate arguments size total.
78 unsigned ArgWords = 0;
79 for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
80 AI != AE; ++AI) {
1a4d82fc
JJ
81 Type *Ty = AI->getType();
82 // 'Dereference' type in case of byval or inalloca parameter attribute.
83 if (AI->hasByValOrInAllocaAttr())
84 Ty = cast<PointerType>(Ty)->getElementType();
85aaf69f
SL
85 // Size should be aligned to pointer size.
86 unsigned PtrSize = TD.getPointerSize();
87 ArgWords += RoundUpToAlignment(TD.getTypeAllocSize(Ty), PtrSize);
1a4d82fc
JJ
88 }
89
90 OS << '@' << ArgWords;
91}
92
93void Mangler::getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV,
94 bool CannotUsePrivateLabel) const {
95 ManglerPrefixTy PrefixTy = Mangler::Default;
96 if (GV->hasPrivateLinkage()) {
97 if (CannotUsePrivateLabel)
98 PrefixTy = Mangler::LinkerPrivate;
99 else
100 PrefixTy = Mangler::Private;
101 }
102
103 if (!GV->hasName()) {
104 // Get the ID for the global, assigning a new one if we haven't got one
105 // already.
106 unsigned &ID = AnonGlobalIDs[GV];
107 if (ID == 0)
108 ID = NextAnonGlobalID++;
109
110 // Must mangle the global into a unique ID.
111 getNameWithPrefix(OS, "__unnamed_" + Twine(ID), PrefixTy);
112 return;
113 }
114
115 StringRef Name = GV->getName();
85aaf69f
SL
116 char Prefix = DL->getGlobalPrefix();
117
118 // Mangle functions with Microsoft calling conventions specially. Only do
119 // this mangling for x86_64 vectorcall and 32-bit x86.
120 const Function *MSFunc = dyn_cast<Function>(GV);
121 if (Name.startswith("\01"))
122 MSFunc = nullptr; // Don't mangle when \01 is present.
123 CallingConv::ID CC =
124 MSFunc ? MSFunc->getCallingConv() : (unsigned)CallingConv::C;
125 if (!DL->hasMicrosoftFastStdCallMangling() &&
126 CC != CallingConv::X86_VectorCall)
127 MSFunc = nullptr;
128 if (MSFunc) {
129 if (CC == CallingConv::X86_FastCall)
130 Prefix = '@'; // fastcall functions have an @ prefix instead of _.
131 else if (CC == CallingConv::X86_VectorCall)
132 Prefix = '\0'; // vectorcall functions have no prefix.
1a4d82fc
JJ
133 }
134
85aaf69f 135 getNameWithPrefixx(OS, Name, PrefixTy, *DL, Prefix);
1a4d82fc
JJ
136
137 if (!MSFunc)
138 return;
139
85aaf69f
SL
140 // If we are supposed to add a microsoft-style suffix for stdcall, fastcall,
141 // or vectorcall, add it. These functions have a suffix of @N where N is the
142 // cumulative byte size of all of the parameters to the function in decimal.
143 if (CC == CallingConv::X86_VectorCall)
144 OS << '@'; // vectorcall functions use a double @ suffix.
1a4d82fc 145 FunctionType *FT = MSFunc->getFunctionType();
85aaf69f 146 if (hasByteCountSuffix(CC) &&
1a4d82fc
JJ
147 // "Pure" variadic functions do not receive @0 suffix.
148 (!FT->isVarArg() || FT->getNumParams() == 0 ||
149 (FT->getNumParams() == 1 && MSFunc->hasStructRetAttr())))
85aaf69f 150 addByteCountSuffix(OS, MSFunc, *DL);
1a4d82fc
JJ
151}
152
153void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
154 const GlobalValue *GV,
155 bool CannotUsePrivateLabel) const {
156 raw_svector_ostream OS(OutName);
157 getNameWithPrefix(OS, GV, CannotUsePrivateLabel);
158}