blob: 83200a0bb9f2be32ca5e5477354f5aad045231b0 [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"
David Greenef24dd5c2010-01-05 01:28:10 +000012#include "llvm/Support/Debug.h"
Daniel Dunbar1689e0c2009-08-14 03:41:23 +000013#include "llvm/Support/raw_ostream.h"
Daniel Dunbar1689e0c2009-08-14 03:41:23 +000014using namespace llvm;
15
Daniel Dunbar8906ff12009-08-22 07:22:36 +000016// Sentinel value for the absolute pseudo section.
17const MCSection *MCSymbol::AbsolutePseudoSection =
18 reinterpret_cast<const MCSection *>(1);
19
Chris Lattnere19f9782009-09-13 18:04:46 +000020static 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 != '@')
Chris Lattneraadb35f2009-09-03 05:57:47 +000025 return false;
Chris Lattnere19f9782009-09-13 18:04:46 +000026 return true;
27}
28
29static char HexDigit(int V) {
30 return V < 10 ? V+'0' : V+'A'-10;
31}
32
33static 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.
Daniel Dunbar2928c832009-11-06 10:58:06 +000039static bool NameNeedsEscaping(StringRef Str, const MCAsmInfo &MAI) {
Chris Lattnere19f9782009-09-13 18:04:46 +000040 assert(!Str.empty() && "Cannot create an empty MCSymbol");
Chris Lattneraadb35f2009-09-03 05:57:47 +000041
Anton Korobeynikovc6f729e2009-09-18 16:57:42 +000042 // 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')
Daniel Dunbar1689e0c2009-08-14 03:41:23 +000045 return true;
46
Chris Lattneraadb35f2009-09-03 05:57:47 +000047 // If any of the characters in the string is an unacceptable character, force
48 // quotes.
Chris Lattnere19f9782009-09-13 18:04:46 +000049 for (unsigned i = 0, e = Str.size(); i != e; ++i)
50 if (!isAcceptableChar(Str[i]))
Daniel Dunbar1689e0c2009-08-14 03:41:23 +000051 return true;
Daniel Dunbar1689e0c2009-08-14 03:41:23 +000052 return false;
53}
54
Anton Korobeynikovc6f729e2009-09-18 16:57:42 +000055static 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') {
Chris Lattnere19f9782009-09-13 18:04:46 +000060 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
Chris Lattner4afcedf2009-09-13 18:11:09 +000072/// 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.
75static 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
Chris Lattnere19f9782009-09-13 18:04:46 +000089
Chris Lattner684c593d2009-09-03 05:46:51 +000090void MCSymbol::print(raw_ostream &OS, const MCAsmInfo *MAI) const {
Chris Lattnere19f9782009-09-13 18:04:46 +000091 if (MAI == 0 || !NameNeedsEscaping(getName(), *MAI)) {
Daniel Dunbar1689e0c2009-08-14 03:41:23 +000092 OS << getName();
Chris Lattnere19f9782009-09-13 18:04:46 +000093 return;
94 }
95
Chris Lattner4afcedf2009-09-13 18:11:09 +000096 // On systems that do not allow quoted names, print with mangling.
97 if (!MAI->doesAllowQuotesInName())
Anton Korobeynikovc6f729e2009-09-18 16:57:42 +000098 return PrintMangledName(OS, getName(), *MAI);
Chris Lattner4afcedf2009-09-13 18:11:09 +000099
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() << '"';
Daniel Dunbar1689e0c2009-08-14 03:41:23 +0000107}
108
109void MCSymbol::dump() const {
David Greenef24dd5c2010-01-05 01:28:10 +0000110 print(dbgs(), 0);
Daniel Dunbar1689e0c2009-08-14 03:41:23 +0000111}