83200a0bb9f2be32ca5e5477354f5aad045231b0
[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 static void PrintMangledName(raw_ostream &OS, StringRef Str,
56                              const MCAsmInfo &MAI) {
57   // The first character is not allowed to be a number unless the target
58   // explicitly allows it.
59   if (!MAI.doesAllowNameToStartWithDigit() && Str[0] >= '0' && Str[0] <= '9') {
60     MangleLetter(OS, Str[0]);
61     Str = Str.substr(1);
62   }
63   
64   for (unsigned i = 0, e = Str.size(); i != e; ++i) {
65     if (!isAcceptableChar(Str[i]))
66       MangleLetter(OS, Str[i]);
67     else
68       OS << Str[i];
69   }
70 }
71
72 /// PrintMangledQuotedName - On systems that support quoted symbols, we still
73 /// have to escape some (obscure) characters like " and \n which would break the
74 /// assembler's lexing.
75 static void PrintMangledQuotedName(raw_ostream &OS, StringRef Str) {
76   OS << '"';
77
78   for (unsigned i = 0, e = Str.size(); i != e; ++i) {
79     if (Str[i] == '"')
80       OS << "_QQ_";
81     else if (Str[i] == '\n')
82       OS << "_NL_";
83     else
84       OS << Str[i];
85   }
86   OS << '"';
87 }
88
89
90 void MCSymbol::print(raw_ostream &OS, const MCAsmInfo *MAI) const {
91   if (MAI == 0 || !NameNeedsEscaping(getName(), *MAI)) {
92     OS << getName();
93     return;
94   }
95
96   // On systems that do not allow quoted names, print with mangling.
97   if (!MAI->doesAllowQuotesInName())
98     return PrintMangledName(OS, getName(), *MAI);
99
100   // If the string contains a double quote or newline, we still have to mangle
101   // it.
102   if (getName().find('"') != std::string::npos ||
103       getName().find('\n') != std::string::npos)
104     return PrintMangledQuotedName(OS, getName());
105     
106   OS << '"' << getName() << '"';
107 }
108
109 void MCSymbol::dump() const {
110   print(dbgs(), 0);
111 }