blob: 812ee69100b60042120ebda1487f105702e650c4 [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
Chris Lattnere19f9782009-09-13 18:04:46 +000029/// NameNeedsEscaping - Return true if the identifier \arg Str needs quotes
30/// for this assembler.
Daniel Dunbar2928c832009-11-06 10:58:06 +000031static bool NameNeedsEscaping(StringRef Str, const MCAsmInfo &MAI) {
Chris Lattnere19f9782009-09-13 18:04:46 +000032 assert(!Str.empty() && "Cannot create an empty MCSymbol");
Chris Lattneraadb35f2009-09-03 05:57:47 +000033
Anton Korobeynikovc6f729e2009-09-18 16:57:42 +000034 // If the first character is a number and the target does not allow this, we
35 // need quotes.
36 if (!MAI.doesAllowNameToStartWithDigit() && Str[0] >= '0' && Str[0] <= '9')
Daniel Dunbar1689e0c2009-08-14 03:41:23 +000037 return true;
Chris Lattneracd03ae2010-01-17 19:23:46 +000038
Chris Lattneraadb35f2009-09-03 05:57:47 +000039 // If any of the characters in the string is an unacceptable character, force
40 // quotes.
Chris Lattnere19f9782009-09-13 18:04:46 +000041 for (unsigned i = 0, e = Str.size(); i != e; ++i)
42 if (!isAcceptableChar(Str[i]))
Daniel Dunbar1689e0c2009-08-14 03:41:23 +000043 return true;
Daniel Dunbar1689e0c2009-08-14 03:41:23 +000044 return false;
45}
46
Chris Lattner684c593d2009-09-03 05:46:51 +000047void MCSymbol::print(raw_ostream &OS, const MCAsmInfo *MAI) const {
Chris Lattneracd03ae2010-01-17 19:23:46 +000048 // The name for this MCSymbol is required to be a valid target name. However,
49 // some targets support quoting names with funny characters. If the name
50 // contains a funny character, then print it quoted.
Chris Lattnere19f9782009-09-13 18:04:46 +000051 if (MAI == 0 || !NameNeedsEscaping(getName(), *MAI)) {
Daniel Dunbar1689e0c2009-08-14 03:41:23 +000052 OS << getName();
Chris Lattnere19f9782009-09-13 18:04:46 +000053 return;
54 }
Chris Lattner4afcedf2009-09-13 18:11:09 +000055
56 OS << '"' << getName() << '"';
Daniel Dunbar1689e0c2009-08-14 03:41:23 +000057}
58
59void MCSymbol::dump() const {
David Greenef24dd5c2010-01-05 01:28:10 +000060 print(dbgs(), 0);
Daniel Dunbar1689e0c2009-08-14 03:41:23 +000061}