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.
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 /// If a symbol has a Fragment, the section is implied, so we only need
49 /// FIXME: We might be able to simplify this by having the asm streamer create
52 /// The section the symbol is defined in. This is null for undefined
53 /// symbols, and the special AbsolutePseudoSection value for absolute
54 /// symbols. If this is a variable symbol, this caches the variable value's
56 mutable MCSection *Section;
58 /// The fragment this symbol's value is relative to, if any.
59 mutable MCFragment *Fragment;
62 /// Value - If non-null, the value for a variable symbol.
65 /// IsTemporary - True if this is an assembler temporary label, which
66 /// typically does not survive in the .o file's symbol table. Usually
68 unsigned IsTemporary : 1;
70 /// \brief True if this symbol can be redefined.
71 unsigned IsRedefinable : 1;
73 /// IsUsed - True if this symbol has been used.
74 mutable unsigned IsUsed : 1;
76 mutable bool IsRegistered : 1;
78 /// This symbol is visible outside this translation unit.
79 mutable unsigned IsExternal : 1;
81 /// This symbol is private extern.
82 mutable unsigned IsPrivateExtern : 1;
84 mutable unsigned HasFragment : 1;
86 /// Index field, for use by the object file implementation.
87 mutable uint32_t Index = 0;
89 /// An expression describing how to calculate the size of a symbol. If a
90 /// symbol has no size this field will be NULL.
91 const MCExpr *SymbolSize = nullptr;
94 /// The offset to apply to the fragment address to form this symbol's value.
97 /// The size of the symbol, if it is 'common'.
101 /// The alignment of the symbol, if it is 'common', or -1.
103 // FIXME: Pack this in with other fields?
104 unsigned CommonAlign = -1U;
106 /// The Flags field is used by object file implementations to store
107 /// additional per symbol information which is not easily classified.
108 mutable uint32_t Flags = 0;
110 private: // MCContext creates and uniques these.
112 friend class MCContext;
113 MCSymbol(const StringMapEntry<bool> *Name, bool isTemporary)
114 : Name(Name), Section(nullptr), Value(nullptr), IsTemporary(isTemporary),
115 IsRedefinable(false), IsUsed(false), IsRegistered(false),
116 IsExternal(false), IsPrivateExtern(false), HasFragment(false) {
120 MCSymbol(const MCSymbol &) = delete;
121 void operator=(const MCSymbol &) = delete;
122 MCSection *getSectionPtr() const {
123 if (MCFragment *F = getFragment())
124 return F->getParent();
125 assert(!HasFragment);
126 if (Section || !Value)
128 return Section = Value->findAssociatedSection();
132 /// getName - Get the symbol name.
133 StringRef getName() const { return Name ? Name->first() : ""; }
135 bool isRegistered() const { return IsRegistered; }
136 void setIsRegistered(bool Value) const { IsRegistered = Value; }
141 /// isTemporary - Check if this is an assembler temporary symbol.
142 bool isTemporary() const { return IsTemporary; }
144 /// isUsed - Check if this is used.
145 bool isUsed() const { return IsUsed; }
146 void setUsed(bool Value) const { IsUsed = Value; }
148 /// \brief Check if this symbol is redefinable.
149 bool isRedefinable() const { return IsRedefinable; }
150 /// \brief Mark this symbol as redefinable.
151 void setRedefinable(bool Value) { IsRedefinable = Value; }
152 /// \brief Prepare this symbol to be redefined.
153 void redefineIfPossible() {
158 IsRedefinable = false;
163 /// \name Associated Sections
166 /// isDefined - Check if this symbol is defined (i.e., it has an address).
168 /// Defined symbols are either absolute or in some section.
169 bool isDefined() const { return getSectionPtr() != nullptr; }
171 /// isInSection - Check if this symbol is defined in some section (i.e., it
172 /// is defined but not absolute).
173 bool isInSection() const { return isDefined() && !isAbsolute(); }
175 /// isUndefined - Check if this symbol undefined (i.e., implicitly defined).
176 bool isUndefined() const { return !isDefined(); }
178 /// isAbsolute - Check if this is an absolute symbol.
179 bool isAbsolute() const { return getSectionPtr() == AbsolutePseudoSection; }
181 /// Get the section associated with a defined, non-absolute symbol.
182 MCSection &getSection() const {
183 assert(isInSection() && "Invalid accessor!");
184 return *getSectionPtr();
187 /// Mark the symbol as defined in the section \p S.
188 void setSection(MCSection &S) {
189 assert(!isVariable() && "Cannot set section of variable");
190 assert(!HasFragment);
194 /// Mark the symbol as undefined.
195 void setUndefined() {
201 /// \name Variable Symbols
204 /// isVariable - Check if this is a variable symbol.
205 bool isVariable() const { return Value != nullptr; }
207 /// getVariableValue() - Get the value for variable symbols.
208 const MCExpr *getVariableValue() const {
209 assert(isVariable() && "Invalid accessor!");
214 void setVariableValue(const MCExpr *Value);
218 /// Get the (implementation defined) index.
219 uint32_t getIndex() const {
223 /// Set the (implementation defined) index.
224 void setIndex(uint32_t Value) const {
228 void setSize(const MCExpr *SS) { SymbolSize = SS; }
230 const MCExpr *getSize() const { return SymbolSize; }
232 uint64_t getOffset() const {
236 void setOffset(uint64_t Value) {
241 /// Return the size of a 'common' symbol.
242 uint64_t getCommonSize() const {
243 assert(isCommon() && "Not a 'common' symbol!");
247 /// Mark this symbol as being 'common'.
249 /// \param Size - The size of the symbol.
250 /// \param Align - The alignment of the symbol.
251 void setCommon(uint64_t Size, unsigned Align) {
252 assert(getOffset() == 0);
257 /// Return the alignment of a 'common' symbol.
258 unsigned getCommonAlignment() const {
259 assert(isCommon() && "Not a 'common' symbol!");
263 /// Is this a 'common' symbol.
264 bool isCommon() const { return CommonAlign != -1U; }
266 /// Get the (implementation defined) symbol flags.
267 uint32_t getFlags() const { return Flags; }
269 /// Set the (implementation defined) symbol flags.
270 void setFlags(uint32_t Value) const { Flags = Value; }
272 /// Modify the flags via a mask
273 void modifyFlags(uint32_t Value, uint32_t Mask) const {
274 Flags = (Flags & ~Mask) | Value;
277 MCFragment *getFragment() const {
282 void setFragment(MCFragment *Value) const {
287 bool isExternal() const { return IsExternal; }
288 void setExternal(bool Value) const { IsExternal = Value; }
290 bool isPrivateExtern() const { return IsPrivateExtern; }
291 void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
293 /// print - Print the value to the stream \p OS.
294 void print(raw_ostream &OS) const;
296 /// dump - Print the value to stderr.
300 inline raw_ostream &operator<<(raw_ostream &OS, const MCSymbol &Sym) {
304 } // end namespace llvm