LI can only take signed values, so values > 32767 can only be loaded with ORI
[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 using namespace llvm;
19
20 static char HexDigit(int V) {
21   return V < 10 ? V+'0' : V+'A'-10;
22 }
23
24 static std::string MangleLetter(unsigned char C) {
25   return std::string("_")+HexDigit(C >> 4) + HexDigit(C & 15) + "_";
26 }
27
28 /// makeNameProper - We don't want identifier names non-C-identifier characters
29 /// in them, so mangle them as appropriate.
30 /// 
31 std::string Mangler::makeNameProper(const std::string &X) {
32   std::string Result;
33   
34   // Mangle the first letter specially, don't allow numbers...
35   if ((X[0] < 'a' || X[0] > 'z') && (X[0] < 'A' || X[0] > 'Z') && X[0] != '_')
36     Result += MangleLetter(X[0]);
37   else
38     Result += X[0];
39
40   for (std::string::const_iterator I = X.begin()+1, E = X.end(); I != E; ++I)
41     if ((*I < 'a' || *I > 'z') && (*I < 'A' || *I > 'Z') &&
42         (*I < '0' || *I > '9') && *I != '_')
43       Result += MangleLetter(*I);
44     else
45       Result += *I;
46   return Result;
47 }
48
49 /// getTypeID - Return a unique ID for the specified LLVM type.
50 ///
51 unsigned Mangler::getTypeID(const Type *Ty) {
52   unsigned &E = TypeMap[Ty];
53   if (E == 0) E = ++TypeCounter;
54   return E;
55 }
56
57
58 std::string Mangler::getValueName(const Value *V) {
59   // Check to see whether we've already named V.
60   ValueMap::iterator VI = Memo.find(V);
61   if (VI != Memo.end()) {
62     return VI->second; // Return the old name for V.
63   }
64
65   std::string name;
66   if (V->hasName()) { // Print out the label if it exists...
67     // Name mangling occurs as follows:
68     // - If V is an intrinsic function, do not change name at all
69     // - If V is not a global, mangling always occurs.
70     // - Otherwise, mangling occurs when any of the following are true:
71     //   1) V has internal linkage
72     //   2) V's name would collide if it is not mangled.
73     //
74     const GlobalValue* gv = dyn_cast<GlobalValue>(V);
75     if (gv && isa<Function>(gv) && cast<Function>(gv)->getIntrinsicID()) {
76       name = gv->getName(); // Is an intrinsic function
77     } else if (gv && !gv->hasInternalLinkage() && !MangledGlobals.count(gv)) {
78       name = makeNameProper(gv->getName());
79       if (AddUnderscorePrefix) name = "_" + name;
80     } else {
81       // Non-global, or global with internal linkage / colliding name
82       // -> mangle.
83       unsigned TypeUniqueID = getTypeID(V->getType());
84       name = "l" + utostr(TypeUniqueID) + "_" + makeNameProper(V->getName());
85     }
86   } else {
87     name = "ltmp_" + utostr(Count++) + "_" + utostr(getTypeID(V->getType()));
88   }
89   
90   Memo[V] = name;
91   return name;
92 }
93
94 void Mangler::InsertName(GlobalValue *GV,
95                          std::map<std::string, GlobalValue*> &Names) {
96   if (!GV->hasName()) {   // We must mangle unnamed globals.
97     MangledGlobals.insert(GV);
98     return;
99   }
100
101   // Figure out if this is already used.
102   GlobalValue *&ExistingValue = Names[GV->getName()];
103   if (!ExistingValue) {
104     ExistingValue = GV;
105   } else {
106     // If GV is external but the existing one is static, mangle the existing one
107     if (GV->hasExternalLinkage() && !ExistingValue->hasExternalLinkage()) {
108       MangledGlobals.insert(ExistingValue);
109       ExistingValue = GV;
110     } else {
111       // Otherwise, mangle GV
112       MangledGlobals.insert(GV);
113     }
114   }
115 }
116
117
118 Mangler::Mangler(Module &m, bool addUnderscorePrefix)
119   : M(m), AddUnderscorePrefix(addUnderscorePrefix), TypeCounter(0), Count(0) {
120   // Calculate which global values have names that will collide when we throw
121   // away type information.
122   std::map<std::string, GlobalValue*> Names;
123   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
124     InsertName(I, Names);
125   for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
126     InsertName(I, Names);
127 }