2009-08-14 11:41:23 +08:00
|
|
|
//===- lib/MC/MCSymbol.cpp - MCSymbol implementation ----------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "llvm/MC/MCSymbol.h"
|
2017-02-09 06:23:19 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2015-06-09 08:31:39 +08:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
2015-06-10 02:36:13 +08:00
|
|
|
#include "llvm/MC/MCContext.h"
|
2010-05-06 03:00:56 +08:00
|
|
|
#include "llvm/MC/MCExpr.h"
|
2017-02-09 06:23:19 +08:00
|
|
|
#include "llvm/MC/MCFragment.h"
|
|
|
|
#include "llvm/Support/Compiler.h"
|
2010-01-05 09:28:10 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2015-06-09 08:31:39 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2009-08-14 11:41:23 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2017-02-09 06:23:19 +08:00
|
|
|
#include <cassert>
|
|
|
|
#include <cstddef>
|
|
|
|
|
2009-08-14 11:41:23 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2015-12-29 17:32:18 +08:00
|
|
|
// Only the address of this fragment is ever actually used.
|
|
|
|
static MCDummyFragment SentinelFragment(nullptr);
|
|
|
|
|
2015-10-05 20:07:05 +08:00
|
|
|
// Sentinel value for the absolute pseudo fragment.
|
2015-12-29 17:32:18 +08:00
|
|
|
MCFragment *MCSymbol::AbsolutePseudoFragment = &SentinelFragment;
|
2009-08-22 15:22:36 +08:00
|
|
|
|
2015-06-10 03:56:05 +08: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-10 02:36:13 +08: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 23:02:18 +08:00
|
|
|
static_assert((unsigned)alignof(MCSymbol) <= alignof(NameEntryStorageTy),
|
2015-06-10 04:58:03 +08:00
|
|
|
"Bad alignment of MCSymbol");
|
2016-10-20 23:02:18 +08:00
|
|
|
void *Storage = Ctx.allocate(Size, alignof(NameEntryStorageTy));
|
2015-06-10 03:56:05 +08:00
|
|
|
NameEntryStorageTy *Start = static_cast<NameEntryStorageTy*>(Storage);
|
|
|
|
NameEntryStorageTy *End = Start + (Name ? 1 : 0);
|
2015-06-10 02:36:13 +08:00
|
|
|
return End;
|
|
|
|
}
|
|
|
|
|
2010-05-06 03:00:56 +08:00
|
|
|
void MCSymbol::setVariableValue(const MCExpr *Value) {
|
2010-11-15 22:40:36 +08:00
|
|
|
assert(!IsUsed && "Cannot set a variable that has already been used.");
|
2010-05-06 03:00:56 +08:00
|
|
|
assert(Value && "Invalid variable value!");
|
2015-06-23 03:57:33 +08:00
|
|
|
assert((SymbolContents == SymContentsUnset ||
|
|
|
|
SymbolContents == SymContentsVariable) &&
|
|
|
|
"Cannot give common/offset symbol a variable value");
|
2010-05-06 03:00:56 +08:00
|
|
|
this->Value = Value;
|
2015-06-23 03:57:33 +08:00
|
|
|
SymbolContents = SymContentsVariable;
|
2015-07-01 04:54:21 +08:00
|
|
|
setUndefined();
|
2010-05-06 03:00:56 +08:00
|
|
|
}
|
|
|
|
|
2015-06-09 08:31:39 +08:00
|
|
|
void MCSymbol::print(raw_ostream &OS, const MCAsmInfo *MAI) const {
|
2010-01-18 03:23:46 +08: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 14:05:49 +08:00
|
|
|
StringRef Name = getName();
|
2015-06-09 08:31:39 +08:00
|
|
|
if (!MAI || MAI->isValidUnquotedName(Name)) {
|
2013-11-14 14:05:49 +08:00
|
|
|
OS << Name;
|
2009-09-14 02:04:46 +08:00
|
|
|
return;
|
|
|
|
}
|
2012-05-11 09:41:30 +08:00
|
|
|
|
2015-06-09 08:31:39 +08:00
|
|
|
if (MAI && !MAI->supportsNameQuoting())
|
|
|
|
report_fatal_error("Symbol name with unsupported characters");
|
|
|
|
|
2013-11-14 14:05:49 +08:00
|
|
|
OS << '"';
|
2015-06-09 08:31:39 +08:00
|
|
|
for (char C : Name) {
|
2013-11-14 14:05:49 +08:00
|
|
|
if (C == '\n')
|
|
|
|
OS << "\\n";
|
|
|
|
else if (C == '"')
|
|
|
|
OS << "\\\"";
|
|
|
|
else
|
|
|
|
OS << C;
|
|
|
|
}
|
|
|
|
OS << '"';
|
2009-08-14 11:41:23 +08:00
|
|
|
}
|
|
|
|
|
2017-10-13 00:16:06 +08:00
|
|
|
#ifdef LLVM_ENABLE_DUMP
|
2017-01-28 10:02:38 +08:00
|
|
|
LLVM_DUMP_METHOD void MCSymbol::dump() const {
|
|
|
|
dbgs() << *this;
|
|
|
|
}
|
|
|
|
#endif
|