expose a static function as a static method on the MCSymbol class.
[oota-llvm.git] / lib / MC / MCSymbol.cpp
1 //===- lib/MC/MCSymbol.cpp - MCSymbol implementation ----------------------===//
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 #include "llvm/MC/MCSymbol.h"
11 #include "llvm/MC/MCAsmInfo.h"
12 #include "llvm/Support/Debug.h"
13 #include "llvm/Support/raw_ostream.h"
14 using namespace llvm;
15
16 // Sentinel value for the absolute pseudo section.
17 const MCSection *MCSymbol::AbsolutePseudoSection =
18   reinterpret_cast<const MCSection *>(1);
19
20 static bool isAcceptableChar(char C) {
21   if ((C < 'a' || C > 'z') &&
22       (C < 'A' || C > 'Z') &&
23       (C < '0' || C > '9') &&
24       C != '_' && C != '$' && C != '.' && C != '@')
25     return false;
26   return true;
27 }
28
29 static char HexDigit(int V) {
30   return V < 10 ? V+'0' : V+'A'-10;
31 }
32
33 static void MangleLetter(raw_ostream &OS, unsigned char C) {
34   OS << '_' << HexDigit(C >> 4) << HexDigit(C & 15) << '_';
35 }
36
37 /// NameNeedsEscaping - Return true if the identifier \arg Str needs quotes
38 /// for this assembler.
39 static bool NameNeedsEscaping(StringRef Str, const MCAsmInfo &MAI) {
40   assert(!Str.empty() && "Cannot create an empty MCSymbol");
41   
42   // If the first character is a number and the target does not allow this, we
43   // need quotes.
44   if (!MAI.doesAllowNameToStartWithDigit() && Str[0] >= '0' && Str[0] <= '9')
45     return true;
46
47   // If any of the characters in the string is an unacceptable character, force
48   // quotes.
49   for (unsigned i = 0, e = Str.size(); i != e; ++i)
50     if (!isAcceptableChar(Str[i]))
51       return true;
52   return false;
53 }
54
55 /// printMangledName - Print the specified string in mangled form if it uses
56 /// any unusual characters.
57 void MCSymbol::printMangledName(StringRef Str, raw_ostream &OS,
58                                 const MCAsmInfo *MAI) {
59   // The first character is not allowed to be a number unless the target
60   // explicitly allows it.
61   if ((MAI == 0 || !MAI->doesAllowNameToStartWithDigit()) &&
62       Str[0] >= '0' && Str[0] <= '9') {
63     MangleLetter(OS, Str[0]);
64     Str = Str.substr(1);
65   }
66   
67   for (unsigned i = 0, e = Str.size(); i != e; ++i) {
68     if (!isAcceptableChar(Str[i]))
69       MangleLetter(OS, Str[i]);
70     else
71       OS << Str[i];
72   }
73 }
74
75 /// PrintMangledQuotedName - On systems that support quoted symbols, we still
76 /// have to escape some (obscure) characters like " and \n which would break the
77 /// assembler's lexing.
78 static void PrintMangledQuotedName(raw_ostream &OS, StringRef Str) {
79   OS << '"';
80
81   for (unsigned i = 0, e = Str.size(); i != e; ++i) {
82     if (Str[i] == '"')
83       OS << "_QQ_";
84     else if (Str[i] == '\n')
85       OS << "_NL_";
86     else
87       OS << Str[i];
88   }
89   OS << '"';
90 }
91
92
93 void MCSymbol::print(raw_ostream &OS, const MCAsmInfo *MAI) const {
94   if (MAI == 0 || !NameNeedsEscaping(getName(), *MAI)) {
95     OS << getName();
96     return;
97   }
98
99   // On systems that do not allow quoted names, print with mangling.
100   if (!MAI->doesAllowQuotesInName())
101     return printMangledName(getName(), OS, MAI);
102
103   // If the string contains a double quote or newline, we still have to mangle
104   // it.
105   if (getName().find('"') != std::string::npos ||
106       getName().find('\n') != std::string::npos)
107     return PrintMangledQuotedName(OS, getName());
108     
109   OS << '"' << getName() << '"';
110 }
111
112 void MCSymbol::dump() const {
113   print(dbgs(), 0);
114 }