blob: 265d06cceba129f77e9b6f1f4f124459e0127010 [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
Chris Lattner4564ec92010-01-13 21:09:59 +000055/// printMangledName - Print the specified string in mangled form if it uses
56/// any unusual characters.
57void MCSymbol::printMangledName(StringRef Str, raw_ostream &OS,
58 const MCAsmInfo *MAI) {
Anton Korobeynikovc6f729e2009-09-18 16:57:42 +000059 // The first character is not allowed to be a number unless the target
60 // explicitly allows it.
Chris Lattner4564ec92010-01-13 21:09:59 +000061 if ((MAI == 0 || !MAI->doesAllowNameToStartWithDigit()) &&
62 Str[0] >= '0' && Str[0] <= '9') {
Chris Lattnere19f9782009-09-13 18:04:46 +000063 MangleLetter(OS, Str[0]);
64 Str = Str.substr(1);
65 }
66
67 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
68 if (!isAcceptableChar(Str[i]))
69 MangleLetter(OS, Str[i]);
70 else
71 OS << Str[i];
72 }
73}
74
Chris Lattner4afcedf2009-09-13 18:11:09 +000075/// 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.
78static void PrintMangledQuotedName(raw_ostream &OS, StringRef Str) {
79 OS << '"';
80
81 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
82 if (Str[i] == '"')
83 OS << "_QQ_";
84 else if (Str[i] == '\n')
85 OS << "_NL_";
86 else
87 OS << Str[i];
88 }
89 OS << '"';
90}
91
Chris Lattnere19f9782009-09-13 18:04:46 +000092
Chris Lattner684c593d2009-09-03 05:46:51 +000093void MCSymbol::print(raw_ostream &OS, const MCAsmInfo *MAI) const {
Chris Lattnere19f9782009-09-13 18:04:46 +000094 if (MAI == 0 || !NameNeedsEscaping(getName(), *MAI)) {
Daniel Dunbar1689e0c2009-08-14 03:41:23 +000095 OS << getName();
Chris Lattnere19f9782009-09-13 18:04:46 +000096 return;
97 }
98
Chris Lattner4afcedf2009-09-13 18:11:09 +000099 // On systems that do not allow quoted names, print with mangling.
100 if (!MAI->doesAllowQuotesInName())
Chris Lattner4564ec92010-01-13 21:09:59 +0000101 return printMangledName(getName(), OS, MAI);
Chris Lattner4afcedf2009-09-13 18:11:09 +0000102
103 // If the string contains a double quote or newline, we still have to mangle
104 // it.
105 if (getName().find('"') != std::string::npos ||
106 getName().find('\n') != std::string::npos)
107 return PrintMangledQuotedName(OS, getName());
108
109 OS << '"' << getName() << '"';
Daniel Dunbar1689e0c2009-08-14 03:41:23 +0000110}
111
112void MCSymbol::dump() const {
David Greenef24dd5c2010-01-05 01:28:10 +0000113 print(dbgs(), 0);
Daniel Dunbar1689e0c2009-08-14 03:41:23 +0000114}