Daniel Dunbar | 1689e0cf | 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 | |
Chandler Carruth | e3e43d9 | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 10 | #include "llvm/MC/MCSymbol.h" |
Eugene Zelenko | 498b48e | 2017-02-08 22:23:19 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/StringRef.h" |
Nico Weber | 0f38c60 | 2018-04-30 14:59:11 +0000 | [diff] [blame] | 12 | #include "llvm/Config/llvm-config.h" |
Matt Arsenault | d99ce2f | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 13 | #include "llvm/MC/MCAsmInfo.h" |
Pete Cooper | 79e04c5 | 2015-06-09 18:36:13 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCContext.h" |
Daniel Dunbar | 8d627d3 | 2010-05-05 19:00:56 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCExpr.h" |
Eugene Zelenko | 498b48e | 2017-02-08 22:23:19 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCFragment.h" |
Eugene Zelenko | 498b48e | 2017-02-08 22:23:19 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Compiler.h" |
David Greene | f24dd5c | 2010-01-05 01:28:10 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Debug.h" |
Matt Arsenault | d99ce2f | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 19 | #include "llvm/Support/ErrorHandling.h" |
Daniel Dunbar | 1689e0cf | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 498b48e | 2017-02-08 22:23:19 +0000 | [diff] [blame] | 21 | #include <cassert> |
| 22 | #include <cstddef> |
| 23 | |
Daniel Dunbar | 1689e0cf | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 24 | using namespace llvm; |
| 25 | |
Chandler Carruth | a202d8e | 2015-12-29 09:32:18 +0000 | [diff] [blame] | 26 | // Only the address of this fragment is ever actually used. |
| 27 | static MCDummyFragment SentinelFragment(nullptr); |
| 28 | |
Rafael Espindola | 0eba49c | 2015-10-05 12:07:05 +0000 | [diff] [blame] | 29 | // Sentinel value for the absolute pseudo fragment. |
Chandler Carruth | a202d8e | 2015-12-29 09:32:18 +0000 | [diff] [blame] | 30 | MCFragment *MCSymbol::AbsolutePseudoFragment = &SentinelFragment; |
Daniel Dunbar | 8906ff1 | 2009-08-22 07:22:36 +0000 | [diff] [blame] | 31 | |
Pete Cooper | 5c8a22f | 2015-06-09 19:56:05 +0000 | [diff] [blame] | 32 | void *MCSymbol::operator new(size_t s, const StringMapEntry<bool> *Name, |
| 33 | MCContext &Ctx) { |
| 34 | // We may need more space for a Name to account for alignment. So allocate |
| 35 | // space for the storage type and not the name pointer. |
| 36 | size_t Size = s + (Name ? sizeof(NameEntryStorageTy) : 0); |
Pete Cooper | 79e04c5 | 2015-06-09 18:36:13 +0000 | [diff] [blame] | 37 | |
| 38 | // For safety, ensure that the alignment of a pointer is enough for an |
| 39 | // MCSymbol. This also ensures we don't need padding between the name and |
| 40 | // symbol. |
Benjamin Kramer | cb58e1e | 2016-10-20 15:02:18 +0000 | [diff] [blame] | 41 | static_assert((unsigned)alignof(MCSymbol) <= alignof(NameEntryStorageTy), |
Pete Cooper | a96fc7a | 2015-06-09 20:58:03 +0000 | [diff] [blame] | 42 | "Bad alignment of MCSymbol"); |
Benjamin Kramer | cb58e1e | 2016-10-20 15:02:18 +0000 | [diff] [blame] | 43 | void *Storage = Ctx.allocate(Size, alignof(NameEntryStorageTy)); |
Pete Cooper | 5c8a22f | 2015-06-09 19:56:05 +0000 | [diff] [blame] | 44 | NameEntryStorageTy *Start = static_cast<NameEntryStorageTy*>(Storage); |
| 45 | NameEntryStorageTy *End = Start + (Name ? 1 : 0); |
Pete Cooper | 79e04c5 | 2015-06-09 18:36:13 +0000 | [diff] [blame] | 46 | return End; |
| 47 | } |
| 48 | |
Daniel Dunbar | 8d627d3 | 2010-05-05 19:00:56 +0000 | [diff] [blame] | 49 | void MCSymbol::setVariableValue(const MCExpr *Value) { |
Rafael Espindola | db9835d | 2010-11-15 14:40:36 +0000 | [diff] [blame] | 50 | assert(!IsUsed && "Cannot set a variable that has already been used."); |
Daniel Dunbar | 8d627d3 | 2010-05-05 19:00:56 +0000 | [diff] [blame] | 51 | assert(Value && "Invalid variable value!"); |
Pete Cooper | 8b4b91a | 2015-06-22 19:57:33 +0000 | [diff] [blame] | 52 | assert((SymbolContents == SymContentsUnset || |
| 53 | SymbolContents == SymContentsVariable) && |
| 54 | "Cannot give common/offset symbol a variable value"); |
Daniel Dunbar | 8d627d3 | 2010-05-05 19:00:56 +0000 | [diff] [blame] | 55 | this->Value = Value; |
Pete Cooper | 8b4b91a | 2015-06-22 19:57:33 +0000 | [diff] [blame] | 56 | SymbolContents = SymContentsVariable; |
Pete Cooper | 8cdbab9 | 2015-06-30 20:54:21 +0000 | [diff] [blame] | 57 | setUndefined(); |
Daniel Dunbar | 8d627d3 | 2010-05-05 19:00:56 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Matt Arsenault | d99ce2f | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 60 | void MCSymbol::print(raw_ostream &OS, const MCAsmInfo *MAI) const { |
Chris Lattner | acd03ae | 2010-01-17 19:23:46 +0000 | [diff] [blame] | 61 | // The name for this MCSymbol is required to be a valid target name. However, |
| 62 | // some targets support quoting names with funny characters. If the name |
| 63 | // contains a funny character, then print it quoted. |
Rafael Espindola | 732cf2d | 2013-11-14 06:05:49 +0000 | [diff] [blame] | 64 | StringRef Name = getName(); |
Matt Arsenault | d99ce2f | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 65 | if (!MAI || MAI->isValidUnquotedName(Name)) { |
Rafael Espindola | 732cf2d | 2013-11-14 06:05:49 +0000 | [diff] [blame] | 66 | OS << Name; |
Chris Lattner | e19f978 | 2009-09-13 18:04:46 +0000 | [diff] [blame] | 67 | return; |
| 68 | } |
Jim Grosbach | 2684d9e | 2012-05-11 01:41:30 +0000 | [diff] [blame] | 69 | |
Matt Arsenault | d99ce2f | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 70 | if (MAI && !MAI->supportsNameQuoting()) |
| 71 | report_fatal_error("Symbol name with unsupported characters"); |
| 72 | |
Rafael Espindola | 732cf2d | 2013-11-14 06:05:49 +0000 | [diff] [blame] | 73 | OS << '"'; |
Matt Arsenault | d99ce2f | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 74 | for (char C : Name) { |
Rafael Espindola | 732cf2d | 2013-11-14 06:05:49 +0000 | [diff] [blame] | 75 | if (C == '\n') |
| 76 | OS << "\\n"; |
| 77 | else if (C == '"') |
| 78 | OS << "\\\""; |
| 79 | else |
| 80 | OS << C; |
| 81 | } |
| 82 | OS << '"'; |
Daniel Dunbar | 1689e0cf | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Aaron Ballman | 1d03d38 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 85 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Matthias Braun | 88d2075 | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 86 | LLVM_DUMP_METHOD void MCSymbol::dump() const { |
| 87 | dbgs() << *this; |
| 88 | } |
| 89 | #endif |