blob: 68ab24d9523a37b1cb0a7d959bb4b7923f86ebc0 [file] [log] [blame]
Daniel Dunbara31e6302009-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 Lattner648353a2009-09-03 05:57:47 +000011#include "llvm/MC/MCAsmInfo.h"
Daniel Dunbara31e6302009-08-14 03:41:23 +000012#include "llvm/Support/raw_ostream.h"
Daniel Dunbara31e6302009-08-14 03:41:23 +000013using namespace llvm;
14
Daniel Dunbar1c2ee9f2009-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 Lattner648353a2009-09-03 05:57:47 +000019/// ShouldQuoteIdentifier - Return true if the identifier \arg Str needs quotes
20/// for this assembler.
21static bool ShouldQuoteIdentifier(const StringRef &Str, const MCAsmInfo &MAI) {
22 // If the assembler doesn't support quotes, never use them.
23 if (!MAI.doesAllowQuotesInName())
24 return false;
25
26 // If empty, we need quotes.
Daniel Dunbara31e6302009-08-14 03:41:23 +000027 if (Str.empty())
28 return true;
Chris Lattner648353a2009-09-03 05:57:47 +000029
30 // If the first character is a number, we need quotes.
31 if (Str[0] >= '0' && Str[0] <= '9')
Daniel Dunbara31e6302009-08-14 03:41:23 +000032 return true;
33
Chris Lattner648353a2009-09-03 05:57:47 +000034 // If any of the characters in the string is an unacceptable character, force
35 // quotes.
36 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
37 char C = Str[i];
38
39 if ((C < 'a' || C > 'z') &&
40 (C < 'A' || C > 'Z') &&
41 (C < '0' || C > '9') &&
42 C != '_' && C != '$' && C != '.')
Daniel Dunbara31e6302009-08-14 03:41:23 +000043 return true;
Chris Lattner648353a2009-09-03 05:57:47 +000044 }
Daniel Dunbara31e6302009-08-14 03:41:23 +000045 return false;
46}
47
Chris Lattner0fe3a1e2009-09-03 05:46:51 +000048void MCSymbol::print(raw_ostream &OS, const MCAsmInfo *MAI) const {
Chris Lattner648353a2009-09-03 05:57:47 +000049 if (!MAI || ShouldQuoteIdentifier(getName(), *MAI))
Daniel Dunbara31e6302009-08-14 03:41:23 +000050 OS << '"' << getName() << '"';
51 else
52 OS << getName();
53}
54
55void MCSymbol::dump() const {
Chris Lattner0fe3a1e2009-09-03 05:46:51 +000056 print(errs(), 0);
Daniel Dunbara31e6302009-08-14 03:41:23 +000057}