blob: 3fb12336c4fc8f967fc6ef423962d0238ca49882 [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"
David Greenef24dd5c2010-01-05 01:28:10 +000011#include "llvm/Support/Debug.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
Chris Lattner6edec7b2010-01-17 20:11:03 +000028/// NameNeedsQuoting - Return true if the identifier \arg Str needs quotes to be
29/// syntactically correct.
30static bool NameNeedsQuoting(StringRef Str) {
Chris Lattnere19f9782009-09-13 18:04:46 +000031 assert(!Str.empty() && "Cannot create an empty MCSymbol");
Chris Lattneraadb35f2009-09-03 05:57:47 +000032
Chris Lattneraadb35f2009-09-03 05:57:47 +000033 // If any of the characters in the string is an unacceptable character, force
34 // quotes.
Chris Lattnere19f9782009-09-13 18:04:46 +000035 for (unsigned i = 0, e = Str.size(); i != e; ++i)
36 if (!isAcceptableChar(Str[i]))
Daniel Dunbar1689e0c2009-08-14 03:41:23 +000037 return true;
Daniel Dunbar1689e0c2009-08-14 03:41:23 +000038 return false;
39}
40
Chris Lattner10b318b2010-01-17 21:43:43 +000041void MCSymbol::print(raw_ostream &OS) const {
Chris Lattneracd03ae2010-01-17 19:23:46 +000042 // The name for this MCSymbol is required to be a valid target name. However,
43 // some targets support quoting names with funny characters. If the name
44 // contains a funny character, then print it quoted.
Chris Lattner6edec7b2010-01-17 20:11:03 +000045 if (!NameNeedsQuoting(getName())) {
Daniel Dunbar1689e0c2009-08-14 03:41:23 +000046 OS << getName();
Chris Lattnere19f9782009-09-13 18:04:46 +000047 return;
48 }
Chris Lattner4afcedf2009-09-13 18:11:09 +000049
50 OS << '"' << getName() << '"';
Daniel Dunbar1689e0c2009-08-14 03:41:23 +000051}
52
53void MCSymbol::dump() const {
Chris Lattner10b318b2010-01-17 21:43:43 +000054 print(dbgs());
Daniel Dunbar1689e0c2009-08-14 03:41:23 +000055}