blob: 20d985df7ea0778c660bf8b0f205d5eaaa053f12 [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.
Benjamin Kramerb2505002016-10-20 15:02:18 +000034 static_assert((unsigned)alignof(MCSymbol) <= alignof(NameEntryStorageTy),
Pete Cooper8ae395d2015-06-09 20:58:03 +000035 "Bad alignment of MCSymbol");
Benjamin Kramerb2505002016-10-20 15:02:18 +000036 void *Storage = Ctx.allocate(Size, alignof(NameEntryStorageTy));
Pete Coopera9ecddb2015-06-09 19:56:05 +000037 NameEntryStorageTy *Start = static_cast<NameEntryStorageTy*>(Storage);
38 NameEntryStorageTy *End = Start + (Name ? 1 : 0);
Pete Cooper234b8752015-06-09 18:36:13 +000039 return End;
40}
41
Daniel Dunbar85574262010-05-05 19:00:56 +000042void MCSymbol::setVariableValue(const MCExpr *Value) {
Rafael Espindola46c79ef2010-11-15 14:40:36 +000043 assert(!IsUsed && "Cannot set a variable that has already been used.");
Daniel Dunbar85574262010-05-05 19:00:56 +000044 assert(Value && "Invalid variable value!");
Pete Cooper63b4dc42015-06-22 19:57:33 +000045 assert((SymbolContents == SymContentsUnset ||
46 SymbolContents == SymContentsVariable) &&
47 "Cannot give common/offset symbol a variable value");
Daniel Dunbar85574262010-05-05 19:00:56 +000048 this->Value = Value;
Pete Cooper63b4dc42015-06-22 19:57:33 +000049 SymbolContents = SymContentsVariable;
Pete Cooper2b00f082015-06-30 20:54:21 +000050 setUndefined();
Daniel Dunbar85574262010-05-05 19:00:56 +000051}
52
Matt Arsenault8b643552015-06-09 00:31:39 +000053void MCSymbol::print(raw_ostream &OS, const MCAsmInfo *MAI) const {
Chris Lattner83e872e2010-01-17 19:23:46 +000054 // The name for this MCSymbol is required to be a valid target name. However,
55 // some targets support quoting names with funny characters. If the name
56 // contains a funny character, then print it quoted.
Rafael Espindolafe4e0882013-11-14 06:05:49 +000057 StringRef Name = getName();
Matt Arsenault8b643552015-06-09 00:31:39 +000058 if (!MAI || MAI->isValidUnquotedName(Name)) {
Rafael Espindolafe4e0882013-11-14 06:05:49 +000059 OS << Name;
Chris Lattner3d6c8eb2009-09-13 18:04:46 +000060 return;
61 }
Jim Grosbachdc1e36e2012-05-11 01:41:30 +000062
Matt Arsenault8b643552015-06-09 00:31:39 +000063 if (MAI && !MAI->supportsNameQuoting())
64 report_fatal_error("Symbol name with unsupported characters");
65
Rafael Espindolafe4e0882013-11-14 06:05:49 +000066 OS << '"';
Matt Arsenault8b643552015-06-09 00:31:39 +000067 for (char C : Name) {
Rafael Espindolafe4e0882013-11-14 06:05:49 +000068 if (C == '\n')
69 OS << "\\n";
70 else if (C == '"')
71 OS << "\\\"";
72 else
73 OS << C;
74 }
75 OS << '"';
Daniel Dunbar1a019d82009-08-14 03:41:23 +000076}
77
Yaron Kereneb2a2542016-01-29 20:50:44 +000078LLVM_DUMP_METHOD void MCSymbol::dump() const { dbgs() << *this; }