blob: 5502c658f56544bcef00f3e53daa643f35eb9e55 [file] [log] [blame]
Daniel Dunbar1689e0cf2009-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 Carruthe3e43d92017-06-06 11:49:48 +000010#include "llvm/MC/MCSymbol.h"
Eugene Zelenko498b48e2017-02-08 22:23:19 +000011#include "llvm/ADT/StringRef.h"
Nico Weber0f38c602018-04-30 14:59:11 +000012#include "llvm/Config/llvm-config.h"
Matt Arsenaultd99ce2f2015-06-09 00:31:39 +000013#include "llvm/MC/MCAsmInfo.h"
Pete Cooper79e04c52015-06-09 18:36:13 +000014#include "llvm/MC/MCContext.h"
Daniel Dunbar8d627d32010-05-05 19:00:56 +000015#include "llvm/MC/MCExpr.h"
Eugene Zelenko498b48e2017-02-08 22:23:19 +000016#include "llvm/MC/MCFragment.h"
Eugene Zelenko498b48e2017-02-08 22:23:19 +000017#include "llvm/Support/Compiler.h"
David Greenef24dd5c2010-01-05 01:28:10 +000018#include "llvm/Support/Debug.h"
Matt Arsenaultd99ce2f2015-06-09 00:31:39 +000019#include "llvm/Support/ErrorHandling.h"
Daniel Dunbar1689e0cf2009-08-14 03:41:23 +000020#include "llvm/Support/raw_ostream.h"
Eugene Zelenko498b48e2017-02-08 22:23:19 +000021#include <cassert>
22#include <cstddef>
23
Daniel Dunbar1689e0cf2009-08-14 03:41:23 +000024using namespace llvm;
25
Chandler Carrutha202d8e2015-12-29 09:32:18 +000026// Only the address of this fragment is ever actually used.
27static MCDummyFragment SentinelFragment(nullptr);
28
Rafael Espindola0eba49c2015-10-05 12:07:05 +000029// Sentinel value for the absolute pseudo fragment.
Chandler Carrutha202d8e2015-12-29 09:32:18 +000030MCFragment *MCSymbol::AbsolutePseudoFragment = &SentinelFragment;
Daniel Dunbar8906ff12009-08-22 07:22:36 +000031
Pete Cooper5c8a22f2015-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 Cooper79e04c52015-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 Kramercb58e1e2016-10-20 15:02:18 +000041 static_assert((unsigned)alignof(MCSymbol) <= alignof(NameEntryStorageTy),
Pete Coopera96fc7a2015-06-09 20:58:03 +000042 "Bad alignment of MCSymbol");
Benjamin Kramercb58e1e2016-10-20 15:02:18 +000043 void *Storage = Ctx.allocate(Size, alignof(NameEntryStorageTy));
Pete Cooper5c8a22f2015-06-09 19:56:05 +000044 NameEntryStorageTy *Start = static_cast<NameEntryStorageTy*>(Storage);
45 NameEntryStorageTy *End = Start + (Name ? 1 : 0);
Pete Cooper79e04c52015-06-09 18:36:13 +000046 return End;
47}
48
Daniel Dunbar8d627d32010-05-05 19:00:56 +000049void MCSymbol::setVariableValue(const MCExpr *Value) {
Rafael Espindoladb9835d2010-11-15 14:40:36 +000050 assert(!IsUsed && "Cannot set a variable that has already been used.");
Daniel Dunbar8d627d32010-05-05 19:00:56 +000051 assert(Value && "Invalid variable value!");
Pete Cooper8b4b91a2015-06-22 19:57:33 +000052 assert((SymbolContents == SymContentsUnset ||
53 SymbolContents == SymContentsVariable) &&
54 "Cannot give common/offset symbol a variable value");
Daniel Dunbar8d627d32010-05-05 19:00:56 +000055 this->Value = Value;
Pete Cooper8b4b91a2015-06-22 19:57:33 +000056 SymbolContents = SymContentsVariable;
Pete Cooper8cdbab92015-06-30 20:54:21 +000057 setUndefined();
Daniel Dunbar8d627d32010-05-05 19:00:56 +000058}
59
Matt Arsenaultd99ce2f2015-06-09 00:31:39 +000060void MCSymbol::print(raw_ostream &OS, const MCAsmInfo *MAI) const {
Chris Lattneracd03ae2010-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 Espindola732cf2d2013-11-14 06:05:49 +000064 StringRef Name = getName();
Matt Arsenaultd99ce2f2015-06-09 00:31:39 +000065 if (!MAI || MAI->isValidUnquotedName(Name)) {
Rafael Espindola732cf2d2013-11-14 06:05:49 +000066 OS << Name;
Chris Lattnere19f9782009-09-13 18:04:46 +000067 return;
68 }
Jim Grosbach2684d9e2012-05-11 01:41:30 +000069
Matt Arsenaultd99ce2f2015-06-09 00:31:39 +000070 if (MAI && !MAI->supportsNameQuoting())
71 report_fatal_error("Symbol name with unsupported characters");
72
Rafael Espindola732cf2d2013-11-14 06:05:49 +000073 OS << '"';
Matt Arsenaultd99ce2f2015-06-09 00:31:39 +000074 for (char C : Name) {
Rafael Espindola732cf2d2013-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 Dunbar1689e0cf2009-08-14 03:41:23 +000083}
84
Aaron Ballman1d03d382017-10-15 14:32:27 +000085#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Matthias Braun88d20752017-01-28 02:02:38 +000086LLVM_DUMP_METHOD void MCSymbol::dump() const {
87 dbgs() << *this;
88}
89#endif