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"
22#include "llvm/Target/TargetMachine.h"
23#include "llvm/Support/raw_ostream.h"
24using 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.
29void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
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.
69static void AddFastCallStdCallSuffix(SmallVectorImpl<char> &OutName,
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.
90void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
91                                const GlobalValue *GV, bool isImplicitlyPrivate,
92                                bool UseGlobalPrefix) {
93  ManglerPrefixTy PrefixTy = Mangler::Default;
94  if (GV->hasPrivateLinkage() || isImplicitlyPrivate)
95    PrefixTy = Mangler::Private;
96  else if (GV->hasLinkerPrivateLinkage() || GV->hasLinkerPrivateWeakLinkage())
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.
120  if (TM->getMCAsmInfo()->hasMicrosoftFastStdCallMangling()) {
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();
136      if ((CC == CallingConv::X86_FastCall || CC == CallingConv::X86_StdCall) &&
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}
144