Shih-wei Liao | e264f62 | 2010-02-10 11:10:31 -0800 | [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/Debug.h" |
| 12 | #include "llvm/Support/raw_ostream.h" |
| 13 | using namespace llvm; |
| 14 | |
| 15 | // Sentinel value for the absolute pseudo section. |
| 16 | const MCSection *MCSymbol::AbsolutePseudoSection = |
| 17 | reinterpret_cast<const MCSection *>(1); |
| 18 | |
| 19 | static 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 != '@') |
| 24 | return false; |
| 25 | return true; |
| 26 | } |
| 27 | |
| 28 | /// NameNeedsQuoting - Return true if the identifier \arg Str needs quotes to be |
| 29 | /// syntactically correct. |
| 30 | static bool NameNeedsQuoting(StringRef Str) { |
| 31 | assert(!Str.empty() && "Cannot create an empty MCSymbol"); |
| 32 | |
| 33 | // If any of the characters in the string is an unacceptable character, force |
| 34 | // quotes. |
| 35 | for (unsigned i = 0, e = Str.size(); i != e; ++i) |
| 36 | if (!isAcceptableChar(Str[i])) |
| 37 | return true; |
| 38 | return false; |
| 39 | } |
| 40 | |
| 41 | void MCSymbol::print(raw_ostream &OS) const { |
| 42 | // 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. |
| 45 | if (!NameNeedsQuoting(getName())) { |
| 46 | OS << getName(); |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | OS << '"' << getName() << '"'; |
| 51 | } |
| 52 | |
| 53 | void MCSymbol::dump() const { |
| 54 | print(dbgs()); |
| 55 | } |