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" |
Daniel Dunbar | 1689e0c | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 12 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | 1689e0c | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 13 | using namespace llvm; |
| 14 | |
Daniel Dunbar | 8906ff1 | 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 | e19f978 | 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 | aadb35f | 2009-09-03 05:57:47 +0000 | [diff] [blame] | 24 | return false; |
Chris Lattner | e19f978 | 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. |
Daniel Dunbar | 2928c83 | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 38 | static bool NameNeedsEscaping(StringRef Str, const MCAsmInfo &MAI) { |
Chris Lattner | e19f978 | 2009-09-13 18:04:46 +0000 | [diff] [blame] | 39 | assert(!Str.empty() && "Cannot create an empty MCSymbol"); |
Chris Lattner | aadb35f | 2009-09-03 05:57:47 +0000 | [diff] [blame] | 40 | |
Anton Korobeynikov | c6f729e | 2009-09-18 16:57:42 +0000 | [diff] [blame] | 41 | // If the first character is a number and the target does not allow this, we |
| 42 | // need quotes. |
| 43 | if (!MAI.doesAllowNameToStartWithDigit() && Str[0] >= '0' && Str[0] <= '9') |
Daniel Dunbar | 1689e0c | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 44 | return true; |
| 45 | |
Chris Lattner | aadb35f | 2009-09-03 05:57:47 +0000 | [diff] [blame] | 46 | // If any of the characters in the string is an unacceptable character, force |
| 47 | // quotes. |
Chris Lattner | e19f978 | 2009-09-13 18:04:46 +0000 | [diff] [blame] | 48 | for (unsigned i = 0, e = Str.size(); i != e; ++i) |
| 49 | if (!isAcceptableChar(Str[i])) |
Daniel Dunbar | 1689e0c | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 50 | return true; |
Daniel Dunbar | 1689e0c | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 51 | return false; |
| 52 | } |
| 53 | |
Anton Korobeynikov | c6f729e | 2009-09-18 16:57:42 +0000 | [diff] [blame] | 54 | static void PrintMangledName(raw_ostream &OS, StringRef Str, |
| 55 | const MCAsmInfo &MAI) { |
| 56 | // The first character is not allowed to be a number unless the target |
| 57 | // explicitly allows it. |
| 58 | if (!MAI.doesAllowNameToStartWithDigit() && Str[0] >= '0' && Str[0] <= '9') { |
Chris Lattner | e19f978 | 2009-09-13 18:04:46 +0000 | [diff] [blame] | 59 | MangleLetter(OS, Str[0]); |
| 60 | Str = Str.substr(1); |
| 61 | } |
| 62 | |
| 63 | for (unsigned i = 0, e = Str.size(); i != e; ++i) { |
| 64 | if (!isAcceptableChar(Str[i])) |
| 65 | MangleLetter(OS, Str[i]); |
| 66 | else |
| 67 | OS << Str[i]; |
| 68 | } |
| 69 | } |
| 70 | |
Chris Lattner | 4afcedf | 2009-09-13 18:11:09 +0000 | [diff] [blame] | 71 | /// PrintMangledQuotedName - On systems that support quoted symbols, we still |
| 72 | /// have to escape some (obscure) characters like " and \n which would break the |
| 73 | /// assembler's lexing. |
| 74 | static void PrintMangledQuotedName(raw_ostream &OS, StringRef Str) { |
| 75 | OS << '"'; |
| 76 | |
| 77 | for (unsigned i = 0, e = Str.size(); i != e; ++i) { |
| 78 | if (Str[i] == '"') |
| 79 | OS << "_QQ_"; |
| 80 | else if (Str[i] == '\n') |
| 81 | OS << "_NL_"; |
| 82 | else |
| 83 | OS << Str[i]; |
| 84 | } |
| 85 | OS << '"'; |
| 86 | } |
| 87 | |
Chris Lattner | e19f978 | 2009-09-13 18:04:46 +0000 | [diff] [blame] | 88 | |
Chris Lattner | 684c593d | 2009-09-03 05:46:51 +0000 | [diff] [blame] | 89 | void MCSymbol::print(raw_ostream &OS, const MCAsmInfo *MAI) const { |
Chris Lattner | e19f978 | 2009-09-13 18:04:46 +0000 | [diff] [blame] | 90 | if (MAI == 0 || !NameNeedsEscaping(getName(), *MAI)) { |
Daniel Dunbar | 1689e0c | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 91 | OS << getName(); |
Chris Lattner | e19f978 | 2009-09-13 18:04:46 +0000 | [diff] [blame] | 92 | return; |
| 93 | } |
| 94 | |
Chris Lattner | 4afcedf | 2009-09-13 18:11:09 +0000 | [diff] [blame] | 95 | // On systems that do not allow quoted names, print with mangling. |
| 96 | if (!MAI->doesAllowQuotesInName()) |
Anton Korobeynikov | c6f729e | 2009-09-18 16:57:42 +0000 | [diff] [blame] | 97 | return PrintMangledName(OS, getName(), *MAI); |
Chris Lattner | 4afcedf | 2009-09-13 18:11:09 +0000 | [diff] [blame] | 98 | |
| 99 | // If the string contains a double quote or newline, we still have to mangle |
| 100 | // it. |
| 101 | if (getName().find('"') != std::string::npos || |
| 102 | getName().find('\n') != std::string::npos) |
| 103 | return PrintMangledQuotedName(OS, getName()); |
| 104 | |
| 105 | OS << '"' << getName() << '"'; |
Daniel Dunbar | 1689e0c | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | void MCSymbol::dump() const { |
Chris Lattner | 684c593d | 2009-09-03 05:46:51 +0000 | [diff] [blame] | 109 | print(errs(), 0); |
Daniel Dunbar | 1689e0c | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 110 | } |