Emit constants to one contiguous block, but this time, respect alignment constraints.
[oota-llvm.git] / lib / VMCore / Mangler.cpp
1 //===-- Mangler.cpp - Self-contained c/asm llvm name mangler --------------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // Unified name mangler for CWriter and assembly backends.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Support/Mangler.h"
15 #include "llvm/Module.h"
16 #include "llvm/Type.h"
17 #include "Support/StringExtras.h"
18
19 namespace llvm {
20
21 static char HexDigit(int V) {
22   return V < 10 ? V+'0' : V+'A'-10;
23 }
24
25 static std::string MangleLetter(unsigned char C) {
26   return std::string("_")+HexDigit(C >> 4) + HexDigit(C & 15) + "_";
27 }
28
29 /// makeNameProper - We don't want identifier names non-C-identifier characters
30 /// in them, so mangle them as appropriate.
31 /// 
32 std::string Mangler::makeNameProper(const std::string &X) {
33   std::string Result;
34   
35   // Mangle the first letter specially, don't allow numbers...
36   if ((X[0] < 'a' || X[0] > 'z') && (X[0] < 'A' || X[0] > 'Z') && X[0] != '_')
37     Result += MangleLetter(X[0]);
38   else
39     Result += X[0];
40
41   for (std::string::const_iterator I = X.begin()+1, E = X.end(); I != E; ++I)
42     if ((*I < 'a' || *I > 'z') && (*I < 'A' || *I > 'Z') &&
43         (*I < '0' || *I > '9') && *I != '_')
44       Result += MangleLetter(*I);
45     else
46       Result += *I;
47   return Result;
48 }
49
50 std::string Mangler::getValueName(const Value *V) {
51   // Check to see whether we've already named V.
52   ValueMap::iterator VI = Memo.find(V);
53   if (VI != Memo.end()) {
54     return VI->second; // Return the old name for V.
55   }
56
57   std::string name;
58   if (V->hasName()) { // Print out the label if it exists...
59     // Name mangling occurs as follows:
60     // - If V is not a global, mangling always occurs.
61     // - Otherwise, mangling occurs when any of the following are true:
62     //   1) V has internal linkage
63     //   2) V's name would collide if it is not mangled.
64     //
65     const GlobalValue* gv = dyn_cast<GlobalValue>(V);
66     if (gv && !gv->hasInternalLinkage() && !MangledGlobals.count(gv)) {
67       name = makeNameProper(gv->getName());
68       if (AddUnderscorePrefix) name = "_" + name;
69     } else {
70       // Non-global, or global with internal linkage / colliding name
71       // -> mangle.
72       name = "l" + utostr(V->getType()->getUniqueID()) + "_" +
73         makeNameProper(V->getName());      
74     }
75   } else {
76     name = "ltmp_" + utostr(Count++) + "_"
77       + utostr(V->getType()->getUniqueID());
78   }
79   
80   Memo[V] = name;
81   return name;
82 }
83
84 Mangler::Mangler(Module &m, bool addUnderscorePrefix)
85   : M(m), AddUnderscorePrefix(addUnderscorePrefix) {
86   // Calculate which global values have names that will collide when we throw
87   // away type information.
88   std::set<std::string> FoundNames;
89   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
90     if (I->hasName())                      // If the global has a name...
91       if (FoundNames.count(I->getName()))  // And the name is already used
92         MangledGlobals.insert(I);          // Mangle the name
93       else
94         FoundNames.insert(I->getName());   // Otherwise, keep track of name
95
96   for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
97     if (I->hasName())                      // If the global has a name...
98       if (FoundNames.count(I->getName()))  // And the name is already used
99         MangledGlobals.insert(I);          // Mangle the name
100       else
101         FoundNames.insert(I->getName());   // Otherwise, keep track of name
102 }
103
104 } // End llvm namespace