blob: 832f8fbcbb4391d096be5654c7a2609cf8f5f997 [file] [log] [blame]
Daniel Dunbar1689e0c2009-08-14 03:41:23 +00001//===- 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"
Chris Lattneraadb35f2009-09-03 05:57:47 +000011#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbar1689e0c2009-08-14 03:41:23 +000012#include "llvm/Support/raw_ostream.h"
Daniel Dunbar1689e0c2009-08-14 03:41:23 +000013using namespace llvm;
14
Daniel Dunbar8906ff12009-08-22 07:22:36 +000015// Sentinel value for the absolute pseudo section.
16const MCSection *MCSymbol::AbsolutePseudoSection =
17 reinterpret_cast<const MCSection *>(1);
18
Chris Lattnere19f9782009-09-13 18:04:46 +000019static 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 != '@')
Chris Lattneraadb35f2009-09-03 05:57:47 +000024 return false;
Chris Lattnere19f9782009-09-13 18:04:46 +000025 return true;
26}
27
28static char HexDigit(int V) {
29 return V < 10 ? V+'0' : V+'A'-10;
30}
31
32static void MangleLetter(raw_ostream &OS, unsigned char C) {
33 OS << '_' << HexDigit(C >> 4) << HexDigit(C & 15) << '_';
34}
35
36/// NameNeedsEscaping - Return true if the identifier \arg Str needs quotes
37/// for this assembler.
38static bool NameNeedsEscaping(const StringRef &Str, const MCAsmInfo &MAI) {
39 assert(!Str.empty() && "Cannot create an empty MCSymbol");
Chris Lattneraadb35f2009-09-03 05:57:47 +000040
41 // If the first character is a number, we need quotes.
42 if (Str[0] >= '0' && Str[0] <= '9')
Daniel Dunbar1689e0c2009-08-14 03:41:23 +000043 return true;
44
Chris Lattneraadb35f2009-09-03 05:57:47 +000045 // If any of the characters in the string is an unacceptable character, force
46 // quotes.
Chris Lattnere19f9782009-09-13 18:04:46 +000047 for (unsigned i = 0, e = Str.size(); i != e; ++i)
48 if (!isAcceptableChar(Str[i]))
Daniel Dunbar1689e0c2009-08-14 03:41:23 +000049 return true;
Daniel Dunbar1689e0c2009-08-14 03:41:23 +000050 return false;
51}
52
Chris Lattnere19f9782009-09-13 18:04:46 +000053static 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]);
57 Str = Str.substr(1);
58 }
59
60 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
61 if (!isAcceptableChar(Str[i]))
62 MangleLetter(OS, Str[i]);
63 else
64 OS << Str[i];
65 }
66}
67
Chris Lattner4afcedf2009-09-13 18:11:09 +000068/// 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.
71static void PrintMangledQuotedName(raw_ostream &OS, StringRef Str) {
72 OS << '"';
73
74 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
75 if (Str[i] == '"')
76 OS << "_QQ_";
77 else if (Str[i] == '\n')
78 OS << "_NL_";
79 else
80 OS << Str[i];
81 }
82 OS << '"';
83}
84
Chris Lattnere19f9782009-09-13 18:04:46 +000085
Chris Lattner684c593d2009-09-03 05:46:51 +000086void MCSymbol::print(raw_ostream &OS, const MCAsmInfo *MAI) const {
Chris Lattnere19f9782009-09-13 18:04:46 +000087 if (MAI == 0 || !NameNeedsEscaping(getName(), *MAI)) {
Daniel Dunbar1689e0c2009-08-14 03:41:23 +000088 OS << getName();
Chris Lattnere19f9782009-09-13 18:04:46 +000089 return;
90 }
91
Chris Lattner4afcedf2009-09-13 18:11:09 +000092 // On systems that do not allow quoted names, print with mangling.
93 if (!MAI->doesAllowQuotesInName())
94 return PrintMangledName(OS, getName());
95
96 // If the string contains a double quote or newline, we still have to mangle
97 // it.
98 if (getName().find('"') != std::string::npos ||
99 getName().find('\n') != std::string::npos)
100 return PrintMangledQuotedName(OS, getName());
101
102 OS << '"' << getName() << '"';
Daniel Dunbar1689e0c2009-08-14 03:41:23 +0000103}
104
105void MCSymbol::dump() const {
Chris Lattner684c593d2009-09-03 05:46:51 +0000106 print(errs(), 0);
Daniel Dunbar1689e0c2009-08-14 03:41:23 +0000107}