1 //===- MCSymbol.h - Machine Code Symbols ------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the declaration of the MCSymbol class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_MC_MCSYMBOL_H
15 #define LLVM_MC_MCSYMBOL_H
17 #include "llvm/ADT/PointerUnion.h"
18 #include "llvm/ADT/StringMap.h"
19 #include "llvm/MC/MCAssembler.h"
20 #include "llvm/MC/MCExpr.h"
21 #include "llvm/Support/Compiler.h"
31 /// MCSymbol - Instances of this class represent a symbol name in the MC file,
32 /// and MCSymbols are created and uniqued by the MCContext class. MCSymbols
33 /// should only be constructed with valid names for the object file.
35 /// If the symbol is defined/emitted into the current translation unit, the
36 /// Section member is set to indicate what section it lives in. Otherwise, if
37 /// it is a reference to an external entity, it has a null section.
40 /// The kind of the symbol. If it is any value other than unset then this
41 /// class is actually one of the appropriate subclasses of MCSymbol.
49 // Special sentinal value for the absolute pseudo section.
51 // FIXME: Use a PointerInt wrapper for this?
52 static MCSection *AbsolutePseudoSection;
54 /// Name - The name of the symbol. The referred-to string data is actually
55 /// held by the StringMap that lives in MCContext.
56 const StringMapEntry<bool> *Name;
58 /// If a symbol has a Fragment, the section is implied, so we only need
60 /// FIXME: We might be able to simplify this by having the asm streamer create
62 /// If this is a section, then it gives the symbol is defined in. This is null
63 /// for undefined symbols, and the special AbsolutePseudoSection value for
64 /// absolute symbols. If this is a variable symbol, this caches the variable
67 /// If this is a fragment, then it gives the fragment this symbol's value is
68 /// relative to, if any.
69 mutable PointerUnion<MCSection *, MCFragment *> SectionOrFragment;
71 /// Value - If non-null, the value for a variable symbol.
74 /// IsTemporary - True if this is an assembler temporary label, which
75 /// typically does not survive in the .o file's symbol table. Usually
77 unsigned IsTemporary : 1;
79 /// \brief True if this symbol can be redefined.
80 unsigned IsRedefinable : 1;
82 /// IsUsed - True if this symbol has been used.
83 mutable unsigned IsUsed : 1;
85 mutable bool IsRegistered : 1;
87 /// This symbol is visible outside this translation unit.
88 mutable unsigned IsExternal : 1;
90 /// This symbol is private extern.
91 mutable unsigned IsPrivateExtern : 1;
95 /// Index field, for use by the object file implementation.
96 mutable uint32_t Index = 0;
99 /// The offset to apply to the fragment address to form this symbol's value.
102 /// The size of the symbol, if it is 'common'.
106 /// The alignment of the symbol, if it is 'common', or -1.
108 // FIXME: Pack this in with other fields?
109 unsigned CommonAlign = -1U;
111 /// The Flags field is used by object file implementations to store
112 /// additional per symbol information which is not easily classified.
113 mutable uint32_t Flags = 0;
115 protected: // MCContext creates and uniques these.
117 friend class MCContext;
118 MCSymbol(SymbolKind Kind, const StringMapEntry<bool> *Name, bool isTemporary)
119 : Name(Name), Value(nullptr), IsTemporary(isTemporary),
120 IsRedefinable(false), IsUsed(false), IsRegistered(false),
121 IsExternal(false), IsPrivateExtern(false),
127 MCSymbol(const MCSymbol &) = delete;
128 void operator=(const MCSymbol &) = delete;
129 MCSection *getSectionPtr() const {
130 if (MCFragment *F = getFragment())
131 return F->getParent();
132 assert(!SectionOrFragment.is<MCFragment *>() && "Section or null expected");
133 MCSection *Section = SectionOrFragment.dyn_cast<MCSection *>();
134 if (Section || !Value)
136 return Section = Value->findAssociatedSection();
140 /// getName - Get the symbol name.
141 StringRef getName() const { return Name ? Name->first() : ""; }
143 bool isRegistered() const { return IsRegistered; }
144 void setIsRegistered(bool Value) const { IsRegistered = Value; }
149 /// isTemporary - Check if this is an assembler temporary symbol.
150 bool isTemporary() const { return IsTemporary; }
152 /// isUsed - Check if this is used.
153 bool isUsed() const { return IsUsed; }
154 void setUsed(bool Value) const { IsUsed = Value; }
156 /// \brief Check if this symbol is redefinable.
157 bool isRedefinable() const { return IsRedefinable; }
158 /// \brief Mark this symbol as redefinable.
159 void setRedefinable(bool Value) { IsRedefinable = Value; }
160 /// \brief Prepare this symbol to be redefined.
161 void redefineIfPossible() {
164 SectionOrFragment = nullptr;
165 IsRedefinable = false;
170 /// \name Associated Sections
173 /// isDefined - Check if this symbol is defined (i.e., it has an address).
175 /// Defined symbols are either absolute or in some section.
176 bool isDefined() const { return getSectionPtr() != nullptr; }
178 /// isInSection - Check if this symbol is defined in some section (i.e., it
179 /// is defined but not absolute).
180 bool isInSection() const { return isDefined() && !isAbsolute(); }
182 /// isUndefined - Check if this symbol undefined (i.e., implicitly defined).
183 bool isUndefined() const { return !isDefined(); }
185 /// isAbsolute - Check if this is an absolute symbol.
186 bool isAbsolute() const { return getSectionPtr() == AbsolutePseudoSection; }
188 /// Get the section associated with a defined, non-absolute symbol.
189 MCSection &getSection() const {
190 assert(isInSection() && "Invalid accessor!");
191 return *getSectionPtr();
194 /// Mark the symbol as defined in the section \p S.
195 void setSection(MCSection &S) {
196 assert(!isVariable() && "Cannot set section of variable");
197 assert(!SectionOrFragment.is<MCFragment *>() && "Section or null expected");
198 SectionOrFragment = &S;
201 /// Mark the symbol as undefined.
202 void setUndefined() {
203 SectionOrFragment = nullptr;
206 bool isELF() const { return Kind == SymbolKindELF; }
208 bool isCOFF() const { return Kind == SymbolKindCOFF; }
210 bool isMachO() const { return Kind == SymbolKindMachO; }
213 /// \name Variable Symbols
216 /// isVariable - Check if this is a variable symbol.
217 bool isVariable() const { return Value != nullptr; }
219 /// getVariableValue() - Get the value for variable symbols.
220 const MCExpr *getVariableValue() const {
221 assert(isVariable() && "Invalid accessor!");
226 void setVariableValue(const MCExpr *Value);
230 /// Get the (implementation defined) index.
231 uint32_t getIndex() const {
235 /// Set the (implementation defined) index.
236 void setIndex(uint32_t Value) const {
240 uint64_t getOffset() const {
244 void setOffset(uint64_t Value) {
249 /// Return the size of a 'common' symbol.
250 uint64_t getCommonSize() const {
251 assert(isCommon() && "Not a 'common' symbol!");
255 /// Mark this symbol as being 'common'.
257 /// \param Size - The size of the symbol.
258 /// \param Align - The alignment of the symbol.
259 void setCommon(uint64_t Size, unsigned Align) {
260 assert(getOffset() == 0);
265 /// Return the alignment of a 'common' symbol.
266 unsigned getCommonAlignment() const {
267 assert(isCommon() && "Not a 'common' symbol!");
271 /// Declare this symbol as being 'common'.
273 /// \param Size - The size of the symbol.
274 /// \param Align - The alignment of the symbol.
275 /// \return True if symbol was already declared as a different type
276 bool declareCommon(uint64_t Size, unsigned Align) {
277 assert(isCommon() || getOffset() == 0);
279 if(CommonSize != Size || CommonAlign != Align)
282 setCommon(Size, Align);
286 /// Is this a 'common' symbol.
287 bool isCommon() const { return CommonAlign != -1U; }
289 MCFragment *getFragment() const {
290 return SectionOrFragment.dyn_cast<MCFragment *>();
292 void setFragment(MCFragment *Value) const {
293 SectionOrFragment = Value;
296 bool isExternal() const { return IsExternal; }
297 void setExternal(bool Value) const { IsExternal = Value; }
299 bool isPrivateExtern() const { return IsPrivateExtern; }
300 void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
302 /// print - Print the value to the stream \p OS.
303 void print(raw_ostream &OS) const;
305 /// dump - Print the value to stderr.
309 /// Get the (implementation defined) symbol flags.
310 uint32_t getFlags() const { return Flags; }
312 /// Set the (implementation defined) symbol flags.
313 void setFlags(uint32_t Value) const { Flags = Value; }
315 /// Modify the flags via a mask
316 void modifyFlags(uint32_t Value, uint32_t Mask) const {
317 Flags = (Flags & ~Mask) | Value;
321 inline raw_ostream &operator<<(raw_ostream &OS, const MCSymbol &Sym) {
325 } // end namespace llvm