Daniel Dunbar | 1689e0c | 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 | aadb35f | 2009-09-03 05:57:47 +0000 | [diff] [blame] | 11 | #include "llvm/MC/MCAsmInfo.h" |
David Greene | f24dd5c | 2010-01-05 01:28:10 +0000 | [diff] [blame] | 12 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | 1689e0c | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 13 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | 1689e0c | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 14 | using namespace llvm; |
| 15 | |
Daniel Dunbar | 8906ff1 | 2009-08-22 07:22:36 +0000 | [diff] [blame] | 16 | // Sentinel value for the absolute pseudo section. |
| 17 | const MCSection *MCSymbol::AbsolutePseudoSection = |
| 18 | reinterpret_cast<const MCSection *>(1); |
| 19 | |
Chris Lattner | e19f978 | 2009-09-13 18:04:46 +0000 | [diff] [blame] | 20 | static bool isAcceptableChar(char C) { |
| 21 | if ((C < 'a' || C > 'z') && |
| 22 | (C < 'A' || C > 'Z') && |
| 23 | (C < '0' || C > '9') && |
| 24 | C != '_' && C != '$' && C != '.' && C != '@') |
Chris Lattner | aadb35f | 2009-09-03 05:57:47 +0000 | [diff] [blame] | 25 | return false; |
Chris Lattner | e19f978 | 2009-09-13 18:04:46 +0000 | [diff] [blame] | 26 | return true; |
| 27 | } |
| 28 | |
Chris Lattner | e19f978 | 2009-09-13 18:04:46 +0000 | [diff] [blame] | 29 | /// NameNeedsEscaping - Return true if the identifier \arg Str needs quotes |
| 30 | /// for this assembler. |
Daniel Dunbar | 2928c83 | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 31 | static bool NameNeedsEscaping(StringRef Str, const MCAsmInfo &MAI) { |
Chris Lattner | e19f978 | 2009-09-13 18:04:46 +0000 | [diff] [blame] | 32 | assert(!Str.empty() && "Cannot create an empty MCSymbol"); |
Chris Lattner | aadb35f | 2009-09-03 05:57:47 +0000 | [diff] [blame] | 33 | |
Anton Korobeynikov | c6f729e | 2009-09-18 16:57:42 +0000 | [diff] [blame] | 34 | // If the first character is a number and the target does not allow this, we |
| 35 | // need quotes. |
| 36 | if (!MAI.doesAllowNameToStartWithDigit() && Str[0] >= '0' && Str[0] <= '9') |
Daniel Dunbar | 1689e0c | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 37 | return true; |
Chris Lattner | acd03ae | 2010-01-17 19:23:46 +0000 | [diff] [blame^] | 38 | |
Chris Lattner | aadb35f | 2009-09-03 05:57:47 +0000 | [diff] [blame] | 39 | // If any of the characters in the string is an unacceptable character, force |
| 40 | // quotes. |
Chris Lattner | e19f978 | 2009-09-13 18:04:46 +0000 | [diff] [blame] | 41 | for (unsigned i = 0, e = Str.size(); i != e; ++i) |
| 42 | if (!isAcceptableChar(Str[i])) |
Daniel Dunbar | 1689e0c | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 43 | return true; |
Daniel Dunbar | 1689e0c | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 44 | return false; |
| 45 | } |
| 46 | |
Chris Lattner | 684c593d | 2009-09-03 05:46:51 +0000 | [diff] [blame] | 47 | void MCSymbol::print(raw_ostream &OS, const MCAsmInfo *MAI) const { |
Chris Lattner | acd03ae | 2010-01-17 19:23:46 +0000 | [diff] [blame^] | 48 | // The name for this MCSymbol is required to be a valid target name. However, |
| 49 | // some targets support quoting names with funny characters. If the name |
| 50 | // contains a funny character, then print it quoted. |
Chris Lattner | e19f978 | 2009-09-13 18:04:46 +0000 | [diff] [blame] | 51 | if (MAI == 0 || !NameNeedsEscaping(getName(), *MAI)) { |
Daniel Dunbar | 1689e0c | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 52 | OS << getName(); |
Chris Lattner | e19f978 | 2009-09-13 18:04:46 +0000 | [diff] [blame] | 53 | return; |
| 54 | } |
Chris Lattner | 4afcedf | 2009-09-13 18:11:09 +0000 | [diff] [blame] | 55 | |
| 56 | OS << '"' << getName() << '"'; |
Daniel Dunbar | 1689e0c | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | void MCSymbol::dump() const { |
David Greene | f24dd5c | 2010-01-05 01:28:10 +0000 | [diff] [blame] | 60 | print(dbgs(), 0); |
Daniel Dunbar | 1689e0c | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 61 | } |