2009-08-14 05:41:23 +02:00
|
|
|
//===- lib/MC/MCSymbol.cpp - MCSymbol implementation ----------------------===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2009-08-14 05:41:23 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-06-06 13:49:48 +02:00
|
|
|
#include "llvm/MC/MCSymbol.h"
|
2017-02-08 23:23:19 +01:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2018-04-30 16:59:11 +02:00
|
|
|
#include "llvm/Config/llvm-config.h"
|
2015-06-09 02:31:39 +02:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
2015-06-09 20:36:13 +02:00
|
|
|
#include "llvm/MC/MCContext.h"
|
2010-05-05 21:00:56 +02:00
|
|
|
#include "llvm/MC/MCExpr.h"
|
2017-02-08 23:23:19 +01:00
|
|
|
#include "llvm/MC/MCFragment.h"
|
|
|
|
#include "llvm/Support/Compiler.h"
|
2010-01-05 02:28:10 +01:00
|
|
|
#include "llvm/Support/Debug.h"
|
2015-06-09 02:31:39 +02:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2009-08-14 05:41:23 +02:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2017-02-08 23:23:19 +01:00
|
|
|
#include <cassert>
|
|
|
|
#include <cstddef>
|
|
|
|
|
2009-08-14 05:41:23 +02:00
|
|
|
using namespace llvm;
|
|
|
|
|
2015-12-29 10:32:18 +01:00
|
|
|
// Only the address of this fragment is ever actually used.
|
|
|
|
static MCDummyFragment SentinelFragment(nullptr);
|
|
|
|
|
2015-10-05 14:07:05 +02:00
|
|
|
// Sentinel value for the absolute pseudo fragment.
|
2015-12-29 10:32:18 +01:00
|
|
|
MCFragment *MCSymbol::AbsolutePseudoFragment = &SentinelFragment;
|
2009-08-22 09:22:36 +02:00
|
|
|
|
2015-06-09 21:56:05 +02:00
|
|
|
void *MCSymbol::operator new(size_t s, const StringMapEntry<bool> *Name,
|
|
|
|
MCContext &Ctx) {
|
|
|
|
// We may need more space for a Name to account for alignment. So allocate
|
|
|
|
// space for the storage type and not the name pointer.
|
|
|
|
size_t Size = s + (Name ? sizeof(NameEntryStorageTy) : 0);
|
2015-06-09 20:36:13 +02:00
|
|
|
|
|
|
|
// For safety, ensure that the alignment of a pointer is enough for an
|
|
|
|
// MCSymbol. This also ensures we don't need padding between the name and
|
|
|
|
// symbol.
|
2016-10-20 17:02:18 +02:00
|
|
|
static_assert((unsigned)alignof(MCSymbol) <= alignof(NameEntryStorageTy),
|
2015-06-09 22:58:03 +02:00
|
|
|
"Bad alignment of MCSymbol");
|
2016-10-20 17:02:18 +02:00
|
|
|
void *Storage = Ctx.allocate(Size, alignof(NameEntryStorageTy));
|
2015-06-09 21:56:05 +02:00
|
|
|
NameEntryStorageTy *Start = static_cast<NameEntryStorageTy*>(Storage);
|
|
|
|
NameEntryStorageTy *End = Start + (Name ? 1 : 0);
|
2015-06-09 20:36:13 +02:00
|
|
|
return End;
|
|
|
|
}
|
|
|
|
|
2010-05-05 21:00:56 +02:00
|
|
|
void MCSymbol::setVariableValue(const MCExpr *Value) {
|
2010-11-15 15:40:36 +01:00
|
|
|
assert(!IsUsed && "Cannot set a variable that has already been used.");
|
2010-05-05 21:00:56 +02:00
|
|
|
assert(Value && "Invalid variable value!");
|
2015-06-22 21:57:33 +02:00
|
|
|
assert((SymbolContents == SymContentsUnset ||
|
|
|
|
SymbolContents == SymContentsVariable) &&
|
|
|
|
"Cannot give common/offset symbol a variable value");
|
2010-05-05 21:00:56 +02:00
|
|
|
this->Value = Value;
|
2015-06-22 21:57:33 +02:00
|
|
|
SymbolContents = SymContentsVariable;
|
2015-06-30 22:54:21 +02:00
|
|
|
setUndefined();
|
2010-05-05 21:00:56 +02:00
|
|
|
}
|
|
|
|
|
2015-06-09 02:31:39 +02:00
|
|
|
void MCSymbol::print(raw_ostream &OS, const MCAsmInfo *MAI) const {
|
2010-01-17 20:23:46 +01:00
|
|
|
// The name for this MCSymbol is required to be a valid target name. However,
|
|
|
|
// some targets support quoting names with funny characters. If the name
|
|
|
|
// contains a funny character, then print it quoted.
|
2013-11-14 07:05:49 +01:00
|
|
|
StringRef Name = getName();
|
2015-06-09 02:31:39 +02:00
|
|
|
if (!MAI || MAI->isValidUnquotedName(Name)) {
|
2013-11-14 07:05:49 +01:00
|
|
|
OS << Name;
|
2009-09-13 20:04:46 +02:00
|
|
|
return;
|
|
|
|
}
|
2012-05-11 03:41:30 +02:00
|
|
|
|
2015-06-09 02:31:39 +02:00
|
|
|
if (MAI && !MAI->supportsNameQuoting())
|
|
|
|
report_fatal_error("Symbol name with unsupported characters");
|
|
|
|
|
2013-11-14 07:05:49 +01:00
|
|
|
OS << '"';
|
2015-06-09 02:31:39 +02:00
|
|
|
for (char C : Name) {
|
2013-11-14 07:05:49 +01:00
|
|
|
if (C == '\n')
|
|
|
|
OS << "\\n";
|
|
|
|
else if (C == '"')
|
|
|
|
OS << "\\\"";
|
|
|
|
else
|
|
|
|
OS << C;
|
|
|
|
}
|
|
|
|
OS << '"';
|
2009-08-14 05:41:23 +02:00
|
|
|
}
|
|
|
|
|
2017-10-15 16:32:27 +02:00
|
|
|
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
|
2017-01-28 03:02:38 +01:00
|
|
|
LLVM_DUMP_METHOD void MCSymbol::dump() const {
|
|
|
|
dbgs() << *this;
|
|
|
|
}
|
|
|
|
#endif
|