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 | e66b5a5 | 2009-09-13 18:04:46 +0000 | [diff] [blame^] | 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 != '@') |
Chris Lattner | 648353a | 2009-09-03 05:57:47 +0000 | [diff] [blame] | 24 | return false; |
Chris Lattner | e66b5a5 | 2009-09-13 18:04:46 +0000 | [diff] [blame^] | 25 | return true; |
| 26 | } |
| 27 | |
| 28 | static char HexDigit(int V) { |
| 29 | return V < 10 ? V+'0' : V+'A'-10; |
| 30 | } |
| 31 | |
| 32 | static void MangleLetter(raw_ostream &OS, unsigned char C) { |
| 33 | OS << '_' << HexDigit(C >> 4) << HexDigit(C & 15) << '_'; |
| 34 | } |
| 35 | |
| 36 | /// NameNeedsEscaping - Return true if the identifier \arg Str needs quotes |
| 37 | /// for this assembler. |
| 38 | static bool NameNeedsEscaping(const StringRef &Str, const MCAsmInfo &MAI) { |
| 39 | assert(!Str.empty() && "Cannot create an empty MCSymbol"); |
Chris Lattner | 648353a | 2009-09-03 05:57:47 +0000 | [diff] [blame] | 40 | |
| 41 | // If the first character is a number, we need quotes. |
| 42 | if (Str[0] >= '0' && Str[0] <= '9') |
Daniel Dunbar | a31e630 | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 43 | return true; |
| 44 | |
Chris Lattner | 648353a | 2009-09-03 05:57:47 +0000 | [diff] [blame] | 45 | // If any of the characters in the string is an unacceptable character, force |
| 46 | // quotes. |
Chris Lattner | e66b5a5 | 2009-09-13 18:04:46 +0000 | [diff] [blame^] | 47 | for (unsigned i = 0, e = Str.size(); i != e; ++i) |
| 48 | if (!isAcceptableChar(Str[i])) |
Daniel Dunbar | a31e630 | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 49 | return true; |
Daniel Dunbar | a31e630 | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 50 | return false; |
| 51 | } |
| 52 | |
Chris Lattner | e66b5a5 | 2009-09-13 18:04:46 +0000 | [diff] [blame^] | 53 | static void PrintMangledName(raw_ostream &OS, StringRef Str) { |
| 54 | // The first character is not allowed to be a number. |
| 55 | if (Str[0] >= '0' && Str[0] <= '9') { |
| 56 | MangleLetter(OS, Str[0]); |
| 57 | Str = Str.substr(1); |
| 58 | } |
| 59 | |
| 60 | for (unsigned i = 0, e = Str.size(); i != e; ++i) { |
| 61 | if (!isAcceptableChar(Str[i])) |
| 62 | MangleLetter(OS, Str[i]); |
| 63 | else |
| 64 | OS << Str[i]; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | |
Chris Lattner | 0fe3a1e | 2009-09-03 05:46:51 +0000 | [diff] [blame] | 69 | void MCSymbol::print(raw_ostream &OS, const MCAsmInfo *MAI) const { |
Chris Lattner | e66b5a5 | 2009-09-13 18:04:46 +0000 | [diff] [blame^] | 70 | if (MAI == 0 || !NameNeedsEscaping(getName(), *MAI)) { |
Daniel Dunbar | a31e630 | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 71 | OS << getName(); |
Chris Lattner | e66b5a5 | 2009-09-13 18:04:46 +0000 | [diff] [blame^] | 72 | return; |
| 73 | } |
| 74 | |
| 75 | // On darwin and other systems that allow quoted names, just do that. |
| 76 | if (MAI->doesAllowQuotesInName()) { |
| 77 | OS << '"' << getName() << '"'; |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | // Otherwise, we have to mangle the name. |
| 82 | PrintMangledName(OS, getName()); |
Daniel Dunbar | a31e630 | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void MCSymbol::dump() const { |
Chris Lattner | 0fe3a1e | 2009-09-03 05:46:51 +0000 | [diff] [blame] | 86 | print(errs(), 0); |
Daniel Dunbar | a31e630 | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 87 | } |