now that mangler is in libtarget, it can use MCAsmInfo instead of clients
[oota-llvm.git] / lib / Target / Mangler.cpp
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/GlobalValue.h"
16 #include "llvm/MC/MCAsmInfo.h"
17 #include "llvm/ADT/SmallString.h"
18 #include "llvm/ADT/Twine.h"
19 #include "llvm/Support/raw_ostream.h"
20 using namespace llvm;
21
22 /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
23 /// and the specified name as the global variable name.  GVName must not be
24 /// empty.
25 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
26                                 const Twine &GVName, ManglerPrefixTy PrefixTy) {
27   SmallString<256> TmpData;
28   StringRef Name = GVName.toStringRef(TmpData);
29   assert(!Name.empty() && "getNameWithPrefix requires non-empty name");
30   
31   // If the global name is not led with \1, add the appropriate prefixes.
32   if (Name[0] != '\1') {
33     if (PrefixTy == Mangler::Private) {
34       const char *Prefix = MAI.getPrivateGlobalPrefix();
35       OutName.append(Prefix, Prefix+strlen(Prefix));
36     } else if (PrefixTy == Mangler::LinkerPrivate) {
37       const char *Prefix = MAI.getLinkerPrivateGlobalPrefix();
38       OutName.append(Prefix, Prefix+strlen(Prefix));
39     }
40
41     const char *Prefix = MAI.getGlobalPrefix();
42     if (Prefix[0] == 0)
43       ; // Common noop, no prefix.
44     else if (Prefix[1] == 0)
45       OutName.push_back(Prefix[0]);  // Common, one character prefix.
46     else
47       OutName.append(Prefix, Prefix+strlen(Prefix)); // Arbitrary length prefix.
48   } else {
49     Name = Name.substr(1);
50   }
51   
52   OutName.append(Name.begin(), Name.end());
53 }
54
55
56 /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
57 /// and the specified global variable's name.  If the global variable doesn't
58 /// have a name, this fills in a unique name for the global.
59 void Mangler::getNameWithPrefix(SmallVectorImpl<char> &OutName,
60                                 const GlobalValue *GV,
61                                 bool isImplicitlyPrivate) {
62   // If this global has a name, handle it simply.
63   if (GV->hasName()) {
64     ManglerPrefixTy PrefixTy = Mangler::Default;
65     if (GV->hasPrivateLinkage() || isImplicitlyPrivate)
66       PrefixTy = Mangler::Private;
67     else if (GV->hasLinkerPrivateLinkage())
68       PrefixTy = Mangler::LinkerPrivate;
69     
70     return getNameWithPrefix(OutName, GV->getName(), PrefixTy);
71   }
72   
73   // If the global variable doesn't have a name, return a unique name for the
74   // global based on a numbering.
75   if (GV->hasPrivateLinkage() || isImplicitlyPrivate) {
76     const char *Prefix = MAI.getPrivateGlobalPrefix();
77     OutName.append(Prefix, Prefix+strlen(Prefix));
78   } else if (GV->hasLinkerPrivateLinkage()) {
79     const char *Prefix = MAI.getLinkerPrivateGlobalPrefix();
80     OutName.append(Prefix, Prefix+strlen(Prefix));
81   }
82   
83   const char *Prefix = MAI.getGlobalPrefix();
84   if (Prefix[0] == 0)
85     ; // Common noop, no prefix.
86   else if (Prefix[1] == 0)
87     OutName.push_back(Prefix[0]);  // Common, one character prefix.
88   else
89     OutName.append(Prefix, Prefix+strlen(Prefix)); // Arbitrary length prefix.
90   
91   // Get the ID for the global, assigning a new one if we haven't got one
92   // already.
93   unsigned &ID = AnonGlobalIDs[GV];
94   if (ID == 0) ID = NextAnonGlobalID++;
95   
96   // Must mangle the global into a unique ID.
97   raw_svector_ostream(OutName) << "__unnamed_" << ID;
98 }
99
100 /// getNameWithPrefix - Fill OutName with the name of the appropriate prefix
101 /// and the specified global variable's name.  If the global variable doesn't
102 /// have a name, this fills in a unique name for the global.
103 std::string Mangler::getNameWithPrefix(const GlobalValue *GV,
104                                        bool isImplicitlyPrivate) {
105   SmallString<64> Buf;
106   getNameWithPrefix(Buf, GV, isImplicitlyPrivate);
107   return std::string(Buf.begin(), Buf.end());
108 }