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" |
Daniel Dunbar | 8d627d3 | 2010-05-05 19:00:56 +0000 | [diff] [blame] | 11 | #include "llvm/MC/MCExpr.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) { |
Hans Wennborg | b74b88e | 2013-10-17 01:13:02 +0000 | [diff] [blame^] | 21 | return (C >= 'a' && C <= 'z') || |
| 22 | (C >= 'A' && C <= 'Z') || |
| 23 | (C >= '0' && C <= '9') || |
| 24 | C == '_' || C == '$' || C == '.'; |
Chris Lattner | e19f978 | 2009-09-13 18:04:46 +0000 | [diff] [blame] | 25 | } |
| 26 | |
Dmitri Gribenko | c5252da | 2012-09-14 14:57:36 +0000 | [diff] [blame] | 27 | /// NameNeedsQuoting - Return true if the identifier \p Str needs quotes to be |
Chris Lattner | 6edec7b | 2010-01-17 20:11:03 +0000 | [diff] [blame] | 28 | /// syntactically correct. |
| 29 | static bool NameNeedsQuoting(StringRef Str) { |
Chris Lattner | e19f978 | 2009-09-13 18:04:46 +0000 | [diff] [blame] | 30 | assert(!Str.empty() && "Cannot create an empty MCSymbol"); |
Jim Grosbach | 2684d9e | 2012-05-11 01:41:30 +0000 | [diff] [blame] | 31 | |
Chris Lattner | aadb35f | 2009-09-03 05:57:47 +0000 | [diff] [blame] | 32 | // If any of the characters in the string is an unacceptable character, force |
| 33 | // quotes. |
Chris Lattner | e19f978 | 2009-09-13 18:04:46 +0000 | [diff] [blame] | 34 | for (unsigned i = 0, e = Str.size(); i != e; ++i) |
| 35 | if (!isAcceptableChar(Str[i])) |
Daniel Dunbar | 1689e0c | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 36 | return true; |
Daniel Dunbar | 1689e0c | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 37 | return false; |
| 38 | } |
| 39 | |
Rafael Espindola | 94ed5fc | 2010-11-15 16:33:49 +0000 | [diff] [blame] | 40 | const MCSymbol &MCSymbol::AliasedSymbol() const { |
| 41 | const MCSymbol *S = this; |
| 42 | while (S->isVariable()) { |
| 43 | const MCExpr *Value = S->getVariableValue(); |
| 44 | if (Value->getKind() != MCExpr::SymbolRef) |
| 45 | return *S; |
| 46 | const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr*>(Value); |
| 47 | S = &Ref->getSymbol(); |
| 48 | } |
| 49 | return *S; |
| 50 | } |
| 51 | |
Daniel Dunbar | 8d627d3 | 2010-05-05 19:00:56 +0000 | [diff] [blame] | 52 | void MCSymbol::setVariableValue(const MCExpr *Value) { |
Rafael Espindola | db9835d | 2010-11-15 14:40:36 +0000 | [diff] [blame] | 53 | assert(!IsUsed && "Cannot set a variable that has already been used."); |
Daniel Dunbar | 8d627d3 | 2010-05-05 19:00:56 +0000 | [diff] [blame] | 54 | assert(Value && "Invalid variable value!"); |
Daniel Dunbar | 8d627d3 | 2010-05-05 19:00:56 +0000 | [diff] [blame] | 55 | this->Value = Value; |
Daniel Dunbar | 2d7fd61 | 2010-05-05 19:01:05 +0000 | [diff] [blame] | 56 | |
Daniel Dunbar | 90604ab | 2011-04-29 18:20:17 +0000 | [diff] [blame] | 57 | // Variables should always be marked as in the same "section" as the value. |
| 58 | const MCSection *Section = Value->FindAssociatedSection(); |
Jim Grosbach | 93a1a0d | 2012-03-20 21:33:17 +0000 | [diff] [blame] | 59 | if (Section) |
Daniel Dunbar | 90604ab | 2011-04-29 18:20:17 +0000 | [diff] [blame] | 60 | setSection(*Section); |
Jim Grosbach | 93a1a0d | 2012-03-20 21:33:17 +0000 | [diff] [blame] | 61 | else |
Daniel Dunbar | 90604ab | 2011-04-29 18:20:17 +0000 | [diff] [blame] | 62 | setUndefined(); |
Daniel Dunbar | 8d627d3 | 2010-05-05 19:00:56 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Chris Lattner | 10b318b | 2010-01-17 21:43:43 +0000 | [diff] [blame] | 65 | void MCSymbol::print(raw_ostream &OS) const { |
Chris Lattner | acd03ae | 2010-01-17 19:23:46 +0000 | [diff] [blame] | 66 | // The name for this MCSymbol is required to be a valid target name. However, |
| 67 | // some targets support quoting names with funny characters. If the name |
| 68 | // contains a funny character, then print it quoted. |
Chris Lattner | 6edec7b | 2010-01-17 20:11:03 +0000 | [diff] [blame] | 69 | if (!NameNeedsQuoting(getName())) { |
Daniel Dunbar | 1689e0c | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 70 | OS << getName(); |
Chris Lattner | e19f978 | 2009-09-13 18:04:46 +0000 | [diff] [blame] | 71 | return; |
| 72 | } |
Jim Grosbach | 2684d9e | 2012-05-11 01:41:30 +0000 | [diff] [blame] | 73 | |
Chris Lattner | 4afcedf | 2009-09-13 18:11:09 +0000 | [diff] [blame] | 74 | OS << '"' << getName() << '"'; |
Daniel Dunbar | 1689e0c | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Manman Ren | 286c4dc | 2012-09-12 05:06:18 +0000 | [diff] [blame] | 77 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Daniel Dunbar | 1689e0c | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 78 | void MCSymbol::dump() const { |
Chris Lattner | 10b318b | 2010-01-17 21:43:43 +0000 | [diff] [blame] | 79 | print(dbgs()); |
Daniel Dunbar | 1689e0c | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 80 | } |
Manman Ren | cc77eec | 2012-09-06 19:55:56 +0000 | [diff] [blame] | 81 | #endif |