Daniel Dunbar | 1a019d8 | 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" |
Daniel Dunbar | 8557426 | 2010-05-05 19:00:56 +0000 | [diff] [blame] | 11 | #include "llvm/MC/MCExpr.h" |
David Greene | cdf0452 | 2010-01-05 01:28:10 +0000 | [diff] [blame] | 12 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | 1a019d8 | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 13 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | 1a019d8 | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 14 | using namespace llvm; |
| 15 | |
Daniel Dunbar | 6860ac7 | 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 | 3d6c8eb | 2009-09-13 18:04:46 +0000 | [diff] [blame] | 20 | static bool isAcceptableChar(char C) { |
Hans Wennborg | 7ddcdc8 | 2013-10-18 02:14:40 +0000 | [diff] [blame] | 21 | if ((C < 'a' || C > 'z') && |
| 22 | (C < 'A' || C > 'Z') && |
| 23 | (C < '0' || C > '9') && |
| 24 | C != '_' && C != '$' && C != '.' && C != '@') |
| 25 | return false; |
| 26 | return true; |
Chris Lattner | 3d6c8eb | 2009-09-13 18:04:46 +0000 | [diff] [blame] | 27 | } |
| 28 | |
Dmitri Gribenko | 5485acd | 2012-09-14 14:57:36 +0000 | [diff] [blame] | 29 | /// NameNeedsQuoting - Return true if the identifier \p Str needs quotes to be |
Chris Lattner | 043e656 | 2010-01-17 20:11:03 +0000 | [diff] [blame] | 30 | /// syntactically correct. |
| 31 | static bool NameNeedsQuoting(StringRef Str) { |
Chris Lattner | 3d6c8eb | 2009-09-13 18:04:46 +0000 | [diff] [blame] | 32 | assert(!Str.empty() && "Cannot create an empty MCSymbol"); |
Jim Grosbach | dc1e36e | 2012-05-11 01:41:30 +0000 | [diff] [blame] | 33 | |
Chris Lattner | 1945453 | 2009-09-03 05:57:47 +0000 | [diff] [blame] | 34 | // If any of the characters in the string is an unacceptable character, force |
| 35 | // quotes. |
Chris Lattner | 3d6c8eb | 2009-09-13 18:04:46 +0000 | [diff] [blame] | 36 | for (unsigned i = 0, e = Str.size(); i != e; ++i) |
| 37 | if (!isAcceptableChar(Str[i])) |
Daniel Dunbar | 1a019d8 | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 38 | return true; |
Daniel Dunbar | 1a019d8 | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 39 | return false; |
| 40 | } |
| 41 | |
Rafael Espindola | 8c3039b | 2010-11-15 16:33:49 +0000 | [diff] [blame] | 42 | const MCSymbol &MCSymbol::AliasedSymbol() const { |
| 43 | const MCSymbol *S = this; |
| 44 | while (S->isVariable()) { |
| 45 | const MCExpr *Value = S->getVariableValue(); |
| 46 | if (Value->getKind() != MCExpr::SymbolRef) |
| 47 | return *S; |
| 48 | const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr*>(Value); |
| 49 | S = &Ref->getSymbol(); |
| 50 | } |
| 51 | return *S; |
| 52 | } |
| 53 | |
Daniel Dunbar | 8557426 | 2010-05-05 19:00:56 +0000 | [diff] [blame] | 54 | void MCSymbol::setVariableValue(const MCExpr *Value) { |
Rafael Espindola | 46c79ef | 2010-11-15 14:40:36 +0000 | [diff] [blame] | 55 | assert(!IsUsed && "Cannot set a variable that has already been used."); |
Daniel Dunbar | 8557426 | 2010-05-05 19:00:56 +0000 | [diff] [blame] | 56 | assert(Value && "Invalid variable value!"); |
Daniel Dunbar | 8557426 | 2010-05-05 19:00:56 +0000 | [diff] [blame] | 57 | this->Value = Value; |
Daniel Dunbar | f3a53ba | 2010-05-05 19:01:05 +0000 | [diff] [blame] | 58 | |
Daniel Dunbar | bea7b93 | 2011-04-29 18:20:17 +0000 | [diff] [blame] | 59 | // Variables should always be marked as in the same "section" as the value. |
| 60 | const MCSection *Section = Value->FindAssociatedSection(); |
Jim Grosbach | 765a6e0 | 2012-03-20 21:33:17 +0000 | [diff] [blame] | 61 | if (Section) |
Daniel Dunbar | bea7b93 | 2011-04-29 18:20:17 +0000 | [diff] [blame] | 62 | setSection(*Section); |
Jim Grosbach | 765a6e0 | 2012-03-20 21:33:17 +0000 | [diff] [blame] | 63 | else |
Daniel Dunbar | bea7b93 | 2011-04-29 18:20:17 +0000 | [diff] [blame] | 64 | setUndefined(); |
Daniel Dunbar | 8557426 | 2010-05-05 19:00:56 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Chris Lattner | 8b5d55e | 2010-01-17 21:43:43 +0000 | [diff] [blame] | 67 | void MCSymbol::print(raw_ostream &OS) const { |
Chris Lattner | 83e872e | 2010-01-17 19:23:46 +0000 | [diff] [blame] | 68 | // The name for this MCSymbol is required to be a valid target name. However, |
| 69 | // some targets support quoting names with funny characters. If the name |
| 70 | // contains a funny character, then print it quoted. |
Rafael Espindola | fe4e088 | 2013-11-14 06:05:49 +0000 | [diff] [blame] | 71 | StringRef Name = getName(); |
| 72 | if (!NameNeedsQuoting(Name)) { |
| 73 | OS << Name; |
Chris Lattner | 3d6c8eb | 2009-09-13 18:04:46 +0000 | [diff] [blame] | 74 | return; |
| 75 | } |
Jim Grosbach | dc1e36e | 2012-05-11 01:41:30 +0000 | [diff] [blame] | 76 | |
Rafael Espindola | fe4e088 | 2013-11-14 06:05:49 +0000 | [diff] [blame] | 77 | OS << '"'; |
| 78 | for (unsigned I = 0, E = Name.size(); I != E; ++I) { |
| 79 | char C = Name[I]; |
| 80 | if (C == '\n') |
| 81 | OS << "\\n"; |
| 82 | else if (C == '"') |
| 83 | OS << "\\\""; |
| 84 | else |
| 85 | OS << C; |
| 86 | } |
| 87 | OS << '"'; |
Daniel Dunbar | 1a019d8 | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Manman Ren | 49d684e | 2012-09-12 05:06:18 +0000 | [diff] [blame] | 90 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Daniel Dunbar | 1a019d8 | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 91 | void MCSymbol::dump() const { |
Chris Lattner | 8b5d55e | 2010-01-17 21:43:43 +0000 | [diff] [blame] | 92 | print(dbgs()); |
Daniel Dunbar | 1a019d8 | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 93 | } |
Manman Ren | c3366cc | 2012-09-06 19:55:56 +0000 | [diff] [blame] | 94 | #endif |