Daniel Dunbar | 1a019d8 | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 1 | //===- lib/MC/MCSymbol.cpp - MCSymbol implementation ----------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Daniel Dunbar | 1a019d8 | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 9 | #include "llvm/MC/MCSymbol.h" |
Eugene Zelenko | 3d8b0eb | 2017-02-08 22:23:19 +0000 | [diff] [blame] | 10 | #include "llvm/ADT/StringRef.h" |
Nico Weber | 432a388 | 2018-04-30 14:59:11 +0000 | [diff] [blame] | 11 | #include "llvm/Config/llvm-config.h" |
Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 12 | #include "llvm/MC/MCAsmInfo.h" |
Pete Cooper | 234b875 | 2015-06-09 18:36:13 +0000 | [diff] [blame] | 13 | #include "llvm/MC/MCContext.h" |
Daniel Dunbar | 8557426 | 2010-05-05 19:00:56 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCExpr.h" |
Eugene Zelenko | 3d8b0eb | 2017-02-08 22:23:19 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCFragment.h" |
Eugene Zelenko | 3d8b0eb | 2017-02-08 22:23:19 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Compiler.h" |
David Greene | cdf0452 | 2010-01-05 01:28:10 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Debug.h" |
Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 18 | #include "llvm/Support/ErrorHandling.h" |
Daniel Dunbar | 1a019d8 | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 19 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 3d8b0eb | 2017-02-08 22:23:19 +0000 | [diff] [blame] | 20 | #include <cassert> |
| 21 | #include <cstddef> |
| 22 | |
Daniel Dunbar | 1a019d8 | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 23 | using namespace llvm; |
| 24 | |
Chandler Carruth | 5bd31b3 | 2015-12-29 09:32:18 +0000 | [diff] [blame] | 25 | // Only the address of this fragment is ever actually used. |
| 26 | static MCDummyFragment SentinelFragment(nullptr); |
| 27 | |
Rafael Espindola | e3a20f5 | 2015-10-05 12:07:05 +0000 | [diff] [blame] | 28 | // Sentinel value for the absolute pseudo fragment. |
Chandler Carruth | 5bd31b3 | 2015-12-29 09:32:18 +0000 | [diff] [blame] | 29 | MCFragment *MCSymbol::AbsolutePseudoFragment = &SentinelFragment; |
Daniel Dunbar | 6860ac7 | 2009-08-22 07:22:36 +0000 | [diff] [blame] | 30 | |
Pete Cooper | a9ecddb | 2015-06-09 19:56:05 +0000 | [diff] [blame] | 31 | void *MCSymbol::operator new(size_t s, const StringMapEntry<bool> *Name, |
| 32 | MCContext &Ctx) { |
| 33 | // We may need more space for a Name to account for alignment. So allocate |
| 34 | // space for the storage type and not the name pointer. |
| 35 | size_t Size = s + (Name ? sizeof(NameEntryStorageTy) : 0); |
Pete Cooper | 234b875 | 2015-06-09 18:36:13 +0000 | [diff] [blame] | 36 | |
| 37 | // For safety, ensure that the alignment of a pointer is enough for an |
| 38 | // MCSymbol. This also ensures we don't need padding between the name and |
| 39 | // symbol. |
Benjamin Kramer | b250500 | 2016-10-20 15:02:18 +0000 | [diff] [blame] | 40 | static_assert((unsigned)alignof(MCSymbol) <= alignof(NameEntryStorageTy), |
Pete Cooper | 8ae395d | 2015-06-09 20:58:03 +0000 | [diff] [blame] | 41 | "Bad alignment of MCSymbol"); |
Benjamin Kramer | b250500 | 2016-10-20 15:02:18 +0000 | [diff] [blame] | 42 | void *Storage = Ctx.allocate(Size, alignof(NameEntryStorageTy)); |
Pete Cooper | a9ecddb | 2015-06-09 19:56:05 +0000 | [diff] [blame] | 43 | NameEntryStorageTy *Start = static_cast<NameEntryStorageTy*>(Storage); |
| 44 | NameEntryStorageTy *End = Start + (Name ? 1 : 0); |
Pete Cooper | 234b875 | 2015-06-09 18:36:13 +0000 | [diff] [blame] | 45 | return End; |
| 46 | } |
| 47 | |
Daniel Dunbar | 8557426 | 2010-05-05 19:00:56 +0000 | [diff] [blame] | 48 | void MCSymbol::setVariableValue(const MCExpr *Value) { |
Rafael Espindola | 46c79ef | 2010-11-15 14:40:36 +0000 | [diff] [blame] | 49 | assert(!IsUsed && "Cannot set a variable that has already been used."); |
Daniel Dunbar | 8557426 | 2010-05-05 19:00:56 +0000 | [diff] [blame] | 50 | assert(Value && "Invalid variable value!"); |
Pete Cooper | 63b4dc4 | 2015-06-22 19:57:33 +0000 | [diff] [blame] | 51 | assert((SymbolContents == SymContentsUnset || |
| 52 | SymbolContents == SymContentsVariable) && |
| 53 | "Cannot give common/offset symbol a variable value"); |
Daniel Dunbar | 8557426 | 2010-05-05 19:00:56 +0000 | [diff] [blame] | 54 | this->Value = Value; |
Pete Cooper | 63b4dc4 | 2015-06-22 19:57:33 +0000 | [diff] [blame] | 55 | SymbolContents = SymContentsVariable; |
Pete Cooper | 2b00f08 | 2015-06-30 20:54:21 +0000 | [diff] [blame] | 56 | setUndefined(); |
Daniel Dunbar | 8557426 | 2010-05-05 19:00:56 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 59 | void MCSymbol::print(raw_ostream &OS, const MCAsmInfo *MAI) const { |
Chris Lattner | 83e872e | 2010-01-17 19:23:46 +0000 | [diff] [blame] | 60 | // The name for this MCSymbol is required to be a valid target name. However, |
| 61 | // some targets support quoting names with funny characters. If the name |
| 62 | // contains a funny character, then print it quoted. |
Rafael Espindola | fe4e088 | 2013-11-14 06:05:49 +0000 | [diff] [blame] | 63 | StringRef Name = getName(); |
Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 64 | if (!MAI || MAI->isValidUnquotedName(Name)) { |
Rafael Espindola | fe4e088 | 2013-11-14 06:05:49 +0000 | [diff] [blame] | 65 | OS << Name; |
Chris Lattner | 3d6c8eb | 2009-09-13 18:04:46 +0000 | [diff] [blame] | 66 | return; |
| 67 | } |
Jim Grosbach | dc1e36e | 2012-05-11 01:41:30 +0000 | [diff] [blame] | 68 | |
Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 69 | if (MAI && !MAI->supportsNameQuoting()) |
| 70 | report_fatal_error("Symbol name with unsupported characters"); |
| 71 | |
Rafael Espindola | fe4e088 | 2013-11-14 06:05:49 +0000 | [diff] [blame] | 72 | OS << '"'; |
Matt Arsenault | 8b64355 | 2015-06-09 00:31:39 +0000 | [diff] [blame] | 73 | for (char C : Name) { |
Rafael Espindola | fe4e088 | 2013-11-14 06:05:49 +0000 | [diff] [blame] | 74 | if (C == '\n') |
| 75 | OS << "\\n"; |
| 76 | else if (C == '"') |
| 77 | OS << "\\\""; |
| 78 | else |
| 79 | OS << C; |
| 80 | } |
| 81 | OS << '"'; |
Daniel Dunbar | 1a019d8 | 2009-08-14 03:41:23 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Aaron Ballman | 615eb47 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 84 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Matthias Braun | 8c209aa | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 85 | LLVM_DUMP_METHOD void MCSymbol::dump() const { |
| 86 | dbgs() << *this; |
| 87 | } |
| 88 | #endif |