blob: 5502c658f56544bcef00f3e53daa643f35eb9e55 [file] [log] [blame]
Daniel Dunbar1a019d82009-08-14 03:41:23 +00001//===- 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 Carruth6bda14b2017-06-06 11:49:48 +000010#include "llvm/MC/MCSymbol.h"
Eugene Zelenko3d8b0eb2017-02-08 22:23:19 +000011#include "llvm/ADT/StringRef.h"
Nico Weber432a3882018-04-30 14:59:11 +000012#include "llvm/Config/llvm-config.h"
Matt Arsenault8b643552015-06-09 00:31:39 +000013#include "llvm/MC/MCAsmInfo.h"
Pete Cooper234b8752015-06-09 18:36:13 +000014#include "llvm/MC/MCContext.h"
Daniel Dunbar85574262010-05-05 19:00:56 +000015#include "llvm/MC/MCExpr.h"
Eugene Zelenko3d8b0eb2017-02-08 22:23:19 +000016#include "llvm/MC/MCFragment.h"
Eugene Zelenko3d8b0eb2017-02-08 22:23:19 +000017#include "llvm/Support/Compiler.h"
David Greenecdf04522010-01-05 01:28:10 +000018#include "llvm/Support/Debug.h"
Matt Arsenault8b643552015-06-09 00:31:39 +000019#include "llvm/Support/ErrorHandling.h"
Daniel Dunbar1a019d82009-08-14 03:41:23 +000020#include "llvm/Support/raw_ostream.h"
Eugene Zelenko3d8b0eb2017-02-08 22:23:19 +000021#include <cassert>
22#include <cstddef>
23
Daniel Dunbar1a019d82009-08-14 03:41:23 +000024using namespace llvm;
25
Chandler Carruth5bd31b32015-12-29 09:32:18 +000026// Only the address of this fragment is ever actually used.
27static MCDummyFragment SentinelFragment(nullptr);
28
Rafael Espindolae3a20f52015-10-05 12:07:05 +000029// Sentinel value for the absolute pseudo fragment.
Chandler Carruth5bd31b32015-12-29 09:32:18 +000030MCFragment *MCSymbol::AbsolutePseudoFragment = &SentinelFragment;
Daniel Dunbar6860ac72009-08-22 07:22:36 +000031
Pete Coopera9ecddb2015-06-09 19:56:05 +000032void *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 Cooper234b8752015-06-09 18:36:13 +000037
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 Kramerb2505002016-10-20 15:02:18 +000041 static_assert((unsigned)alignof(MCSymbol) <= alignof(NameEntryStorageTy),
Pete Cooper8ae395d2015-06-09 20:58:03 +000042 "Bad alignment of MCSymbol");
Benjamin Kramerb2505002016-10-20 15:02:18 +000043 void *Storage = Ctx.allocate(Size, alignof(NameEntryStorageTy));
Pete Coopera9ecddb2015-06-09 19:56:05 +000044 NameEntryStorageTy *Start = static_cast<NameEntryStorageTy*>(Storage);
45 NameEntryStorageTy *End = Start + (Name ? 1 : 0);
Pete Cooper234b8752015-06-09 18:36:13 +000046 return End;
47}
48
Daniel Dunbar85574262010-05-05 19:00:56 +000049void MCSymbol::setVariableValue(const MCExpr *Value) {
Rafael Espindola46c79ef2010-11-15 14:40:36 +000050 assert(!IsUsed && "Cannot set a variable that has already been used.");
Daniel Dunbar85574262010-05-05 19:00:56 +000051 assert(Value && "Invalid variable value!");
Pete Cooper63b4dc42015-06-22 19:57:33 +000052 assert((SymbolContents == SymContentsUnset ||
53 SymbolContents == SymContentsVariable) &&
54 "Cannot give common/offset symbol a variable value");
Daniel Dunbar85574262010-05-05 19:00:56 +000055 this->Value = Value;
Pete Cooper63b4dc42015-06-22 19:57:33 +000056 SymbolContents = SymContentsVariable;
Pete Cooper2b00f082015-06-30 20:54:21 +000057 setUndefined();
Daniel Dunbar85574262010-05-05 19:00:56 +000058}
59
Matt Arsenault8b643552015-06-09 00:31:39 +000060void MCSymbol::print(raw_ostream &OS, const MCAsmInfo *MAI) const {
Chris Lattner83e872e2010-01-17 19:23:46 +000061 // 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 Espindolafe4e0882013-11-14 06:05:49 +000064 StringRef Name = getName();
Matt Arsenault8b643552015-06-09 00:31:39 +000065 if (!MAI || MAI->isValidUnquotedName(Name)) {
Rafael Espindolafe4e0882013-11-14 06:05:49 +000066 OS << Name;
Chris Lattner3d6c8eb2009-09-13 18:04:46 +000067 return;
68 }
Jim Grosbachdc1e36e2012-05-11 01:41:30 +000069
Matt Arsenault8b643552015-06-09 00:31:39 +000070 if (MAI && !MAI->supportsNameQuoting())
71 report_fatal_error("Symbol name with unsupported characters");
72
Rafael Espindolafe4e0882013-11-14 06:05:49 +000073 OS << '"';
Matt Arsenault8b643552015-06-09 00:31:39 +000074 for (char C : Name) {
Rafael Espindolafe4e0882013-11-14 06:05:49 +000075 if (C == '\n')
76 OS << "\\n";
77 else if (C == '"')
78 OS << "\\\"";
79 else
80 OS << C;
81 }
82 OS << '"';
Daniel Dunbar1a019d82009-08-14 03:41:23 +000083}
84
Aaron Ballman615eb472017-10-15 14:32:27 +000085#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Matthias Braun8c209aa2017-01-28 02:02:38 +000086LLVM_DUMP_METHOD void MCSymbol::dump() const {
87 dbgs() << *this;
88}
89#endif