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;
93 /// LLVM RTTI discriminator. This is actually a SymbolKind enumerator, but is
94 /// unsigned to avoid sign extension and achieve better bitpacking with MSVC.
97 /// Index field, for use by the object file implementation.
98 mutable uint32_t Index = 0;
101 /// The offset to apply to the fragment address to form this symbol's value.
104 /// The size of the symbol, if it is 'common'.
108 /// The alignment of the symbol, if it is 'common', or -1.
110 // FIXME: Pack this in with other fields?
111 unsigned CommonAlign = -1U;
113 /// The Flags field is used by object file implementations to store
114 /// additional per symbol information which is not easily classified.
115 mutable uint32_t Flags = 0;
117 protected: // MCContext creates and uniques these.
119 friend class MCContext;
120 MCSymbol(SymbolKind Kind, const StringMapEntry<bool> *Name, bool isTemporary)
121 : Name(Name), Value(nullptr), IsTemporary(isTemporary),
122 IsRedefinable(false), IsUsed(false), IsRegistered(false),
123 IsExternal(false), IsPrivateExtern(false),
129 MCSymbol(const MCSymbol &) = delete;
130 void operator=(const MCSymbol &) = delete;
131 MCSection *getSectionPtr() const {
132 if (MCFragment *F = getFragment())
133 return F->getParent();
134 assert(!SectionOrFragment.is<MCFragment *>() && "Section or null expected");
135 MCSection *Section = SectionOrFragment.dyn_cast<MCSection *>();
136 if (Section || !Value)
138 return Section = Value->findAssociatedSection();
142 /// getName - Get the symbol name.
143 StringRef getName() const { return Name ? Name->first() : ""; }
145 bool isRegistered() const { return IsRegistered; }
146 void setIsRegistered(bool Value) const { IsRegistered = Value; }
151 /// isTemporary - Check if this is an assembler temporary symbol.
152 bool isTemporary() const { return IsTemporary; }
154 /// isUsed - Check if this is used.
155 bool isUsed() const { return IsUsed; }
156 void setUsed(bool Value) const { IsUsed = Value; }
158 /// \brief Check if this symbol is redefinable.
159 bool isRedefinable() const { return IsRedefinable; }
160 /// \brief Mark this symbol as redefinable.
161 void setRedefinable(bool Value) { IsRedefinable = Value; }
162 /// \brief Prepare this symbol to be redefined.
163 void redefineIfPossible() {
166 SectionOrFragment = nullptr;
167 IsRedefinable = false;
172 /// \name Associated Sections
175 /// isDefined - Check if this symbol is defined (i.e., it has an address).
177 /// Defined symbols are either absolute or in some section.
178 bool isDefined() const { return getSectionPtr() != nullptr; }
180 /// isInSection - Check if this symbol is defined in some section (i.e., it
181 /// is defined but not absolute).
182 bool isInSection() const { return isDefined() && !isAbsolute(); }
184 /// isUndefined - Check if this symbol undefined (i.e., implicitly defined).
185 bool isUndefined() const { return !isDefined(); }
187 /// isAbsolute - Check if this is an absolute symbol.
188 bool isAbsolute() const { return getSectionPtr() == AbsolutePseudoSection; }
190 /// Get the section associated with a defined, non-absolute symbol.
191 MCSection &getSection() const {
192 assert(isInSection() && "Invalid accessor!");
193 return *getSectionPtr();
196 /// Mark the symbol as defined in the section \p S.
197 void setSection(MCSection &S) {
198 assert(!isVariable() && "Cannot set section of variable");
199 assert(!SectionOrFragment.is<MCFragment *>() && "Section or null expected");
200 SectionOrFragment = &S;
203 /// Mark the symbol as undefined.
204 void setUndefined() {
205 SectionOrFragment = nullptr;
208 bool isELF() const { return Kind == SymbolKindELF; }
210 bool isCOFF() const { return Kind == SymbolKindCOFF; }
212 bool isMachO() const { return Kind == SymbolKindMachO; }
215 /// \name Variable Symbols
218 /// isVariable - Check if this is a variable symbol.
219 bool isVariable() const { return Value != nullptr; }
221 /// getVariableValue() - Get the value for variable symbols.
222 const MCExpr *getVariableValue() const {
223 assert(isVariable() && "Invalid accessor!");
228 void setVariableValue(const MCExpr *Value);
232 /// Get the (implementation defined) index.
233 uint32_t getIndex() const {
237 /// Set the (implementation defined) index.
238 void setIndex(uint32_t Value) const {
242 uint64_t getOffset() const {
246 void setOffset(uint64_t Value) {
251 /// Return the size of a 'common' symbol.
252 uint64_t getCommonSize() const {
253 assert(isCommon() && "Not a 'common' symbol!");
257 /// Mark this symbol as being 'common'.
259 /// \param Size - The size of the symbol.
260 /// \param Align - The alignment of the symbol.
261 void setCommon(uint64_t Size, unsigned Align) {
262 assert(getOffset() == 0);
267 /// Return the alignment of a 'common' symbol.
268 unsigned getCommonAlignment() const {
269 assert(isCommon() && "Not a 'common' symbol!");
273 /// Declare this symbol as being 'common'.
275 /// \param Size - The size of the symbol.
276 /// \param Align - The alignment of the symbol.
277 /// \return True if symbol was already declared as a different type
278 bool declareCommon(uint64_t Size, unsigned Align) {
279 assert(isCommon() || getOffset() == 0);
281 if(CommonSize != Size || CommonAlign != Align)
284 setCommon(Size, Align);
288 /// Is this a 'common' symbol.
289 bool isCommon() const { return CommonAlign != -1U; }
291 MCFragment *getFragment() const {
292 return SectionOrFragment.dyn_cast<MCFragment *>();
294 void setFragment(MCFragment *Value) const {
295 SectionOrFragment = Value;
298 bool isExternal() const { return IsExternal; }
299 void setExternal(bool Value) const { IsExternal = Value; }
301 bool isPrivateExtern() const { return IsPrivateExtern; }
302 void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
304 /// print - Print the value to the stream \p OS.
305 void print(raw_ostream &OS) const;
307 /// dump - Print the value to stderr.
311 /// Get the (implementation defined) symbol flags.
312 uint32_t getFlags() const { return Flags; }
314 /// Set the (implementation defined) symbol flags.
315 void setFlags(uint32_t Value) const { Flags = Value; }
317 /// Modify the flags via a mask
318 void modifyFlags(uint32_t Value, uint32_t Mask) const {
319 Flags = (Flags & ~Mask) | Value;
323 inline raw_ostream &operator<<(raw_ostream &OS, const MCSymbol &Sym) {
327 } // end namespace llvm