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 // TODO: Merge completely with MCSymbol.
32 /// Fragment - The fragment this symbol's value is relative to, if any. Also
33 /// stores if this symbol is visible outside this translation unit (bit 0) or
34 /// if it is private extern (bit 1).
35 PointerIntPair<MCFragment *, 2> Fragment;
38 /// Flags - The Flags field is used by object file implementations to store
39 /// additional per symbol information which is not easily classified.
45 MCFragment *getFragment() const { return Fragment.getPointer(); }
46 void setFragment(MCFragment *Value) { Fragment.setPointer(Value); }
49 /// \name Symbol Attributes
52 bool isExternal() const { return Fragment.getInt() & 1; }
53 void setExternal(bool Value) {
54 Fragment.setInt((Fragment.getInt() & ~1) | unsigned(Value));
57 bool isPrivateExtern() const { return Fragment.getInt() & 2; }
58 void setPrivateExtern(bool Value) {
59 Fragment.setInt((Fragment.getInt() & ~2) | (unsigned(Value) << 1));
62 /// getFlags - Get the (implementation defined) symbol flags.
63 uint32_t getFlags() const { return Flags; }
65 /// setFlags - Set the (implementation defined) symbol flags.
66 void setFlags(uint32_t Value) { Flags = Value; }
68 /// modifyFlags - Modify the flags via a mask
69 void modifyFlags(uint32_t Value, uint32_t Mask) {
70 Flags = (Flags & ~Mask) | Value;
78 /// MCSymbol - Instances of this class represent a symbol name in the MC file,
79 /// and MCSymbols are created and uniqued by the MCContext class. MCSymbols
80 /// should only be constructed with valid names for the object file.
82 /// If the symbol is defined/emitted into the current translation unit, the
83 /// Section member is set to indicate what section it lives in. Otherwise, if
84 /// it is a reference to an external entity, it has a null section.
86 // Special sentinal value for the absolute pseudo section.
88 // FIXME: Use a PointerInt wrapper for this?
89 static MCSection *AbsolutePseudoSection;
91 /// Name - The name of the symbol. The referred-to string data is actually
92 /// held by the StringMap that lives in MCContext.
93 const StringMapEntry<bool> *Name;
95 /// The section the symbol is defined in. This is null for undefined symbols,
96 /// and the special AbsolutePseudoSection value for absolute symbols. If this
97 /// is a variable symbol, this caches the variable value's section.
98 mutable MCSection *Section;
100 /// Value - If non-null, the value for a variable symbol.
103 /// IsTemporary - True if this is an assembler temporary label, which
104 /// typically does not survive in the .o file's symbol table. Usually
105 /// "Lfoo" or ".foo".
106 unsigned IsTemporary : 1;
108 /// \brief True if this symbol can be redefined.
109 unsigned IsRedefinable : 1;
111 /// IsUsed - True if this symbol has been used.
112 mutable unsigned IsUsed : 1;
114 mutable bool HasData : 1;
116 /// Index field, for use by the object file implementation.
117 mutable uint64_t Index : 60;
119 /// An expression describing how to calculate the size of a symbol. If a
120 /// symbol has no size this field will be NULL.
121 const MCExpr *SymbolSize = nullptr;
123 /// The alignment of the symbol, if it is 'common', or -1.
125 // FIXME: Pack this in with other fields?
126 unsigned CommonAlign = -1U;
129 /// The offset to apply to the fragment address to form this symbol's value.
132 /// The size of the symbol, if it is 'common'.
136 mutable MCSymbolData Data;
138 private: // MCContext creates and uniques these.
140 friend class MCContext;
141 MCSymbol(const StringMapEntry<bool> *Name, bool isTemporary)
142 : Name(Name), Section(nullptr), Value(nullptr), IsTemporary(isTemporary),
143 IsRedefinable(false), IsUsed(false), HasData(false), Index(0) {
147 MCSymbol(const MCSymbol &) = delete;
148 void operator=(const MCSymbol &) = delete;
149 MCSection *getSectionPtr() const {
150 if (Section || !Value)
152 return Section = Value->FindAssociatedSection();
156 /// getName - Get the symbol name.
157 StringRef getName() const { return Name ? Name->first() : ""; }
159 bool hasData() const { return HasData; }
161 /// Get associated symbol data.
162 MCSymbolData &getData() const {
163 assert(HasData && "Missing symbol data!");
167 /// Initialize symbol data.
169 /// Nothing really to do here, but this is enables an assertion that \a
170 /// MCAssembler::getOrCreateSymbolData() has actually been called before
171 /// anyone calls \a getData().
172 void initializeData() const { HasData = true; }
177 /// isTemporary - Check if this is an assembler temporary symbol.
178 bool isTemporary() const { return IsTemporary; }
180 /// isUsed - Check if this is used.
181 bool isUsed() const { return IsUsed; }
182 void setUsed(bool Value) const { IsUsed = Value; }
184 /// \brief Check if this symbol is redefinable.
185 bool isRedefinable() const { return IsRedefinable; }
186 /// \brief Mark this symbol as redefinable.
187 void setRedefinable(bool Value) { IsRedefinable = Value; }
188 /// \brief Prepare this symbol to be redefined.
189 void redefineIfPossible() {
193 IsRedefinable = false;
198 /// \name Associated Sections
201 /// isDefined - Check if this symbol is defined (i.e., it has an address).
203 /// Defined symbols are either absolute or in some section.
204 bool isDefined() const { return getSectionPtr() != nullptr; }
206 /// isInSection - Check if this symbol is defined in some section (i.e., it
207 /// is defined but not absolute).
208 bool isInSection() const { return isDefined() && !isAbsolute(); }
210 /// isUndefined - Check if this symbol undefined (i.e., implicitly defined).
211 bool isUndefined() const { return !isDefined(); }
213 /// isAbsolute - Check if this is an absolute symbol.
214 bool isAbsolute() const { return getSectionPtr() == AbsolutePseudoSection; }
216 /// Get the section associated with a defined, non-absolute symbol.
217 MCSection &getSection() const {
218 assert(isInSection() && "Invalid accessor!");
219 return *getSectionPtr();
222 /// Mark the symbol as defined in the section \p S.
223 void setSection(MCSection &S) {
224 assert(!isVariable() && "Cannot set section of variable");
228 /// setUndefined - Mark the symbol as undefined.
229 void setUndefined() { Section = nullptr; }
232 /// \name Variable Symbols
235 /// isVariable - Check if this is a variable symbol.
236 bool isVariable() const { return Value != nullptr; }
238 /// getVariableValue() - Get the value for variable symbols.
239 const MCExpr *getVariableValue() const {
240 assert(isVariable() && "Invalid accessor!");
245 void setVariableValue(const MCExpr *Value);
249 /// Get the (implementation defined) index.
250 uint64_t getIndex() const {
251 assert(HasData && "Uninitialized symbol data");
255 /// Set the (implementation defined) index.
256 void setIndex(uint64_t Value) const {
257 assert(HasData && "Uninitialized symbol data");
258 assert(!(Value >> 60) && "Not enough bits for value");
262 void setSize(const MCExpr *SS) { SymbolSize = SS; }
264 const MCExpr *getSize() const { return SymbolSize; }
266 uint64_t getOffset() const {
270 void setOffset(uint64_t Value) {
275 /// Return the size of a 'common' symbol.
276 uint64_t getCommonSize() const {
277 assert(isCommon() && "Not a 'common' symbol!");
281 /// Mark this symbol as being 'common'.
283 /// \param Size - The size of the symbol.
284 /// \param Align - The alignment of the symbol.
285 void setCommon(uint64_t Size, unsigned Align) {
286 assert(getOffset() == 0);
291 /// Return the alignment of a 'common' symbol.
292 unsigned getCommonAlignment() const {
293 assert(isCommon() && "Not a 'common' symbol!");
297 /// Is this a 'common' symbol.
298 bool isCommon() const { return CommonAlign != -1U; }
300 /// print - Print the value to the stream \p OS.
301 void print(raw_ostream &OS) const;
303 /// dump - Print the value to stderr.
307 inline raw_ostream &operator<<(raw_ostream &OS, const MCSymbol &Sym) {
311 } // end namespace llvm