blob: bbd34f157400d84d61197147f063f35e382bfda1 [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
Daniel Dunbar6860ac72009-08-22 07:22:36 +000019// Sentinel value for the absolute pseudo section.
Rafael Espindola0709a7b2015-05-21 19:20:38 +000020MCSection *MCSymbol::AbsolutePseudoSection = reinterpret_cast<MCSection *>(1);
Daniel Dunbar6860ac72009-08-22 07:22:36 +000021
Pete Coopera9ecddb2015-06-09 19:56:05 +000022void *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 Cooper234b8752015-06-09 18:36:13 +000027
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 Cooper17d63592015-06-09 23:33:35 +000031 static_assert((unsigned)AlignOf<MCSymbol>::Alignment <=
Pete Cooper8ae395d2015-06-09 20:58:03 +000032 AlignOf<NameEntryStorageTy>::Alignment,
33 "Bad alignment of MCSymbol");
Pete Coopera9ecddb2015-06-09 19:56:05 +000034 void *Storage = Ctx.allocate(Size, alignOf<NameEntryStorageTy>());
35 NameEntryStorageTy *Start = static_cast<NameEntryStorageTy*>(Storage);
36 NameEntryStorageTy *End = Start + (Name ? 1 : 0);
Pete Cooper234b8752015-06-09 18:36:13 +000037 return End;
38}
39
Daniel Dunbar85574262010-05-05 19:00:56 +000040void MCSymbol::setVariableValue(const MCExpr *Value) {
Rafael Espindola46c79ef2010-11-15 14:40:36 +000041 assert(!IsUsed && "Cannot set a variable that has already been used.");
Daniel Dunbar85574262010-05-05 19:00:56 +000042 assert(Value && "Invalid variable value!");
Pete Cooper63b4dc42015-06-22 19:57:33 +000043 assert((SymbolContents == SymContentsUnset ||
44 SymbolContents == SymContentsVariable) &&
45 "Cannot give common/offset symbol a variable value");
Daniel Dunbar85574262010-05-05 19:00:56 +000046 this->Value = Value;
Pete Cooper63b4dc42015-06-22 19:57:33 +000047 SymbolContents = SymContentsVariable;
Pete Cooper11472c02015-06-08 18:41:57 +000048 SectionOrFragment = nullptr;
Daniel Dunbar85574262010-05-05 19:00:56 +000049}
50
Matt Arsenault8b643552015-06-09 00:31:39 +000051void MCSymbol::print(raw_ostream &OS, const MCAsmInfo *MAI) const {
Chris Lattner83e872e2010-01-17 19:23:46 +000052 // 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 Espindolafe4e0882013-11-14 06:05:49 +000055 StringRef Name = getName();
Matt Arsenault8b643552015-06-09 00:31:39 +000056 if (!MAI || MAI->isValidUnquotedName(Name)) {
Rafael Espindolafe4e0882013-11-14 06:05:49 +000057 OS << Name;
Chris Lattner3d6c8eb2009-09-13 18:04:46 +000058 return;
59 }
Jim Grosbachdc1e36e2012-05-11 01:41:30 +000060
Matt Arsenault8b643552015-06-09 00:31:39 +000061 if (MAI && !MAI->supportsNameQuoting())
62 report_fatal_error("Symbol name with unsupported characters");
63
Rafael Espindolafe4e0882013-11-14 06:05:49 +000064 OS << '"';
Matt Arsenault8b643552015-06-09 00:31:39 +000065 for (char C : Name) {
Rafael Espindolafe4e0882013-11-14 06:05:49 +000066 if (C == '\n')
67 OS << "\\n";
68 else if (C == '"')
69 OS << "\\\"";
70 else
71 OS << C;
72 }
73 OS << '"';
Daniel Dunbar1a019d82009-08-14 03:41:23 +000074}
75
Manman Ren49d684e2012-09-12 05:06:18 +000076#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Rafael Espindolaf4a13652015-05-27 13:05:42 +000077void MCSymbol::dump() const { dbgs() << *this; }
Manman Renc3366cc2012-09-06 19:55:56 +000078#endif