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;
88 /// Index field, for use by the object file implementation.
89 mutable uint32_t Index = 0;
92 /// The offset to apply to the fragment address to form this symbol's value.
95 /// The size of the symbol, if it is 'common'.
99 /// The alignment of the symbol, if it is 'common', or -1.
101 // FIXME: Pack this in with other fields?
102 unsigned CommonAlign = -1U;
104 /// The Flags field is used by object file implementations to store
105 /// additional per symbol information which is not easily classified.
106 mutable uint32_t Flags = 0;
108 protected: // MCContext creates and uniques these.
110 friend class MCContext;
111 MCSymbol(bool IsELF, const StringMapEntry<bool> *Name, bool isTemporary)
112 : Name(Name), Section(nullptr), Value(nullptr), IsTemporary(isTemporary),
113 IsRedefinable(false), IsUsed(false), IsRegistered(false),
114 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() {
200 bool isELF() const { return IsELF; }
203 /// \name Variable Symbols
206 /// isVariable - Check if this is a variable symbol.
207 bool isVariable() const { return Value != nullptr; }
209 /// getVariableValue() - Get the value for variable symbols.
210 const MCExpr *getVariableValue() const {
211 assert(isVariable() && "Invalid accessor!");
216 void setVariableValue(const MCExpr *Value);
220 /// Get the (implementation defined) index.
221 uint32_t getIndex() const {
225 /// Set the (implementation defined) index.
226 void setIndex(uint32_t Value) const {
230 uint64_t getOffset() const {
234 void setOffset(uint64_t Value) {
239 /// Return the size of a 'common' symbol.
240 uint64_t getCommonSize() const {
241 assert(isCommon() && "Not a 'common' symbol!");
245 /// Mark this symbol as being 'common'.
247 /// \param Size - The size of the symbol.
248 /// \param Align - The alignment of the symbol.
249 void setCommon(uint64_t Size, unsigned Align) {
250 assert(getOffset() == 0);
255 /// Return the alignment of a 'common' symbol.
256 unsigned getCommonAlignment() const {
257 assert(isCommon() && "Not a 'common' symbol!");
261 /// Is this a 'common' symbol.
262 bool isCommon() const { return CommonAlign != -1U; }
264 /// Get the (implementation defined) symbol flags.
265 uint32_t getFlags() const { return Flags; }
267 /// Set the (implementation defined) symbol flags.
268 void setFlags(uint32_t Value) const { Flags = Value; }
270 /// Modify the flags via a mask
271 void modifyFlags(uint32_t Value, uint32_t Mask) const {
272 Flags = (Flags & ~Mask) | Value;
275 MCFragment *getFragment() const {
280 void setFragment(MCFragment *Value) const {
285 bool isExternal() const { return IsExternal; }
286 void setExternal(bool Value) const { IsExternal = Value; }
288 bool isPrivateExtern() const { return IsPrivateExtern; }
289 void setPrivateExtern(bool Value) { IsPrivateExtern = Value; }
291 /// print - Print the value to the stream \p OS.
292 void print(raw_ostream &OS) const;
294 /// dump - Print the value to stderr.
298 inline raw_ostream &operator<<(raw_ostream &OS, const MCSymbol &Sym) {
302 } // end namespace llvm