blob: 2ddece6bddce06000ca276a1b76e8bddc8b99110 [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
10#include "llvm/MC/MCSymbol.h"
Matt Arsenault8b643552015-06-09 00:31:39 +000011#include "llvm/MC/MCAsmInfo.h"
Pete Cooper234b8752015-06-09 18:36:13 +000012#include "llvm/MC/MCContext.h"
Daniel Dunbar85574262010-05-05 19:00:56 +000013#include "llvm/MC/MCExpr.h"
David Greenecdf04522010-01-05 01:28:10 +000014#include "llvm/Support/Debug.h"
Matt Arsenault8b643552015-06-09 00:31:39 +000015#include "llvm/Support/ErrorHandling.h"
Daniel Dunbar1a019d82009-08-14 03:41:23 +000016#include "llvm/Support/raw_ostream.h"
Daniel Dunbar1a019d82009-08-14 03:41:23 +000017using namespace llvm;
18
Chandler Carruth5bd31b32015-12-29 09:32:18 +000019// Only the address of this fragment is ever actually used.
20static MCDummyFragment SentinelFragment(nullptr);
21
Rafael Espindolae3a20f52015-10-05 12:07:05 +000022// Sentinel value for the absolute pseudo fragment.
Chandler Carruth5bd31b32015-12-29 09:32:18 +000023MCFragment *MCSymbol::AbsolutePseudoFragment = &SentinelFragment;
Daniel Dunbar6860ac72009-08-22 07:22:36 +000024
Pete Coopera9ecddb2015-06-09 19:56:05 +000025void *MCSymbol::operator new(size_t s, const StringMapEntry<bool> *Name,
26 MCContext &Ctx) {
27 // We may need more space for a Name to account for alignment. So allocate
28 // space for the storage type and not the name pointer.
29 size_t Size = s + (Name ? sizeof(NameEntryStorageTy) : 0);
Pete Cooper234b8752015-06-09 18:36:13 +000030
31 // For safety, ensure that the alignment of a pointer is enough for an
32 // MCSymbol. This also ensures we don't need padding between the name and
33 // symbol.
Pete Cooper17d63592015-06-09 23:33:35 +000034 static_assert((unsigned)AlignOf<MCSymbol>::Alignment <=
Pete Cooper8ae395d2015-06-09 20:58:03 +000035 AlignOf<NameEntryStorageTy>::Alignment,
36 "Bad alignment of MCSymbol");
Pete Coopera9ecddb2015-06-09 19:56:05 +000037 void *Storage = Ctx.allocate(Size, alignOf<NameEntryStorageTy>());
38 NameEntryStorageTy *Start = static_cast<NameEntryStorageTy*>(Storage);
39 NameEntryStorageTy *End = Start + (Name ? 1 : 0);
Pete Cooper234b8752015-06-09 18:36:13 +000040 return End;
41}
42
Daniel Dunbar85574262010-05-05 19:00:56 +000043void MCSymbol::setVariableValue(const MCExpr *Value) {
Rafael Espindola46c79ef2010-11-15 14:40:36 +000044 assert(!IsUsed && "Cannot set a variable that has already been used.");
Daniel Dunbar85574262010-05-05 19:00:56 +000045 assert(Value && "Invalid variable value!");
Pete Cooper63b4dc42015-06-22 19:57:33 +000046 assert((SymbolContents == SymContentsUnset ||
47 SymbolContents == SymContentsVariable) &&
48 "Cannot give common/offset symbol a variable value");
Daniel Dunbar85574262010-05-05 19:00:56 +000049 this->Value = Value;
Pete Cooper63b4dc42015-06-22 19:57:33 +000050 SymbolContents = SymContentsVariable;
Pete Cooper2b00f082015-06-30 20:54:21 +000051 setUndefined();
Daniel Dunbar85574262010-05-05 19:00:56 +000052}
53
Matt Arsenault8b643552015-06-09 00:31:39 +000054void MCSymbol::print(raw_ostream &OS, const MCAsmInfo *MAI) const {
Chris Lattner83e872e2010-01-17 19:23:46 +000055 // The name for this MCSymbol is required to be a valid target name. However,
56 // some targets support quoting names with funny characters. If the name
57 // contains a funny character, then print it quoted.
Rafael Espindolafe4e0882013-11-14 06:05:49 +000058 StringRef Name = getName();
Matt Arsenault8b643552015-06-09 00:31:39 +000059 if (!MAI || MAI->isValidUnquotedName(Name)) {
Rafael Espindolafe4e0882013-11-14 06:05:49 +000060 OS << Name;
Chris Lattner3d6c8eb2009-09-13 18:04:46 +000061 return;
62 }
Jim Grosbachdc1e36e2012-05-11 01:41:30 +000063
Matt Arsenault8b643552015-06-09 00:31:39 +000064 if (MAI && !MAI->supportsNameQuoting())
65 report_fatal_error("Symbol name with unsupported characters");
66
Rafael Espindolafe4e0882013-11-14 06:05:49 +000067 OS << '"';
Matt Arsenault8b643552015-06-09 00:31:39 +000068 for (char C : Name) {
Rafael Espindolafe4e0882013-11-14 06:05:49 +000069 if (C == '\n')
70 OS << "\\n";
71 else if (C == '"')
72 OS << "\\\"";
73 else
74 OS << C;
75 }
76 OS << '"';
Daniel Dunbar1a019d82009-08-14 03:41:23 +000077}
78
Manman Ren49d684e2012-09-12 05:06:18 +000079#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Kereneb2a2542016-01-29 20:50:44 +000080LLVM_DUMP_METHOD void MCSymbol::dump() const { dbgs() << *this; }
Manman Renc3366cc2012-09-06 19:55:56 +000081#endif