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" |
| 11 | #include "llvm/Support/raw_ostream.h" |
| 12 | |
| 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 | |
Daniel Dunbar | a31e630 | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 19 | /// NeedsQuoting - Return true if the string \arg Str needs quoting, i.e., it |
| 20 | /// does not match [a-zA-Z_.][a-zA-Z0-9_.]*. |
| 21 | // |
| 22 | // FIXME: This could be more permissive, do we care? |
| 23 | static inline bool NeedsQuoting(const StringRef &Str) { |
| 24 | if (Str.empty()) |
| 25 | return true; |
| 26 | |
| 27 | // Check that first character is in [a-zA-Z_.]. |
| 28 | if (!((Str[0] >= 'a' && Str[0] <= 'z') || |
| 29 | (Str[0] >= 'A' && Str[0] <= 'Z') || |
| 30 | (Str[0] == '_' || Str[0] == '.'))) |
| 31 | return true; |
| 32 | |
| 33 | // Check subsequent characters are in [a-zA-Z0-9_.]. |
| 34 | for (unsigned i = 1, e = Str.size(); i != e; ++i) |
| 35 | if (!((Str[i] >= 'a' && Str[i] <= 'z') || |
| 36 | (Str[i] >= 'A' && Str[i] <= 'Z') || |
| 37 | (Str[i] >= '0' && Str[i] <= '9') || |
| 38 | (Str[i] == '_' || Str[i] == '.'))) |
| 39 | return true; |
| 40 | |
| 41 | return false; |
| 42 | } |
| 43 | |
Chris Lattner | 0fe3a1e | 2009-09-03 05:46:51 +0000 | [diff] [blame] | 44 | void MCSymbol::print(raw_ostream &OS, const MCAsmInfo *MAI) const { |
Daniel Dunbar | a31e630 | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 45 | if (NeedsQuoting(getName())) |
| 46 | OS << '"' << getName() << '"'; |
| 47 | else |
| 48 | OS << getName(); |
| 49 | } |
| 50 | |
| 51 | void MCSymbol::dump() const { |
Chris Lattner | 0fe3a1e | 2009-09-03 05:46:51 +0000 | [diff] [blame] | 52 | print(errs(), 0); |
Daniel Dunbar | a31e630 | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 53 | } |