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/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 /// If a symbol has a Fragment, the section is implied, so we only need
56 /// FIXME: We might be able to simplify this by having the asm streamer create
58 /// If this is a section, then it gives the symbol is defined in. This is null
59 /// for undefined symbols, and the special AbsolutePseudoSection value for
60 /// absolute symbols. If this is a variable symbol, this caches the variable
63 /// If this is a fragment, then it gives the fragment this symbol's value is
64 /// relative to, if any.
65 mutable PointerUnion<MCSection *, MCFragment *> SectionOrFragment;
67 /// Value - If non-null, the value for a variable symbol.
70 /// IsTemporary - True if this is an assembler temporary label, which
71 /// typically does not survive in the .o file's symbol table. Usually
73 unsigned IsTemporary : 1;
75 /// \brief True if this symbol can be redefined.
76 unsigned IsRedefinable : 1;
78 /// IsUsed - True if this symbol has been used.
79 mutable unsigned IsUsed : 1;
81 mutable bool IsRegistered : 1;
83 /// This symbol is visible outside this translation unit.
84 mutable unsigned IsExternal : 1;
86 /// This symbol is private extern.
87 mutable unsigned IsPrivateExtern : 1;
89 /// True if this symbol is named.
90 /// A named symbol will have a pointer to the name allocated in the bytes
91 /// immediately prior to the MCSymbol.
94 /// LLVM RTTI discriminator. This is actually a SymbolKind enumerator, but is
95 /// unsigned to avoid sign extension and achieve better bitpacking with MSVC.
98 /// Index field, for use by the object file implementation.
99 mutable uint32_t Index = 0;
102 /// The offset to apply to the fragment address to form this symbol's value.
105 /// The size of the symbol, if it is 'common'.
109 /// The alignment of the symbol, if it is 'common', or -1.
111 // FIXME: Pack this in with other fields?
112 unsigned CommonAlign = -1U;
114 /// The Flags field is used by object file implementations to store
115 /// additional per symbol information which is not easily classified.
116 mutable uint32_t Flags = 0;
118 protected: // MCContext creates and uniques these.
120 friend class MCContext;
122 /// \brief The name for a symbol.
123 /// MCSymbol contains a uint64_t so is probably aligned to 8. On a 32-bit
124 /// system, the name is a pointer so isn't going to satisfy the 8 byte
125 /// alignment of uint64_t. Account for that here.
127 const StringMapEntry<bool> *NameEntry;
128 uint64_t AlignmentPadding;
129 } NameEntryStorageTy;
131 MCSymbol(SymbolKind Kind, const StringMapEntry<bool> *Name, bool isTemporary)
132 : Value(nullptr), IsTemporary(isTemporary),
133 IsRedefinable(false), IsUsed(false), IsRegistered(false),
134 IsExternal(false), IsPrivateExtern(false), HasName(!!Name),
138 getNameEntryPtr() = Name;
141 // Provide custom new/delete as we will only allocate space for a name
143 void *operator new(size_t s, const StringMapEntry<bool> *Name,
148 void operator delete(void *);
149 /// \brief Placement delete - required by std, but never called.
150 void operator delete(void*, unsigned) {
151 llvm_unreachable("Constructor throws?");
153 /// \brief Placement delete - required by std, but never called.
154 void operator delete(void*, unsigned, bool) {
155 llvm_unreachable("Constructor throws?");
158 MCSymbol(const MCSymbol &) = delete;
159 void operator=(const MCSymbol &) = delete;
160 MCSection *getSectionPtr() const {
161 if (MCFragment *F = getFragment())
162 return F->getParent();
163 assert(!SectionOrFragment.is<MCFragment *>() && "Section or null expected");
164 MCSection *Section = SectionOrFragment.dyn_cast<MCSection *>();
165 if (Section || !Value)
167 return Section = Value->findAssociatedSection();
170 /// \brief Get a reference to the name field. Requires that we have a name
171 const StringMapEntry<bool> *&getNameEntryPtr() {
172 assert(HasName && "Name is required");
173 NameEntryStorageTy *Name = reinterpret_cast<NameEntryStorageTy *>(this);
174 return (*(Name - 1)).NameEntry;
176 const StringMapEntry<bool> *&getNameEntryPtr() const {
177 return const_cast<MCSymbol*>(this)->getNameEntryPtr();
181 /// getName - Get the symbol name.
182 StringRef getName() const {
186 return getNameEntryPtr()->first();
189 bool isRegistered() const { return IsRegistered; }
190 void setIsRegistered(bool Value) const { IsRegistered = Value; }
195 /// isTemporary - Check if this is an assembler temporary symbol.
196 bool isTemporary() const { return IsTemporary; }
198 /// isUsed - Check if this is used.
199 bool isUsed() const { return IsUsed; }
200 void setUsed(bool Value) const { IsUsed = Value; }
202 /// \brief Check if this symbol is redefinable.
203 bool isRedefinable() const { return IsRedefinable; }
204 /// \brief Mark this symbol as redefinable.
205 void setRedefinable(bool Value) { IsRedefinable = Value; }
206 /// \brief Prepare this symbol to be redefined.
207 void redefineIfPossible() {
210 SectionOrFragment = nullptr;
211 IsRedefinable = false;
216 /// \name Associated Sections
219 /// isDefined - Check if this symbol is defined (i.e., it has an address).
221 /// Defined symbols are either absolute or in some section.
222 bool isDefined() const { return getSectionPtr() != nullptr; }
224 /// isInSection - Check if this symbol is defined in some section (i.e., it
225 /// is defined but not absolute).
226 bool isInSection() const { return isDefined() && !isAbsolute(); }
228 /// isUndefined - Check if this symbol undefined (i.e., implicitly defined).
229 bool isUndefined() const { return !isDefined(); }
231 /// isAbsolute - Check if this is an absolute symbol.
232 bool isAbsolute() const { return getSectionPtr() == AbsolutePseudoSection; }
234 /// Get the section associated with a defined, non-absolute symbol.
235 MCSection &getSection() const {
236 assert(isInSection() && "Invalid accessor!");
237 return *getSectionPtr();
240 /// Mark the symbol as defined in the section \p S.
241 void setSection(MCSection &S) {
242 assert(!isVariable() && "Cannot set section of variable");
243 assert(!SectionOrFragment.is<MCFragment *>() && "Section or null expected");
244 SectionOrFragment = &S;
247 /// Mark the symbol as undefined.
248 void setUndefined() {
249 SectionOrFragment = nullptr;
252 bool isELF() const { return Kind == SymbolKindELF; }
254 bool isCOFF() const { return Kind == SymbolKindCOFF; }
256 bool isMachO() const { return Kind == SymbolKindMachO; }
259 /// \name Variable Symbols
262 /// isVariable - Check if this is a variable symbol.
263 bool isVariable() const { return Value != nullptr; }
265 /// getVariableValue() - Get the value for variable symbols.
266 const MCExpr *getVariableValue() const {
267 assert(isVariable() && "Invalid accessor!");
272 void setVariableValue(const MCExpr *Value);
276 /// Get the (implementation defined) index.
277 uint32_t getIndex() const {
281 /// Set the (implementation defined) index.
282 void setIndex(uint32_t Value) const {
286 uint64_t getOffset() const {
290 void setOffset(uint64_t Value) {
295 /// Return the size of a 'common' symbol.
296 uint64_t getCommonSize() const {
297 assert(isCommon() && "Not a 'common' symbol!");
301 /// Mark this symbol as being 'common'.
303 /// \param Size - The size of the symbol.
304 /// \param Align - The alignment of the symbol.
305 void setCommon(uint64_t Size, unsigned Align) {
306 assert(getOffset() == 0);
311 /// Return the alignment of a 'common' symbol.
312 unsigned getCommonAlignment() const {
313 assert(isCommon() && "Not a 'common' symbol!");
317 /// Declare this symbol as being 'common'.
319 /// \param Size - The size of the symbol.
320 /// \param Align - The alignment of the symbol.
321 /// \return True if symbol was already declared as a different type
322 bool declareCommon(uint64_t Size, unsigned Align) {
323 assert(isCommon() || getOffset() == 0);
325 if(CommonSize != Size || CommonAlign != Align)
328 setCommon(Size, Align);
332 /// Is this a 'common' symbol.
333 bool isCommon() const { return CommonAlign != -1U; }
335 MCFragment *getFragment() const {
336 return SectionOrFragment.dyn_cast<MCFragment *>();
338 void setFragment(MCFragment *Value) const {
339 SectionOrFragment = Value;
342 bool isExternal() const { return IsExternal; }
343 void setExternal(bool Value) const { IsExternal = Value; }
345 bool isPrivateExtern() const { return IsPrivateExtern; }
346 void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
348 /// print - Print the value to the stream \p OS.
349 void print(raw_ostream &OS, const MCAsmInfo *MAI) const;
351 /// dump - Print the value to stderr.
355 /// Get the (implementation defined) symbol flags.
356 uint32_t getFlags() const { return Flags; }
358 /// Set the (implementation defined) symbol flags.
359 void setFlags(uint32_t Value) const { Flags = Value; }
361 /// Modify the flags via a mask
362 void modifyFlags(uint32_t Value, uint32_t Mask) const {
363 Flags = (Flags & ~Mask) | Value;
367 inline raw_ostream &operator<<(raw_ostream &OS, const MCSymbol &Sym) {
368 Sym.print(OS, nullptr);
371 } // end namespace llvm