blob: 67cab9a92722acd7d4188fe8dc86bafe3d55bde2 [file] [log] [blame]
Daniel Dunbar1a019d82009-08-14 03:41:23 +00001//===- lib/MC/MCSymbol.cpp - MCSymbol implementation ----------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Daniel Dunbar1a019d82009-08-14 03:41:23 +00006//
7//===----------------------------------------------------------------------===//
8
Chandler Carruth6bda14b2017-06-06 11:49:48 +00009#include "llvm/MC/MCSymbol.h"
Eugene Zelenko3d8b0eb2017-02-08 22:23:19 +000010#include "llvm/ADT/StringRef.h"
Nico Weber432a3882018-04-30 14:59:11 +000011#include "llvm/Config/llvm-config.h"
Matt Arsenault8b643552015-06-09 00:31:39 +000012#include "llvm/MC/MCAsmInfo.h"
Pete Cooper234b8752015-06-09 18:36:13 +000013#include "llvm/MC/MCContext.h"
Daniel Dunbar85574262010-05-05 19:00:56 +000014#include "llvm/MC/MCExpr.h"
Eugene Zelenko3d8b0eb2017-02-08 22:23:19 +000015#include "llvm/MC/MCFragment.h"
Eugene Zelenko3d8b0eb2017-02-08 22:23:19 +000016#include "llvm/Support/Compiler.h"
David Greenecdf04522010-01-05 01:28:10 +000017#include "llvm/Support/Debug.h"
Matt Arsenault8b643552015-06-09 00:31:39 +000018#include "llvm/Support/ErrorHandling.h"
Daniel Dunbar1a019d82009-08-14 03:41:23 +000019#include "llvm/Support/raw_ostream.h"
Eugene Zelenko3d8b0eb2017-02-08 22:23:19 +000020#include <cassert>
21#include <cstddef>
22
Daniel Dunbar1a019d82009-08-14 03:41:23 +000023using namespace llvm;
24
Chandler Carruth5bd31b32015-12-29 09:32:18 +000025// Only the address of this fragment is ever actually used.
26static MCDummyFragment SentinelFragment(nullptr);
27
Rafael Espindolae3a20f52015-10-05 12:07:05 +000028// Sentinel value for the absolute pseudo fragment.
Chandler Carruth5bd31b32015-12-29 09:32:18 +000029MCFragment *MCSymbol::AbsolutePseudoFragment = &SentinelFragment;
Daniel Dunbar6860ac72009-08-22 07:22:36 +000030
Pete Coopera9ecddb2015-06-09 19:56:05 +000031void *MCSymbol::operator new(size_t s, const StringMapEntry<bool> *Name,
32 MCContext &Ctx) {
33 // We may need more space for a Name to account for alignment. So allocate
34 // space for the storage type and not the name pointer.
35 size_t Size = s + (Name ? sizeof(NameEntryStorageTy) : 0);
Pete Cooper234b8752015-06-09 18:36:13 +000036
37 // For safety, ensure that the alignment of a pointer is enough for an
38 // MCSymbol. This also ensures we don't need padding between the name and
39 // symbol.
Benjamin Kramerb2505002016-10-20 15:02:18 +000040 static_assert((unsigned)alignof(MCSymbol) <= alignof(NameEntryStorageTy),
Pete Cooper8ae395d2015-06-09 20:58:03 +000041 "Bad alignment of MCSymbol");
Benjamin Kramerb2505002016-10-20 15:02:18 +000042 void *Storage = Ctx.allocate(Size, alignof(NameEntryStorageTy));
Pete Coopera9ecddb2015-06-09 19:56:05 +000043 NameEntryStorageTy *Start = static_cast<NameEntryStorageTy*>(Storage);
44 NameEntryStorageTy *End = Start + (Name ? 1 : 0);
Pete Cooper234b8752015-06-09 18:36:13 +000045 return End;
46}
47
Daniel Dunbar85574262010-05-05 19:00:56 +000048void MCSymbol::setVariableValue(const MCExpr *Value) {
Rafael Espindola46c79ef2010-11-15 14:40:36 +000049 assert(!IsUsed && "Cannot set a variable that has already been used.");
Daniel Dunbar85574262010-05-05 19:00:56 +000050 assert(Value && "Invalid variable value!");
Pete Cooper63b4dc42015-06-22 19:57:33 +000051 assert((SymbolContents == SymContentsUnset ||
52 SymbolContents == SymContentsVariable) &&
53 "Cannot give common/offset symbol a variable value");
Daniel Dunbar85574262010-05-05 19:00:56 +000054 this->Value = Value;
Pete Cooper63b4dc42015-06-22 19:57:33 +000055 SymbolContents = SymContentsVariable;
Pete Cooper2b00f082015-06-30 20:54:21 +000056 setUndefined();
Daniel Dunbar85574262010-05-05 19:00:56 +000057}
58
Matt Arsenault8b643552015-06-09 00:31:39 +000059void MCSymbol::print(raw_ostream &OS, const MCAsmInfo *MAI) const {
Chris Lattner83e872e2010-01-17 19:23:46 +000060 // The name for this MCSymbol is required to be a valid target name. However,
61 // some targets support quoting names with funny characters. If the name
62 // contains a funny character, then print it quoted.
Rafael Espindolafe4e0882013-11-14 06:05:49 +000063 StringRef Name = getName();
Matt Arsenault8b643552015-06-09 00:31:39 +000064 if (!MAI || MAI->isValidUnquotedName(Name)) {
Rafael Espindolafe4e0882013-11-14 06:05:49 +000065 OS << Name;
Chris Lattner3d6c8eb2009-09-13 18:04:46 +000066 return;
67 }
Jim Grosbachdc1e36e2012-05-11 01:41:30 +000068
Matt Arsenault8b643552015-06-09 00:31:39 +000069 if (MAI && !MAI->supportsNameQuoting())
70 report_fatal_error("Symbol name with unsupported characters");
71
Rafael Espindolafe4e0882013-11-14 06:05:49 +000072 OS << '"';
Matt Arsenault8b643552015-06-09 00:31:39 +000073 for (char C : Name) {
Rafael Espindolafe4e0882013-11-14 06:05:49 +000074 if (C == '\n')
75 OS << "\\n";
76 else if (C == '"')
77 OS << "\\\"";
78 else
79 OS << C;
80 }
81 OS << '"';
Daniel Dunbar1a019d82009-08-14 03:41:23 +000082}
83
Aaron Ballman615eb472017-10-15 14:32:27 +000084#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Matthias Braun8c209aa2017-01-28 02:02:38 +000085LLVM_DUMP_METHOD void MCSymbol::dump() const {
86 dbgs() << *this;
87}
88#endif