Create a MCSymbolELF.

This create a MCSymbolELF class and moves SymbolSize since only ELF
needs a size expression.

This reduces the size of MCSymbol from 56 to 48 bytes.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@238801 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Rafael Espindola 2015-06-02 00:25:12 +00:00
parent 398f175c45
commit f7e0685b9a
18 changed files with 95 additions and 56 deletions

View File

@ -30,6 +30,7 @@ namespace llvm {
class MCExpr;
class MCSection;
class MCSymbol;
class MCSymbolELF;
class MCLabel;
struct MCDwarfFile;
class MCDwarfLoc;
@ -75,7 +76,7 @@ namespace llvm {
/// ELF sections can have a corresponding symbol. This maps one to the
/// other.
DenseMap<const MCSectionELF *, MCSymbol *> SectionSymbols;
DenseMap<const MCSectionELF *, MCSymbolELF *> SectionSymbols;
/// A mapping from a local label number and an instance count to a symbol.
/// For example, in the assembly
@ -205,6 +206,8 @@ namespace llvm {
/// Do automatic reset in destructor
bool AutoReset;
MCSymbol *createSymbolImpl(const StringMapEntry<bool> *Name,
bool IsTemporary);
MCSymbol *CreateSymbol(StringRef Name, bool AlwaysAddSuffix);
MCSymbol *getOrCreateDirectionalLocalSymbol(unsigned LocalLabelVal,
@ -263,7 +266,7 @@ namespace llvm {
/// \param Name - The symbol name, which must be unique across all symbols.
MCSymbol *getOrCreateSymbol(const Twine &Name);
MCSymbol *getOrCreateSectionSymbol(const MCSectionELF &Section);
MCSymbolELF *getOrCreateSectionSymbol(const MCSectionELF &Section);
/// Gets a symbol that will be defined to the final stack offset of a local
/// variable after codegen.

View File

@ -23,7 +23,6 @@ class MCAssembler;
class MCCodeEmitter;
class MCExpr;
class MCInst;
class MCSymbol;
class raw_ostream;
class MCELFStreamer : public MCObjectStreamer {
@ -61,7 +60,7 @@ public:
void EmitCOFFSymbolType(int Type) override;
void EndCOFFSymbolDef() override;
void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) override;
void emitELFSize(MCSymbolELF *Symbol, const MCExpr *Value) override;
void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
unsigned ByteAlignment) override;

View File

@ -34,6 +34,7 @@ class MCInstPrinter;
class MCSection;
class MCStreamer;
class MCSymbol;
class MCSymbolELF;
class MCSymbolRefExpr;
class MCSubtargetInfo;
class StringRef;
@ -450,7 +451,7 @@ public:
///
/// This corresponds to an assembler statement such as:
/// .size symbol, expression
virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value);
virtual void emitELFSize(MCSymbolELF *Symbol, const MCExpr *Value);
/// \brief Emit a Linker Optimization Hint (LOH) directive.
/// \param Args - Arguments of the LOH.

View File

@ -83,13 +83,11 @@ class MCSymbol {
mutable unsigned HasFragment : 1;
unsigned IsELF : 1;
/// Index field, for use by the object file implementation.
mutable uint32_t Index = 0;
/// An expression describing how to calculate the size of a symbol. If a
/// symbol has no size this field will be NULL.
const MCExpr *SymbolSize = nullptr;
union {
/// The offset to apply to the fragment address to form this symbol's value.
uint64_t Offset;
@ -107,16 +105,18 @@ class MCSymbol {
/// additional per symbol information which is not easily classified.
mutable uint32_t Flags = 0;
private: // MCContext creates and uniques these.
protected: // MCContext creates and uniques these.
friend class MCExpr;
friend class MCContext;
MCSymbol(const StringMapEntry<bool> *Name, bool isTemporary)
MCSymbol(bool IsELF, const StringMapEntry<bool> *Name, bool isTemporary)
: Name(Name), Section(nullptr), Value(nullptr), IsTemporary(isTemporary),
IsRedefinable(false), IsUsed(false), IsRegistered(false),
IsExternal(false), IsPrivateExtern(false), HasFragment(false) {
IsExternal(false), IsPrivateExtern(false), HasFragment(false),
IsELF(IsELF) {
Offset = 0;
}
private:
MCSymbol(const MCSymbol &) = delete;
void operator=(const MCSymbol &) = delete;
MCSection *getSectionPtr() const {
@ -197,6 +197,8 @@ public:
Section = nullptr;
}
bool isELF() const { return IsELF; }
/// @}
/// \name Variable Symbols
/// @{
@ -225,10 +227,6 @@ public:
Index = Value;
}
void setSize(const MCExpr *SS) { SymbolSize = SS; }
const MCExpr *getSize() const { return SymbolSize; }
uint64_t getOffset() const {
assert(!isCommon());
return Offset;

View File

@ -0,0 +1,31 @@
//===- MCSymbolELF.h - -----------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_MC_MCSYMBOLELF_H
#define LLVM_MC_MCSYMBOLELF_H
#include "llvm/MC/MCSymbol.h"
namespace llvm {
class MCSymbolELF : public MCSymbol {
/// An expression describing how to calculate the size of a symbol. If a
/// symbol has no size this field will be NULL.
const MCExpr *SymbolSize = nullptr;
public:
MCSymbolELF(const StringMapEntry<bool> *Name, bool isTemporary)
: MCSymbol(true, Name, isTemporary) {}
void setSize(const MCExpr *SS) { SymbolSize = SS; }
const MCExpr *getSize() const { return SymbolSize; }
static bool classof(const MCSymbol *S) { return S->isELF(); }
};
}
#endif

View File

@ -53,7 +53,6 @@ public:
void EmitCOFFSafeSEH(MCSymbol const *Symbol) override;
void EmitCOFFSectionIndex(MCSymbol const *Symbol) override;
void EmitCOFFSecRel32(MCSymbol const *Symbol) override;
void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) override;
void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
unsigned ByteAlignment) override;
void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,

View File

@ -40,7 +40,7 @@
#include "llvm/MC/MCInst.h"
#include "llvm/MC/MCSection.h"
#include "llvm/MC/MCStreamer.h"
#include "llvm/MC/MCSymbol.h"
#include "llvm/MC/MCSymbolELF.h"
#include "llvm/MC/MCValue.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/Format.h"
@ -512,7 +512,8 @@ void AsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
if (MAI->hasDotTypeDotSizeDirective())
// .size foo, 42
OutStreamer->EmitELFSize(GVSym, MCConstantExpr::create(Size, OutContext));
OutStreamer->emitELFSize(cast<MCSymbolELF>(GVSym),
MCConstantExpr::create(Size, OutContext));
OutStreamer->AddBlankLine();
}
@ -904,7 +905,7 @@ void AsmPrinter::EmitFunctionBody() {
MCSymbolRefExpr::create(CurrentFnSymForSize,
OutContext),
OutContext);
OutStreamer->EmitELFSize(CurrentFnSym, SizeExp);
OutStreamer->emitELFSize(cast<MCSymbolELF>(CurrentFnSym), SizeExp);
}
for (const HandlerInfo &HI : Handlers) {

View File

@ -30,7 +30,7 @@
#include "llvm/MC/MCSectionELF.h"
#include "llvm/MC/MCSectionMachO.h"
#include "llvm/MC/MCStreamer.h"
#include "llvm/MC/MCSymbol.h"
#include "llvm/MC/MCSymbolELF.h"
#include "llvm/MC/MCValue.h"
#include "llvm/Support/Dwarf.h"
#include "llvm/Support/ELF.h"
@ -63,7 +63,8 @@ void TargetLoweringObjectFileELF::emitPersonalityValue(MCStreamer &Streamer,
const MCSymbol *Sym) const {
SmallString<64> NameData("DW.ref.");
NameData += Sym->getName();
MCSymbol *Label = getContext().getOrCreateSymbol(NameData);
MCSymbolELF *Label =
cast<MCSymbolELF>(getContext().getOrCreateSymbol(NameData));
Streamer.EmitSymbolAttribute(Label, MCSA_Hidden);
Streamer.EmitSymbolAttribute(Label, MCSA_Weak);
StringRef Prefix = ".data.";
@ -76,7 +77,7 @@ void TargetLoweringObjectFileELF::emitPersonalityValue(MCStreamer &Streamer,
Streamer.EmitValueToAlignment(TM.getDataLayout()->getPointerABIAlignment());
Streamer.EmitSymbolAttribute(Label, MCSA_ELF_TypeObject);
const MCExpr *E = MCConstantExpr::create(Size, getContext());
Streamer.EmitELFSize(Label, E);
Streamer.emitELFSize(Label, E);
Streamer.EmitLabel(Label);
Streamer.EmitSymbolValue(Sym, Size);

View File

@ -27,6 +27,7 @@
#include "llvm/MC/MCFixupKindInfo.h"
#include "llvm/MC/MCObjectWriter.h"
#include "llvm/MC/MCSectionELF.h"
#include "llvm/MC/MCSymbolELF.h"
#include "llvm/MC/MCValue.h"
#include "llvm/MC/StringTableBuilder.h"
#include "llvm/Support/Compression.h"
@ -79,7 +80,7 @@ class ELFObjectWriter : public MCObjectWriter {
/// Helper struct for containing some precomputed information on symbols.
struct ELFSymbolData {
const MCSymbol *Symbol;
const MCSymbolELF *Symbol;
uint32_t SectionIndex;
StringRef Name;
@ -451,7 +452,8 @@ void ELFObjectWriter::writeSymbol(SymbolTableWriter &Writer,
assert((!Symbol.getFragment() ||
(Symbol.getFragment()->getParent() == &Symbol.getSection())) &&
"The symbol's section doesn't match the fragment's symbol");
const MCSymbol *Base = Layout.getBaseSymbol(Symbol);
const MCSymbolELF *Base =
cast_or_null<MCSymbolELF>(Layout.getBaseSymbol(Symbol));
// This has to be in sync with when computeSymbolTable uses SHN_ABS or
// SHN_COMMON.
@ -810,7 +812,7 @@ void ELFObjectWriter::computeSymbolTable(
continue;
ELFSymbolData MSD;
MSD.Symbol = &Symbol;
MSD.Symbol = cast<MCSymbolELF>(&Symbol);
// Undefined symbols are global, but this is the first place we
// are able to set it.

View File

@ -24,7 +24,7 @@
#include "llvm/MC/MCRegisterInfo.h"
#include "llvm/MC/MCSectionCOFF.h"
#include "llvm/MC/MCSectionMachO.h"
#include "llvm/MC/MCSymbol.h"
#include "llvm/MC/MCSymbolELF.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/Format.h"
@ -139,7 +139,7 @@ public:
void EmitCOFFSafeSEH(MCSymbol const *Symbol) override;
void EmitCOFFSectionIndex(MCSymbol const *Symbol) override;
void EmitCOFFSecRel32(MCSymbol const *Symbol) override;
void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) override;
void emitELFSize(MCSymbolELF *Symbol, const MCExpr *Value) override;
void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
unsigned ByteAlignment) override;
@ -502,7 +502,7 @@ void MCAsmStreamer::EmitCOFFSecRel32(MCSymbol const *Symbol) {
EmitEOL();
}
void MCAsmStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
void MCAsmStreamer::emitELFSize(MCSymbolELF *Symbol, const MCExpr *Value) {
assert(MAI->hasDotTypeDotSizeDirective());
OS << "\t.size\t" << *Symbol << ", " << *Value << '\n';
}

View File

@ -20,7 +20,7 @@
#include "llvm/MC/MCSectionELF.h"
#include "llvm/MC/MCSectionMachO.h"
#include "llvm/MC/MCStreamer.h"
#include "llvm/MC/MCSymbol.h"
#include "llvm/MC/MCSymbolELF.h"
#include "llvm/Support/ELF.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/FileSystem.h"
@ -119,8 +119,8 @@ MCSymbol *MCContext::getOrCreateSymbol(const Twine &Name) {
return Sym;
}
MCSymbol *MCContext::getOrCreateSectionSymbol(const MCSectionELF &Section) {
MCSymbol *&Sym = SectionSymbols[&Section];
MCSymbolELF *MCContext::getOrCreateSectionSymbol(const MCSectionELF &Section) {
MCSymbolELF *&Sym = SectionSymbols[&Section];
if (Sym)
return Sym;
@ -128,12 +128,12 @@ MCSymbol *MCContext::getOrCreateSectionSymbol(const MCSectionELF &Section) {
MCSymbol *&OldSym = Symbols[Name];
if (OldSym && OldSym->isUndefined()) {
Sym = OldSym;
return OldSym;
Sym = cast<MCSymbolELF>(OldSym);
return Sym;
}
auto NameIter = UsedNames.insert(std::make_pair(Name, true)).first;
Sym = new (*this) MCSymbol(&*NameIter, /*isTemporary*/ false);
Sym = new (*this) MCSymbolELF(&*NameIter, /*isTemporary*/ false);
if (!OldSym)
OldSym = Sym;
@ -157,6 +157,14 @@ MCSymbol *MCContext::getOrCreateLSDASymbol(StringRef FuncName) {
FuncName);
}
MCSymbol *MCContext::createSymbolImpl(const StringMapEntry<bool> *Name,
bool IsTemporary) {
bool IsELF = MOFI && MOFI->getObjectFileType() == MCObjectFileInfo::IsELF;
if (IsELF)
return new (*this) MCSymbolELF(Name, IsTemporary);
return new (*this) MCSymbol(false, Name, IsTemporary);
}
MCSymbol *MCContext::CreateSymbol(StringRef Name, bool AlwaysAddSuffix) {
// Determine whether this is an assembler temporary or normal label, if used.
bool IsTemporary = false;
@ -164,7 +172,7 @@ MCSymbol *MCContext::CreateSymbol(StringRef Name, bool AlwaysAddSuffix) {
IsTemporary = Name.startswith(MAI->getPrivateGlobalPrefix());
if (IsTemporary && AlwaysAddSuffix && !UseNamesOnTempLabels)
return new (*this) MCSymbol(nullptr, true);
return createSymbolImpl(nullptr, true);
SmallString<128> NewName = Name;
bool AddSuffix = AlwaysAddSuffix;
@ -178,8 +186,7 @@ MCSymbol *MCContext::CreateSymbol(StringRef Name, bool AlwaysAddSuffix) {
if (NameEntry.second) {
// Ok, we found a name. Have the MCSymbol object itself refer to the copy
// of the string that is embedded in the UsedNames entry.
MCSymbol *Result = new (*this) MCSymbol(&*NameEntry.first, IsTemporary);
return Result;
return createSymbolImpl(&*NameEntry.first, IsTemporary);
}
assert(IsTemporary && "Cannot rename non-temporary symbols");
AddSuffix = true;

View File

@ -28,6 +28,7 @@
#include "llvm/MC/MCObjectStreamer.h"
#include "llvm/MC/MCSection.h"
#include "llvm/MC/MCSectionELF.h"
#include "llvm/MC/MCSymbolELF.h"
#include "llvm/MC/MCSymbol.h"
#include "llvm/MC/MCValue.h"
#include "llvm/Support/Debug.h"
@ -333,10 +334,11 @@ void MCELFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
Symbol->setCommon(Size, ByteAlignment);
}
Symbol->setSize(MCConstantExpr::create(Size, getContext()));
cast<MCSymbolELF>(Symbol)
->setSize(MCConstantExpr::create(Size, getContext()));
}
void MCELFStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
void MCELFStreamer::emitELFSize(MCSymbolELF *Symbol, const MCExpr *Value) {
Symbol->setSize(Value);
}

View File

@ -93,9 +93,6 @@ public:
void EndCOFFSymbolDef() override {
llvm_unreachable("macho doesn't support this directive");
}
void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) override {
llvm_unreachable("macho doesn't support this directive");
}
void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
unsigned ByteAlignment) override;
void EmitZerofill(MCSection *Section, MCSymbol *Symbol = nullptr,

View File

@ -16,7 +16,7 @@
#include "llvm/MC/MCParser/MCAsmLexer.h"
#include "llvm/MC/MCSectionELF.h"
#include "llvm/MC/MCStreamer.h"
#include "llvm/MC/MCSymbol.h"
#include "llvm/MC/MCSymbolELF.h"
#include "llvm/Support/ELF.h"
using namespace llvm;
@ -209,7 +209,7 @@ bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
StringRef Name;
if (getParser().parseIdentifier(Name))
return TokError("expected identifier in directive");
MCSymbol *Sym = getContext().getOrCreateSymbol(Name);
MCSymbolELF *Sym = cast<MCSymbolELF>(getContext().getOrCreateSymbol(Name));
if (getLexer().isNot(AsmToken::Comma))
return TokError("unexpected token in directive");
@ -222,7 +222,7 @@ bool ELFAsmParser::ParseDirectiveSize(StringRef, SMLoc) {
if (getLexer().isNot(AsmToken::EndOfStatement))
return TokError("unexpected token in directive");
getStreamer().EmitELFSize(Sym, Expr);
getStreamer().emitELFSize(Sym, Expr);
return false;
}

View File

@ -646,7 +646,7 @@ void MCStreamer::EndCOFFSymbolDef() {}
void MCStreamer::EmitFileDirective(StringRef Filename) {}
void MCStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {}
void MCStreamer::EmitCOFFSymbolType(int Type) {}
void MCStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {}
void MCStreamer::emitELFSize(MCSymbolELF *Symbol, const MCExpr *Value) {}
void MCStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
unsigned ByteAlignment) {}
void MCStreamer::EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,

View File

@ -195,10 +195,6 @@ void MCWinCOFFStreamer::EmitCOFFSecRel32(MCSymbol const *Symbol) {
DF->getContents().resize(DF->getContents().size() + 4, 0);
}
void MCWinCOFFStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
llvm_unreachable("not supported");
}
void MCWinCOFFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
unsigned ByteAlignment) {
assert((!Symbol->isInSection() ||

View File

@ -41,7 +41,7 @@
#include "llvm/MC/MCInst.h"
#include "llvm/MC/MCSection.h"
#include "llvm/MC/MCSectionELF.h"
#include "llvm/MC/MCSymbol.h"
#include "llvm/MC/MCSymbolELF.h"
#include "llvm/Support/ELF.h"
#include "llvm/Support/TargetRegistry.h"
#include "llvm/Support/raw_ostream.h"
@ -983,7 +983,8 @@ void MipsAsmPrinter::EmitFPCallStub(
// __call_stub_fp_xxxx:
//
std::string x = "__call_stub_fp_" + std::string(Symbol);
MCSymbol *Stub = OutContext.getOrCreateSymbol(StringRef(x));
MCSymbolELF *Stub =
cast<MCSymbolELF>(OutContext.getOrCreateSymbol(StringRef(x)));
TS.emitDirectiveEnt(*Stub);
MCSymbol *MType =
OutContext.getOrCreateSymbol("__call_stub_fp_" + Twine(Symbol));
@ -1031,7 +1032,7 @@ void MipsAsmPrinter::EmitFPCallStub(
const MCSymbolRefExpr *E = MCSymbolRefExpr::create(Stub, OutContext);
const MCSymbolRefExpr *T = MCSymbolRefExpr::create(Tmp, OutContext);
const MCExpr *T_min_E = MCBinaryExpr::createSub(T, E, OutContext);
OutStreamer->EmitELFSize(Stub, T_min_E);
OutStreamer->emitELFSize(Stub, T_min_E);
TS.emitDirectiveEnd(x);
OutStreamer->PopSection();
}

View File

@ -37,7 +37,7 @@
#include "llvm/MC/MCExpr.h"
#include "llvm/MC/MCInst.h"
#include "llvm/MC/MCStreamer.h"
#include "llvm/MC/MCSymbol.h"
#include "llvm/MC/MCSymbolELF.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/TargetRegistry.h"
#include "llvm/Support/raw_ostream.h"
@ -157,7 +157,8 @@ void XCoreAsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
unsigned Size = TD->getTypeAllocSize(C->getType());
if (MAI->hasDotTypeDotSizeDirective()) {
OutStreamer->EmitSymbolAttribute(GVSym, MCSA_ELF_TypeObject);
OutStreamer->EmitELFSize(GVSym, MCConstantExpr::create(Size, OutContext));
OutStreamer->emitELFSize(cast<MCSymbolELF>(GVSym),
MCConstantExpr::create(Size, OutContext));
}
OutStreamer->EmitLabel(GVSym);