Daniel Dunbar | a31e630 | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 1 | //===- 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 Lattner | 648353a | 2009-09-03 05:57:47 +0000 | [diff] [blame^] | 11 | #include "llvm/MC/MCAsmInfo.h" |
Daniel Dunbar | a31e630 | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 12 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | a31e630 | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 13 | using namespace llvm; |
| 14 | |
Daniel Dunbar | 1c2ee9f | 2009-08-22 07:22:36 +0000 | [diff] [blame] | 15 | // Sentinel value for the absolute pseudo section. |
| 16 | const MCSection *MCSymbol::AbsolutePseudoSection = |
| 17 | reinterpret_cast<const MCSection *>(1); |
| 18 | |
Chris Lattner | 648353a | 2009-09-03 05:57:47 +0000 | [diff] [blame^] | 19 | /// ShouldQuoteIdentifier - Return true if the identifier \arg Str needs quotes |
| 20 | /// for this assembler. |
| 21 | static 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 Dunbar | a31e630 | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 27 | if (Str.empty()) |
| 28 | return true; |
Chris Lattner | 648353a | 2009-09-03 05:57:47 +0000 | [diff] [blame^] | 29 | |
| 30 | // If the first character is a number, we need quotes. |
| 31 | if (Str[0] >= '0' && Str[0] <= '9') |
Daniel Dunbar | a31e630 | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 32 | return true; |
| 33 | |
Chris Lattner | 648353a | 2009-09-03 05:57:47 +0000 | [diff] [blame^] | 34 | // 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 Dunbar | a31e630 | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 43 | return true; |
Chris Lattner | 648353a | 2009-09-03 05:57:47 +0000 | [diff] [blame^] | 44 | } |
Daniel Dunbar | a31e630 | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 45 | return false; |
| 46 | } |
| 47 | |
Chris Lattner | 0fe3a1e | 2009-09-03 05:46:51 +0000 | [diff] [blame] | 48 | void MCSymbol::print(raw_ostream &OS, const MCAsmInfo *MAI) const { |
Chris Lattner | 648353a | 2009-09-03 05:57:47 +0000 | [diff] [blame^] | 49 | if (!MAI || ShouldQuoteIdentifier(getName(), *MAI)) |
Daniel Dunbar | a31e630 | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 50 | OS << '"' << getName() << '"'; |
| 51 | else |
| 52 | OS << getName(); |
| 53 | } |
| 54 | |
| 55 | void MCSymbol::dump() const { |
Chris Lattner | 0fe3a1e | 2009-09-03 05:46:51 +0000 | [diff] [blame] | 56 | print(errs(), 0); |
Daniel Dunbar | a31e630 | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 57 | } |