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/StringMap.h"
18 #include "llvm/MC/MCAssembler.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.
39 /// The kind of the symbol. If it is any value other than unset then this
40 /// class is actually one of the appropriate subclasses of MCSymbol.
48 // Special sentinal value for the absolute pseudo section.
50 // FIXME: Use a PointerInt wrapper for this?
51 static MCSection *AbsolutePseudoSection;
53 /// Name - The name of the symbol. The referred-to string data is actually
54 /// held by the StringMap that lives in MCContext.
55 const StringMapEntry<bool> *Name;
57 /// If a symbol has a Fragment, the section is implied, so we only need
59 /// FIXME: We might be able to simplify this by having the asm streamer create
62 /// The section the symbol is defined in. This is null for undefined
63 /// symbols, and the special AbsolutePseudoSection value for absolute
64 /// symbols. If this is a variable symbol, this caches the variable value's
66 mutable MCSection *Section;
68 /// The fragment this symbol's value is relative to, if any.
69 mutable MCFragment *Fragment;
72 /// Value - If non-null, the value for a variable symbol.
75 /// IsTemporary - True if this is an assembler temporary label, which
76 /// typically does not survive in the .o file's symbol table. Usually
78 unsigned IsTemporary : 1;
80 /// \brief True if this symbol can be redefined.
81 unsigned IsRedefinable : 1;
83 /// IsUsed - True if this symbol has been used.
84 mutable unsigned IsUsed : 1;
86 mutable bool IsRegistered : 1;
88 /// This symbol is visible outside this translation unit.
89 mutable unsigned IsExternal : 1;
91 /// This symbol is private extern.
92 mutable unsigned IsPrivateExtern : 1;
94 mutable unsigned HasFragment : 1;
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;
121 MCSymbol(SymbolKind Kind, const StringMapEntry<bool> *Name, bool isTemporary)
122 : Name(Name), Section(nullptr), Value(nullptr), IsTemporary(isTemporary),
123 IsRedefinable(false), IsUsed(false), IsRegistered(false),
124 IsExternal(false), IsPrivateExtern(false), HasFragment(false),
130 MCSymbol(const MCSymbol &) = delete;
131 void operator=(const MCSymbol &) = delete;
132 MCSection *getSectionPtr() const {
133 if (MCFragment *F = getFragment())
134 return F->getParent();
135 assert(!HasFragment);
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() {
168 IsRedefinable = false;
173 /// \name Associated Sections
176 /// isDefined - Check if this symbol is defined (i.e., it has an address).
178 /// Defined symbols are either absolute or in some section.
179 bool isDefined() const { return getSectionPtr() != nullptr; }
181 /// isInSection - Check if this symbol is defined in some section (i.e., it
182 /// is defined but not absolute).
183 bool isInSection() const { return isDefined() && !isAbsolute(); }
185 /// isUndefined - Check if this symbol undefined (i.e., implicitly defined).
186 bool isUndefined() const { return !isDefined(); }
188 /// isAbsolute - Check if this is an absolute symbol.
189 bool isAbsolute() const { return getSectionPtr() == AbsolutePseudoSection; }
191 /// Get the section associated with a defined, non-absolute symbol.
192 MCSection &getSection() const {
193 assert(isInSection() && "Invalid accessor!");
194 return *getSectionPtr();
197 /// Mark the symbol as defined in the section \p S.
198 void setSection(MCSection &S) {
199 assert(!isVariable() && "Cannot set section of variable");
200 assert(!HasFragment);
204 /// Mark the symbol as undefined.
205 void setUndefined() {
210 bool isELF() const { return Kind == SymbolKindELF; }
212 bool isCOFF() const { return Kind == SymbolKindCOFF; }
214 bool isMachO() const { return Kind == SymbolKindMachO; }
217 /// \name Variable Symbols
220 /// isVariable - Check if this is a variable symbol.
221 bool isVariable() const { return Value != nullptr; }
223 /// getVariableValue() - Get the value for variable symbols.
224 const MCExpr *getVariableValue() const {
225 assert(isVariable() && "Invalid accessor!");
230 void setVariableValue(const MCExpr *Value);
234 /// Get the (implementation defined) index.
235 uint32_t getIndex() const {
239 /// Set the (implementation defined) index.
240 void setIndex(uint32_t Value) const {
244 uint64_t getOffset() const {
248 void setOffset(uint64_t Value) {
253 /// Return the size of a 'common' symbol.
254 uint64_t getCommonSize() const {
255 assert(isCommon() && "Not a 'common' symbol!");
259 /// Mark this symbol as being 'common'.
261 /// \param Size - The size of the symbol.
262 /// \param Align - The alignment of the symbol.
263 void setCommon(uint64_t Size, unsigned Align) {
264 assert(getOffset() == 0);
269 /// Return the alignment of a 'common' symbol.
270 unsigned getCommonAlignment() const {
271 assert(isCommon() && "Not a 'common' symbol!");
275 /// Declare this symbol as being 'common'.
277 /// \param Size - The size of the symbol.
278 /// \param Align - The alignment of the symbol.
279 /// \return True if symbol was already declared as a different type
280 bool declareCommon(uint64_t Size, unsigned Align) {
281 assert(isCommon() || getOffset() == 0);
283 if(CommonSize != Size || CommonAlign != Align)
286 setCommon(Size, Align);
290 /// Is this a 'common' symbol.
291 bool isCommon() const { return CommonAlign != -1U; }
293 /// Get the (implementation defined) symbol flags.
294 uint32_t getFlags() const { return Flags; }
296 /// Set the (implementation defined) symbol flags.
297 void setFlags(uint32_t Value) const { Flags = Value; }
299 /// Modify the flags via a mask
300 void modifyFlags(uint32_t Value, uint32_t Mask) const {
301 Flags = (Flags & ~Mask) | Value;
304 MCFragment *getFragment() const {
309 void setFragment(MCFragment *Value) const {
314 bool isExternal() const { return IsExternal; }
315 void setExternal(bool Value) const { IsExternal = Value; }
317 bool isPrivateExtern() const { return IsPrivateExtern; }
318 void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
320 /// print - Print the value to the stream \p OS.
321 void print(raw_ostream &OS) const;
323 /// dump - Print the value to stderr.
327 inline raw_ostream &operator<<(raw_ostream &OS, const MCSymbol &Sym) {
331 } // end namespace llvm