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/Debug.h"
13 #include "llvm/Support/raw_ostream.h"
16 // Sentinel value for the absolute pseudo section.
17 const MCSection *MCSymbol::AbsolutePseudoSection =
18 reinterpret_cast<const MCSection *>(1);
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 != '@')
29 static char HexDigit(int V) {
30 return V < 10 ? V+'0' : V+'A'-10;
33 static void MangleLetter(raw_ostream &OS, unsigned char C) {
34 OS << '_' << HexDigit(C >> 4) << HexDigit(C & 15) << '_';
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");
42 // If the first character is a number and the target does not allow this, we
44 if (!MAI.doesAllowNameToStartWithDigit() && Str[0] >= '0' && Str[0] <= '9')
47 // If any of the characters in the string is an unacceptable character, force
49 for (unsigned i = 0, e = Str.size(); i != e; ++i)
50 if (!isAcceptableChar(Str[i]))
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]);
67 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
68 if (!isAcceptableChar(Str[i]))
69 MangleLetter(OS, Str[i]);
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) {
81 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
84 else if (Str[i] == '\n')
93 void MCSymbol::print(raw_ostream &OS, const MCAsmInfo *MAI) const {
94 if (MAI == 0 || !NameNeedsEscaping(getName(), *MAI)) {
99 // On systems that do not allow quoted names, print with mangling.
100 if (!MAI->doesAllowQuotesInName())
101 return printMangledName(getName(), OS, MAI);
103 // If the string contains a double quote or newline, we still have to mangle
105 if (getName().find('"') != std::string::npos ||
106 getName().find('\n') != std::string::npos)
107 return PrintMangledQuotedName(OS, getName());
109 OS << '"' << getName() << '"';
112 void MCSymbol::dump() const {