LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Mangler.cpp
Go to the documentation of this file.
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/Target/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/MC/MCAsmInfo.h"
21 #include "llvm/MC/MCContext.h"
24 using namespace llvm;
25 
26 /// 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.
30  const Twine &GVName, ManglerPrefixTy PrefixTy,
31  bool UseGlobalPrefix) {
32  SmallString<256> TmpData;
33  StringRef Name = GVName.toStringRef(TmpData);
34  assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
35 
36  const MCAsmInfo *MAI = TM->getMCAsmInfo();
37 
38  // If the global name is not led with \1, add the appropriate prefixes.
39  if (Name[0] == '\1') {
40  Name = Name.substr(1);
41  } else {
42  if (PrefixTy == Mangler::Private) {
43  const char *Prefix = MAI->getPrivateGlobalPrefix();
44  OutName.append(Prefix, Prefix+strlen(Prefix));
45  } else if (PrefixTy == Mangler::LinkerPrivate) {
46  const char *Prefix = MAI->getLinkerPrivateGlobalPrefix();
47  OutName.append(Prefix, Prefix+strlen(Prefix));
48  }
49 
50  if (UseGlobalPrefix) {
51  const char *Prefix = MAI->getGlobalPrefix();
52  if (Prefix[0] == 0)
53  ; // Common noop, no prefix.
54  else if (Prefix[1] == 0)
55  OutName.push_back(Prefix[0]); // Common, one character prefix.
56  else
57  // Arbitrary length prefix.
58  OutName.append(Prefix, Prefix+strlen(Prefix));
59  }
60  }
61 
62  // If this is a simple string that doesn't need escaping, just append it.
63  OutName.append(Name.begin(), Name.end());
64 }
65 
66 /// AddFastCallStdCallSuffix - Microsoft fastcall and stdcall functions require
67 /// a suffix on their name indicating the number of words of arguments they
68 /// take.
70  const Function *F, const DataLayout &TD) {
71  // Calculate arguments size total.
72  unsigned ArgWords = 0;
73  for (Function::const_arg_iterator AI = F->arg_begin(), AE = F->arg_end();
74  AI != AE; ++AI) {
75  Type *Ty = AI->getType();
76  // 'Dereference' type in case of byval parameter attribute
77  if (AI->hasByValAttr())
78  Ty = cast<PointerType>(Ty)->getElementType();
79  // Size should be aligned to DWORD boundary
80  ArgWords += ((TD.getTypeAllocSize(Ty) + 3)/4)*4;
81  }
82 
83  raw_svector_ostream(OutName) << '@' << ArgWords;
84 }
85 
86 
87 /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
88 /// and the specified global variable's name. If the global variable doesn't
89 /// have a name, this fills in a unique name for the global.
91  const GlobalValue *GV, bool isImplicitlyPrivate,
92  bool UseGlobalPrefix) {
94  if (GV->hasPrivateLinkage() || isImplicitlyPrivate)
95  PrefixTy = Mangler::Private;
97  PrefixTy = Mangler::LinkerPrivate;
98 
99  // If this global has a name, handle it simply.
100  if (GV->hasName()) {
101  StringRef Name = GV->getName();
102  getNameWithPrefix(OutName, Name, PrefixTy, UseGlobalPrefix);
103  // No need to do anything else if the global has the special "do not mangle"
104  // flag in the name.
105  if (Name[0] == 1)
106  return;
107  } else {
108  // Get the ID for the global, assigning a new one if we haven't got one
109  // already.
110  unsigned &ID = AnonGlobalIDs[GV];
111  if (ID == 0) ID = NextAnonGlobalID++;
112 
113  // Must mangle the global into a unique ID.
114  getNameWithPrefix(OutName, "__unnamed_" + Twine(ID), PrefixTy,
115  UseGlobalPrefix);
116  }
117 
118  // If we are supposed to add a microsoft-style suffix for stdcall/fastcall,
119  // add it.
121  if (const Function *F = dyn_cast<Function>(GV)) {
122  CallingConv::ID CC = F->getCallingConv();
123 
124  // fastcall functions need to start with @.
125  // FIXME: This logic seems unlikely to be right.
126  if (CC == CallingConv::X86_FastCall) {
127  if (OutName[0] == '_')
128  OutName[0] = '@';
129  else
130  OutName.insert(OutName.begin(), '@');
131  }
132 
133  // fastcall and stdcall functions usually need @42 at the end to specify
134  // the argument info.
135  FunctionType *FT = F->getFunctionType();
137  // "Pure" variadic functions do not receive @0 suffix.
138  (!FT->isVarArg() || FT->getNumParams() == 0 ||
139  (FT->getNumParams() == 1 && F->hasStructRetAttr())))
140  AddFastCallStdCallSuffix(OutName, F, *TM->getDataLayout());
141  }
142  }
143 }
void push_back(const T &Elt)
Definition: SmallVector.h:236
bool hasName() const
Definition: Value.h:117
unsigned getNumParams() const
Definition: DerivedTypes.h:133
void getNameWithPrefix(SmallVectorImpl< char > &OutName, const GlobalValue *GV, bool isImplicitlyPrivate, bool UseGlobalPrefix=true)
Definition: Mangler.cpp:90
StringRef substr(size_t Start, size_t N=npos) const
Definition: StringRef.h:392
iterator insert(iterator I, const T &Elt)
Definition: SmallVector.h:537
arg_iterator arg_end()
Definition: Function.h:418
F(f)
StringRef getName() const
Definition: Value.cpp:167
const MCAsmInfo * getMCAsmInfo() const
bool hasLinkerPrivateWeakLinkage() const
Definition: GlobalValue.h:208
ID
LLVM Calling Convention Representation.
Definition: CallingConv.h:26
bool hasPrivateLinkage() const
Definition: GlobalValue.h:206
Emit "linker private" prefix before each symbol.
Definition: Mangler.h:32
iterator begin() const
Definition: StringRef.h:97
bool hasLinkerPrivateLinkage() const
Definition: GlobalValue.h:207
arg_iterator arg_begin()
Definition: Function.h:410
static void AddFastCallStdCallSuffix(SmallVectorImpl< char > &OutName, const Function *F, const DataLayout &TD)
Definition: Mangler.cpp:69
void append(in_iter in_start, in_iter in_end)
Definition: SmallVector.h:445
bool hasMicrosoftFastStdCallMangling() const
Definition: MCAsmInfo.h:387
uint64_t getTypeAllocSize(Type *Ty) const
Definition: DataLayout.h:326
StringRef toStringRef(SmallVectorImpl< char > &Out) const
Definition: Twine.cpp:31
size_t strlen(const char *s);
virtual const DataLayout * getDataLayout() const
bool isVarArg() const
Definition: DerivedTypes.h:120
Emit "private" prefix before each symbol.
Definition: Mangler.h:31
iterator end() const
Definition: StringRef.h:99
Emit default string before each symbol.
Definition: Mangler.h:30
INITIALIZE_PASS(GlobalMerge,"global-merge","Global Merge", false, false) bool GlobalMerge const DataLayout * TD
bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:110