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" |
Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 11 | #include "llvm/MC/MCAsmInfo.h" |
Pete Cooper | 234b875 | 2015-06-09 18:36:13 +0000 | [diff] [blame] | 12 | #include "llvm/MC/MCContext.h" |
Daniel Dunbar | 8557426 | 2010-05-05 19:00:56 +0000 | [diff] [blame] | 13 | #include "llvm/MC/MCExpr.h" |
David Greene | cdf0452 | 2010-01-05 01:28:10 +0000 | [diff] [blame] | 14 | #include "llvm/Support/Debug.h" |
Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 15 | #include "llvm/Support/ErrorHandling.h" |
Daniel Dunbar | 1a019d8 | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 16 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | 1a019d8 | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 17 | using namespace llvm; |
| 18 | |
Daniel Dunbar | 6860ac7 | 2009-08-22 07:22:36 +0000 | [diff] [blame] | 19 | // Sentinel value for the absolute pseudo section. |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 20 | MCSection *MCSymbol::AbsolutePseudoSection = reinterpret_cast<MCSection *>(1); |
Daniel Dunbar | 6860ac7 | 2009-08-22 07:22:36 +0000 | [diff] [blame] | 21 | |
Pete Cooper | a9ecddb | 2015-06-09 19:56:05 +0000 | [diff] [blame] | 22 | void *MCSymbol::operator new(size_t s, const StringMapEntry<bool> *Name, |
| 23 | MCContext &Ctx) { |
| 24 | // We may need more space for a Name to account for alignment. So allocate |
| 25 | // space for the storage type and not the name pointer. |
| 26 | size_t Size = s + (Name ? sizeof(NameEntryStorageTy) : 0); |
Pete Cooper | 234b875 | 2015-06-09 18:36:13 +0000 | [diff] [blame] | 27 | |
| 28 | // For safety, ensure that the alignment of a pointer is enough for an |
| 29 | // MCSymbol. This also ensures we don't need padding between the name and |
| 30 | // symbol. |
Pete Cooper | 17d6359 | 2015-06-09 23:33:35 +0000 | [diff] [blame] | 31 | static_assert((unsigned)AlignOf<MCSymbol>::Alignment <= |
Pete Cooper | 8ae395d | 2015-06-09 20:58:03 +0000 | [diff] [blame] | 32 | AlignOf<NameEntryStorageTy>::Alignment, |
| 33 | "Bad alignment of MCSymbol"); |
Pete Cooper | a9ecddb | 2015-06-09 19:56:05 +0000 | [diff] [blame] | 34 | void *Storage = Ctx.allocate(Size, alignOf<NameEntryStorageTy>()); |
| 35 | NameEntryStorageTy *Start = static_cast<NameEntryStorageTy*>(Storage); |
| 36 | NameEntryStorageTy *End = Start + (Name ? 1 : 0); |
Pete Cooper | 234b875 | 2015-06-09 18:36:13 +0000 | [diff] [blame] | 37 | return End; |
| 38 | } |
| 39 | |
Daniel Dunbar | 8557426 | 2010-05-05 19:00:56 +0000 | [diff] [blame] | 40 | void MCSymbol::setVariableValue(const MCExpr *Value) { |
Rafael Espindola | 46c79ef | 2010-11-15 14:40:36 +0000 | [diff] [blame] | 41 | assert(!IsUsed && "Cannot set a variable that has already been used."); |
Daniel Dunbar | 8557426 | 2010-05-05 19:00:56 +0000 | [diff] [blame] | 42 | assert(Value && "Invalid variable value!"); |
Pete Cooper | 63b4dc4 | 2015-06-22 19:57:33 +0000 | [diff] [blame^] | 43 | assert((SymbolContents == SymContentsUnset || |
| 44 | SymbolContents == SymContentsVariable) && |
| 45 | "Cannot give common/offset symbol a variable value"); |
Daniel Dunbar | 8557426 | 2010-05-05 19:00:56 +0000 | [diff] [blame] | 46 | this->Value = Value; |
Pete Cooper | 63b4dc4 | 2015-06-22 19:57:33 +0000 | [diff] [blame^] | 47 | SymbolContents = SymContentsVariable; |
Pete Cooper | 11472c0 | 2015-06-08 18:41:57 +0000 | [diff] [blame] | 48 | SectionOrFragment = nullptr; |
Daniel Dunbar | 8557426 | 2010-05-05 19:00:56 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 51 | void MCSymbol::print(raw_ostream &OS, const MCAsmInfo *MAI) const { |
Chris Lattner | 83e872e | 2010-01-17 19:23:46 +0000 | [diff] [blame] | 52 | // The name for this MCSymbol is required to be a valid target name. However, |
| 53 | // some targets support quoting names with funny characters. If the name |
| 54 | // contains a funny character, then print it quoted. |
Rafael Espindola | fe4e088 | 2013-11-14 06:05:49 +0000 | [diff] [blame] | 55 | StringRef Name = getName(); |
Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 56 | if (!MAI || MAI->isValidUnquotedName(Name)) { |
Rafael Espindola | fe4e088 | 2013-11-14 06:05:49 +0000 | [diff] [blame] | 57 | OS << Name; |
Chris Lattner | 3d6c8eb | 2009-09-13 18:04:46 +0000 | [diff] [blame] | 58 | return; |
| 59 | } |
Jim Grosbach | dc1e36e | 2012-05-11 01:41:30 +0000 | [diff] [blame] | 60 | |
Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 61 | if (MAI && !MAI->supportsNameQuoting()) |
| 62 | report_fatal_error("Symbol name with unsupported characters"); |
| 63 | |
Rafael Espindola | fe4e088 | 2013-11-14 06:05:49 +0000 | [diff] [blame] | 64 | OS << '"'; |
Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 65 | for (char C : Name) { |
Rafael Espindola | fe4e088 | 2013-11-14 06:05:49 +0000 | [diff] [blame] | 66 | if (C == '\n') |
| 67 | OS << "\\n"; |
| 68 | else if (C == '"') |
| 69 | OS << "\\\""; |
| 70 | else |
| 71 | OS << C; |
| 72 | } |
| 73 | OS << '"'; |
Daniel Dunbar | 1a019d8 | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Manman Ren | 49d684e | 2012-09-12 05:06:18 +0000 | [diff] [blame] | 76 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Rafael Espindola | f4a1365 | 2015-05-27 13:05:42 +0000 | [diff] [blame] | 77 | void MCSymbol::dump() const { dbgs() << *this; } |
Manman Ren | c3366cc | 2012-09-06 19:55:56 +0000 | [diff] [blame] | 78 | #endif |