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/StringMap.h"
19 #include "llvm/MC/MCExpr.h"
20 #include "llvm/Support/Compiler.h"
30 /// MCSymbol - Instances of this class represent a symbol name in the MC file,
31 /// and MCSymbols are created and uniqued by the MCContext class. MCSymbols
32 /// should only be constructed with valid names for the object file.
34 /// If the symbol is defined/emitted into the current translation unit, the
35 /// Section member is set to indicate what section it lives in. Otherwise, if
36 /// it is a reference to an external entity, it has a null section.
38 // Special sentinal value for the absolute pseudo section.
40 // FIXME: Use a PointerInt wrapper for this?
41 static MCSection *AbsolutePseudoSection;
43 /// Name - The name of the symbol. The referred-to string data is actually
44 /// held by the StringMap that lives in MCContext.
45 const StringMapEntry<bool> *Name;
47 /// The section the symbol is defined in. This is null for undefined symbols,
48 /// and the special AbsolutePseudoSection value for absolute symbols. If this
49 /// is a variable symbol, this caches the variable value's section.
50 mutable MCSection *Section;
52 /// Value - If non-null, the value for a variable symbol.
55 /// IsTemporary - True if this is an assembler temporary label, which
56 /// typically does not survive in the .o file's symbol table. Usually
58 unsigned IsTemporary : 1;
60 /// \brief True if this symbol can be redefined.
61 unsigned IsRedefinable : 1;
63 /// IsUsed - True if this symbol has been used.
64 mutable unsigned IsUsed : 1;
66 mutable bool IsRegistered : 1;
68 /// Index field, for use by the object file implementation.
69 mutable uint32_t Index = 0;
71 /// An expression describing how to calculate the size of a symbol. If a
72 /// symbol has no size this field will be NULL.
73 const MCExpr *SymbolSize = nullptr;
76 /// The offset to apply to the fragment address to form this symbol's value.
79 /// The size of the symbol, if it is 'common'.
83 /// The alignment of the symbol, if it is 'common', or -1.
85 // FIXME: Pack this in with other fields?
86 unsigned CommonAlign = -1U;
88 /// The Flags field is used by object file implementations to store
89 /// additional per symbol information which is not easily classified.
90 mutable uint32_t Flags = 0;
92 /// The fragment this symbol's value is relative to, if any. Also stores if
93 /// this symbol is visible outside this translation unit (bit 0) or if it is
94 /// private extern (bit 1).
95 mutable PointerIntPair<MCFragment *, 2> Fragment;
97 private: // MCContext creates and uniques these.
99 friend class MCContext;
100 MCSymbol(const StringMapEntry<bool> *Name, bool isTemporary)
101 : Name(Name), Section(nullptr), Value(nullptr), IsTemporary(isTemporary),
102 IsRedefinable(false), IsUsed(false), IsRegistered(false) {
106 MCSymbol(const MCSymbol &) = delete;
107 void operator=(const MCSymbol &) = delete;
108 MCSection *getSectionPtr() const {
109 if (Section || !Value)
111 return Section = Value->findAssociatedSection();
115 /// getName - Get the symbol name.
116 StringRef getName() const { return Name ? Name->first() : ""; }
118 bool isRegistered() const { return IsRegistered; }
119 void setIsRegistered(bool Value) const { IsRegistered = Value; }
124 /// isTemporary - Check if this is an assembler temporary symbol.
125 bool isTemporary() const { return IsTemporary; }
127 /// isUsed - Check if this is used.
128 bool isUsed() const { return IsUsed; }
129 void setUsed(bool Value) const { IsUsed = Value; }
131 /// \brief Check if this symbol is redefinable.
132 bool isRedefinable() const { return IsRedefinable; }
133 /// \brief Mark this symbol as redefinable.
134 void setRedefinable(bool Value) { IsRedefinable = Value; }
135 /// \brief Prepare this symbol to be redefined.
136 void redefineIfPossible() {
140 IsRedefinable = false;
145 /// \name Associated Sections
148 /// isDefined - Check if this symbol is defined (i.e., it has an address).
150 /// Defined symbols are either absolute or in some section.
151 bool isDefined() const { return getSectionPtr() != nullptr; }
153 /// isInSection - Check if this symbol is defined in some section (i.e., it
154 /// is defined but not absolute).
155 bool isInSection() const { return isDefined() && !isAbsolute(); }
157 /// isUndefined - Check if this symbol undefined (i.e., implicitly defined).
158 bool isUndefined() const { return !isDefined(); }
160 /// isAbsolute - Check if this is an absolute symbol.
161 bool isAbsolute() const { return getSectionPtr() == AbsolutePseudoSection; }
163 /// Get the section associated with a defined, non-absolute symbol.
164 MCSection &getSection() const {
165 assert(isInSection() && "Invalid accessor!");
166 return *getSectionPtr();
169 /// Mark the symbol as defined in the section \p S.
170 void setSection(MCSection &S) {
171 assert(!isVariable() && "Cannot set section of variable");
175 /// setUndefined - Mark the symbol as undefined.
176 void setUndefined() { Section = nullptr; }
179 /// \name Variable Symbols
182 /// isVariable - Check if this is a variable symbol.
183 bool isVariable() const { return Value != nullptr; }
185 /// getVariableValue() - Get the value for variable symbols.
186 const MCExpr *getVariableValue() const {
187 assert(isVariable() && "Invalid accessor!");
192 void setVariableValue(const MCExpr *Value);
196 /// Get the (implementation defined) index.
197 uint32_t getIndex() const {
201 /// Set the (implementation defined) index.
202 void setIndex(uint32_t Value) const {
206 void setSize(const MCExpr *SS) { SymbolSize = SS; }
208 const MCExpr *getSize() const { return SymbolSize; }
210 uint64_t getOffset() const {
214 void setOffset(uint64_t Value) {
219 /// Return the size of a 'common' symbol.
220 uint64_t getCommonSize() const {
221 assert(isCommon() && "Not a 'common' symbol!");
225 /// Mark this symbol as being 'common'.
227 /// \param Size - The size of the symbol.
228 /// \param Align - The alignment of the symbol.
229 void setCommon(uint64_t Size, unsigned Align) {
230 assert(getOffset() == 0);
235 /// Return the alignment of a 'common' symbol.
236 unsigned getCommonAlignment() const {
237 assert(isCommon() && "Not a 'common' symbol!");
241 /// Is this a 'common' symbol.
242 bool isCommon() const { return CommonAlign != -1U; }
244 /// Get the (implementation defined) symbol flags.
245 uint32_t getFlags() const { return Flags; }
247 /// Set the (implementation defined) symbol flags.
248 void setFlags(uint32_t Value) const { Flags = Value; }
250 /// Modify the flags via a mask
251 void modifyFlags(uint32_t Value, uint32_t Mask) const {
252 Flags = (Flags & ~Mask) | Value;
255 MCFragment *getFragment() const { return Fragment.getPointer(); }
256 void setFragment(MCFragment *Value) const { Fragment.setPointer(Value); }
258 bool isExternal() const { return Fragment.getInt() & 1; }
259 void setExternal(bool Value) const {
260 Fragment.setInt((Fragment.getInt() & ~1) | unsigned(Value));
263 bool isPrivateExtern() const { return Fragment.getInt() & 2; }
264 void setPrivateExtern(bool Value) {
265 Fragment.setInt((Fragment.getInt() & ~2) | (unsigned(Value) << 1));
268 /// print - Print the value to the stream \p OS.
269 void print(raw_ostream &OS) const;
271 /// dump - Print the value to stderr.
275 inline raw_ostream &operator<<(raw_ostream &OS, const MCSymbol &Sym) {
279 } // end namespace llvm