1 //===- lib/MC/MCSymbol.cpp - MCSymbol implementation ----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/MC/MCSymbol.h"
11 #include "llvm/MC/MCAsmInfo.h"
12 #include "llvm/Support/raw_ostream.h"
15 // Sentinel value for the absolute pseudo section.
16 const MCSection
*MCSymbol::AbsolutePseudoSection
=
17 reinterpret_cast<const MCSection
*>(1);
19 static bool isAcceptableChar(char C
) {
20 if ((C
< 'a' || C
> 'z') &&
21 (C
< 'A' || C
> 'Z') &&
22 (C
< '0' || C
> '9') &&
23 C
!= '_' && C
!= '$' && C
!= '.' && C
!= '@')
28 static char HexDigit(int V
) {
29 return V
< 10 ? V
+'0' : V
+'A'-10;
32 static void MangleLetter(raw_ostream
&OS
, unsigned char C
) {
33 OS
<< '_' << HexDigit(C
>> 4) << HexDigit(C
& 15) << '_';
36 /// NameNeedsEscaping - Return true if the identifier \arg Str needs quotes
37 /// for this assembler.
38 static bool NameNeedsEscaping(const StringRef
&Str
, const MCAsmInfo
&MAI
) {
39 assert(!Str
.empty() && "Cannot create an empty MCSymbol");
41 // If the first character is a number, we need quotes.
42 if (Str
[0] >= '0' && Str
[0] <= '9')
45 // If any of the characters in the string is an unacceptable character, force
47 for (unsigned i
= 0, e
= Str
.size(); i
!= e
; ++i
)
48 if (!isAcceptableChar(Str
[i
]))
53 static void PrintMangledName(raw_ostream
&OS
, StringRef Str
) {
54 // The first character is not allowed to be a number.
55 if (Str
[0] >= '0' && Str
[0] <= '9') {
56 MangleLetter(OS
, Str
[0]);
60 for (unsigned i
= 0, e
= Str
.size(); i
!= e
; ++i
) {
61 if (!isAcceptableChar(Str
[i
]))
62 MangleLetter(OS
, Str
[i
]);
68 /// PrintMangledQuotedName - On systems that support quoted symbols, we still
69 /// have to escape some (obscure) characters like " and \n which would break the
70 /// assembler's lexing.
71 static void PrintMangledQuotedName(raw_ostream
&OS
, StringRef Str
) {
74 for (unsigned i
= 0, e
= Str
.size(); i
!= e
; ++i
) {
77 else if (Str
[i
] == '\n')
86 void MCSymbol::print(raw_ostream
&OS
, const MCAsmInfo
*MAI
) const {
87 if (MAI
== 0 || !NameNeedsEscaping(getName(), *MAI
)) {
92 // On systems that do not allow quoted names, print with mangling.
93 if (!MAI
->doesAllowQuotesInName())
94 return PrintMangledName(OS
, getName());
96 // If the string contains a double quote or newline, we still have to mangle
98 if (getName().find('"') != std::string::npos
||
99 getName().find('\n') != std::string::npos
)
100 return PrintMangledQuotedName(OS
, getName());
102 OS
<< '"' << getName() << '"';
105 void MCSymbol::dump() const {