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/PointerIntPair.h"
18 #include "llvm/ADT/PointerUnion.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/MC/MCAssembler.h"
21 #include "llvm/Support/Compiler.h"
32 /// MCSymbol - Instances of this class represent a symbol name in the MC file,
33 /// and MCSymbols are created and uniqued by the MCContext class. MCSymbols
34 /// should only be constructed with valid names for the object file.
36 /// If the symbol is defined/emitted into the current translation unit, the
37 /// Section member is set to indicate what section it lives in. Otherwise, if
38 /// it is a reference to an external entity, it has a null section.
41 /// The kind of the symbol. If it is any value other than unset then this
42 /// class is actually one of the appropriate subclasses of MCSymbol.
50 /// A symbol can contain an Offset, or Value, or be Common, but never more
51 /// than one of these.
52 enum Contents : uint8_t {
59 // Special sentinal value for the absolute pseudo fragment.
60 static MCFragment *AbsolutePseudoFragment;
62 /// If a symbol has a Fragment, the section is implied, so we only need
64 /// The special AbsolutePseudoFragment value is for absolute symbols.
65 /// If this is a variable symbol, this caches the variable value's fragment.
66 /// FIXME: We might be able to simplify this by having the asm streamer create
68 /// If this is a section, then it gives the symbol is defined in. This is null
69 /// for undefined symbols.
71 /// If this is a fragment, then it gives the fragment this symbol's value is
72 /// relative to, if any.
74 /// For the 'HasName' integer, this is true if this symbol is named.
75 /// A named symbol will have a pointer to the name allocated in the bytes
76 /// immediately prior to the MCSymbol.
77 mutable PointerIntPair<MCFragment *, 1> FragmentAndHasName;
79 /// IsTemporary - True if this is an assembler temporary label, which
80 /// typically does not survive in the .o file's symbol table. Usually
82 unsigned IsTemporary : 1;
84 /// \brief True if this symbol can be redefined.
85 unsigned IsRedefinable : 1;
87 /// IsUsed - True if this symbol has been used.
88 mutable unsigned IsUsed : 1;
90 mutable bool IsRegistered : 1;
92 /// This symbol is visible outside this translation unit.
93 mutable unsigned IsExternal : 1;
95 /// This symbol is private extern.
96 mutable unsigned IsPrivateExtern : 1;
98 /// LLVM RTTI discriminator. This is actually a SymbolKind enumerator, but is
99 /// unsigned to avoid sign extension and achieve better bitpacking with MSVC.
102 /// True if we have created a relocation that uses this symbol.
103 mutable unsigned IsUsedInReloc : 1;
105 /// This is actually a Contents enumerator, but is unsigned to avoid sign
106 /// extension and achieve better bitpacking with MSVC.
107 unsigned SymbolContents : 2;
109 /// The alignment of the symbol, if it is 'common', or -1.
111 /// The alignment is stored as log2(align) + 1. This allows all values from
112 /// 0 to 2^31 to be stored which is every power of 2 representable by an
114 enum : unsigned { NumCommonAlignmentBits = 5 };
115 unsigned CommonAlignLog2 : NumCommonAlignmentBits;
117 /// The Flags field is used by object file implementations to store
118 /// additional per symbol information which is not easily classified.
119 enum : unsigned { NumFlagsBits = 16 };
120 mutable uint32_t Flags : NumFlagsBits;
122 /// Index field, for use by the object file implementation.
123 mutable uint32_t Index = 0;
126 /// The offset to apply to the fragment address to form this symbol's value.
129 /// The size of the symbol, if it is 'common'.
132 /// If non-null, the value for a variable symbol.
136 protected: // MCContext creates and uniques these.
138 friend class MCContext;
140 /// \brief The name for a symbol.
141 /// MCSymbol contains a uint64_t so is probably aligned to 8. On a 32-bit
142 /// system, the name is a pointer so isn't going to satisfy the 8 byte
143 /// alignment of uint64_t. Account for that here.
145 const StringMapEntry<bool> *NameEntry;
146 uint64_t AlignmentPadding;
147 } NameEntryStorageTy;
149 MCSymbol(SymbolKind Kind, const StringMapEntry<bool> *Name, bool isTemporary)
150 : IsTemporary(isTemporary), IsRedefinable(false), IsUsed(false),
151 IsRegistered(false), IsExternal(false), IsPrivateExtern(false),
152 Kind(Kind), IsUsedInReloc(false), SymbolContents(SymContentsUnset),
153 CommonAlignLog2(0), Flags(0) {
155 FragmentAndHasName.setInt(!!Name);
157 getNameEntryPtr() = Name;
160 // Provide custom new/delete as we will only allocate space for a name
162 void *operator new(size_t s, const StringMapEntry<bool> *Name,
167 void operator delete(void *);
168 /// \brief Placement delete - required by std, but never called.
169 void operator delete(void*, unsigned) {
170 llvm_unreachable("Constructor throws?");
172 /// \brief Placement delete - required by std, but never called.
173 void operator delete(void*, unsigned, bool) {
174 llvm_unreachable("Constructor throws?");
177 MCSymbol(const MCSymbol &) = delete;
178 void operator=(const MCSymbol &) = delete;
179 MCSection *getSectionPtr(bool SetUsed = true) const {
180 if (MCFragment *F = getFragment(SetUsed)) {
181 assert(F != AbsolutePseudoFragment);
182 return F->getParent();
187 /// \brief Get a reference to the name field. Requires that we have a name
188 const StringMapEntry<bool> *&getNameEntryPtr() {
189 assert(FragmentAndHasName.getInt() && "Name is required");
190 NameEntryStorageTy *Name = reinterpret_cast<NameEntryStorageTy *>(this);
191 return (*(Name - 1)).NameEntry;
193 const StringMapEntry<bool> *&getNameEntryPtr() const {
194 return const_cast<MCSymbol*>(this)->getNameEntryPtr();
198 /// getName - Get the symbol name.
199 StringRef getName() const {
200 if (!FragmentAndHasName.getInt())
203 return getNameEntryPtr()->first();
206 bool isRegistered() const { return IsRegistered; }
207 void setIsRegistered(bool Value) const { IsRegistered = Value; }
209 void setUsedInReloc() const { IsUsedInReloc = true; }
210 bool isUsedInReloc() const { return IsUsedInReloc; }
215 /// isTemporary - Check if this is an assembler temporary symbol.
216 bool isTemporary() const { return IsTemporary; }
218 /// isUsed - Check if this is used.
219 bool isUsed() const { return IsUsed; }
220 void setUsed(bool Value) const { IsUsed |= Value; }
222 /// \brief Check if this symbol is redefinable.
223 bool isRedefinable() const { return IsRedefinable; }
224 /// \brief Mark this symbol as redefinable.
225 void setRedefinable(bool Value) { IsRedefinable = Value; }
226 /// \brief Prepare this symbol to be redefined.
227 void redefineIfPossible() {
229 if (SymbolContents == SymContentsVariable) {
231 SymbolContents = SymContentsUnset;
234 IsRedefinable = false;
239 /// \name Associated Sections
242 /// isDefined - Check if this symbol is defined (i.e., it has an address).
244 /// Defined symbols are either absolute or in some section.
245 bool isDefined(bool SetUsed = true) const {
246 return getFragment(SetUsed) != nullptr;
249 /// isInSection - Check if this symbol is defined in some section (i.e., it
250 /// is defined but not absolute).
251 bool isInSection(bool SetUsed = true) const {
252 return isDefined(SetUsed) && !isAbsolute(SetUsed);
255 /// isUndefined - Check if this symbol undefined (i.e., implicitly defined).
256 bool isUndefined(bool SetUsed = true) const { return !isDefined(SetUsed); }
258 /// isAbsolute - Check if this is an absolute symbol.
259 bool isAbsolute(bool SetUsed = true) const {
260 return getFragment(SetUsed) == AbsolutePseudoFragment;
263 /// Get the section associated with a defined, non-absolute symbol.
264 MCSection &getSection(bool SetUsed = true) const {
265 assert(isInSection(SetUsed) && "Invalid accessor!");
266 return *getSectionPtr(SetUsed);
269 /// Mark the symbol as defined in the fragment \p F.
270 void setFragment(MCFragment *F) const {
271 assert(!isVariable() && "Cannot set fragment of variable");
272 FragmentAndHasName.setPointer(F);
275 /// Mark the symbol as undefined.
276 void setUndefined() { FragmentAndHasName.setPointer(nullptr); }
278 bool isELF() const { return Kind == SymbolKindELF; }
280 bool isCOFF() const { return Kind == SymbolKindCOFF; }
282 bool isMachO() const { return Kind == SymbolKindMachO; }
285 /// \name Variable Symbols
288 /// isVariable - Check if this is a variable symbol.
289 bool isVariable() const {
290 return SymbolContents == SymContentsVariable;
293 /// getVariableValue - Get the value for variable symbols.
294 const MCExpr *getVariableValue(bool SetUsed = true) const {
295 assert(isVariable() && "Invalid accessor!");
300 void setVariableValue(const MCExpr *Value);
304 /// Get the (implementation defined) index.
305 uint32_t getIndex() const {
309 /// Set the (implementation defined) index.
310 void setIndex(uint32_t Value) const {
314 uint64_t getOffset() const {
315 assert((SymbolContents == SymContentsUnset ||
316 SymbolContents == SymContentsOffset) &&
317 "Cannot get offset for a common/variable symbol");
320 void setOffset(uint64_t Value) {
321 assert((SymbolContents == SymContentsUnset ||
322 SymbolContents == SymContentsOffset) &&
323 "Cannot set offset for a common/variable symbol");
325 SymbolContents = SymContentsOffset;
328 /// Return the size of a 'common' symbol.
329 uint64_t getCommonSize() const {
330 assert(isCommon() && "Not a 'common' symbol!");
334 /// Mark this symbol as being 'common'.
336 /// \param Size - The size of the symbol.
337 /// \param Align - The alignment of the symbol.
338 void setCommon(uint64_t Size, unsigned Align) {
339 assert(getOffset() == 0);
341 SymbolContents = SymContentsCommon;
343 assert((!Align || isPowerOf2_32(Align)) &&
344 "Alignment must be a power of 2");
345 unsigned Log2Align = Log2_32(Align) + 1;
346 assert(Log2Align < (1U << NumCommonAlignmentBits) &&
347 "Out of range alignment");
348 CommonAlignLog2 = Log2Align;
351 /// Return the alignment of a 'common' symbol.
352 unsigned getCommonAlignment() const {
353 assert(isCommon() && "Not a 'common' symbol!");
354 return CommonAlignLog2 ? (1U << (CommonAlignLog2 - 1)) : 0;
357 /// Declare this symbol as being 'common'.
359 /// \param Size - The size of the symbol.
360 /// \param Align - The alignment of the symbol.
361 /// \return True if symbol was already declared as a different type
362 bool declareCommon(uint64_t Size, unsigned Align) {
363 assert(isCommon() || getOffset() == 0);
365 if(CommonSize != Size || getCommonAlignment() != Align)
368 setCommon(Size, Align);
372 /// Is this a 'common' symbol.
373 bool isCommon() const {
374 return SymbolContents == SymContentsCommon;
377 MCFragment *getFragment(bool SetUsed = true) const {
378 MCFragment *Fragment = FragmentAndHasName.getPointer();
379 if (Fragment || !isVariable())
381 Fragment = getVariableValue(SetUsed)->findAssociatedFragment();
382 FragmentAndHasName.setPointer(Fragment);
386 bool isExternal() const { return IsExternal; }
387 void setExternal(bool Value) const { IsExternal = Value; }
389 bool isPrivateExtern() const { return IsPrivateExtern; }
390 void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
392 /// print - Print the value to the stream \p OS.
393 void print(raw_ostream &OS, const MCAsmInfo *MAI) const;
395 /// dump - Print the value to stderr.
399 /// Get the (implementation defined) symbol flags.
400 uint32_t getFlags() const { return Flags; }
402 /// Set the (implementation defined) symbol flags.
403 void setFlags(uint32_t Value) const {
404 assert(Value < (1U << NumFlagsBits) && "Out of range flags");
408 /// Modify the flags via a mask
409 void modifyFlags(uint32_t Value, uint32_t Mask) const {
410 assert(Value < (1U << NumFlagsBits) && "Out of range flags");
411 Flags = (Flags & ~Mask) | Value;
415 inline raw_ostream &operator<<(raw_ostream &OS, const MCSymbol &Sym) {
416 Sym.print(OS, nullptr);
419 } // end namespace llvm