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;
39 MCSymbolData() = default;
41 MCFragment *getFragment() const { return Fragment.getPointer(); }
42 void setFragment(MCFragment *Value) { Fragment.setPointer(Value); }
45 /// \name Symbol Attributes
48 bool isExternal() const { return Fragment.getInt() & 1; }
49 void setExternal(bool Value) {
50 Fragment.setInt((Fragment.getInt() & ~1) | unsigned(Value));
53 bool isPrivateExtern() const { return Fragment.getInt() & 2; }
54 void setPrivateExtern(bool Value) {
55 Fragment.setInt((Fragment.getInt() & ~2) | (unsigned(Value) << 1));
63 /// MCSymbol - Instances of this class represent a symbol name in the MC file,
64 /// and MCSymbols are created and uniqued by the MCContext class. MCSymbols
65 /// should only be constructed with valid names for the object file.
67 /// If the symbol is defined/emitted into the current translation unit, the
68 /// Section member is set to indicate what section it lives in. Otherwise, if
69 /// it is a reference to an external entity, it has a null section.
71 // Special sentinal value for the absolute pseudo section.
73 // FIXME: Use a PointerInt wrapper for this?
74 static MCSection *AbsolutePseudoSection;
76 /// Name - The name of the symbol. The referred-to string data is actually
77 /// held by the StringMap that lives in MCContext.
78 const StringMapEntry<bool> *Name;
80 /// The section the symbol is defined in. This is null for undefined symbols,
81 /// and the special AbsolutePseudoSection value for absolute symbols. If this
82 /// is a variable symbol, this caches the variable value's section.
83 mutable MCSection *Section;
85 /// Value - If non-null, the value for a variable symbol.
88 /// IsTemporary - True if this is an assembler temporary label, which
89 /// typically does not survive in the .o file's symbol table. Usually
91 unsigned IsTemporary : 1;
93 /// \brief True if this symbol can be redefined.
94 unsigned IsRedefinable : 1;
96 /// IsUsed - True if this symbol has been used.
97 mutable unsigned IsUsed : 1;
99 mutable bool HasData : 1;
101 /// Index field, for use by the object file implementation.
102 mutable uint64_t Index : 60;
104 /// An expression describing how to calculate the size of a symbol. If a
105 /// symbol has no size this field will be NULL.
106 const MCExpr *SymbolSize = nullptr;
108 /// The alignment of the symbol, if it is 'common', or -1.
110 // FIXME: Pack this in with other fields?
111 unsigned CommonAlign = -1U;
114 /// The offset to apply to the fragment address to form this symbol's value.
117 /// The size of the symbol, if it is 'common'.
121 /// The Flags field is used by object file implementations to store
122 /// additional per symbol information which is not easily classified.
123 mutable uint32_t Flags = 0;
125 mutable MCSymbolData Data;
127 private: // MCContext creates and uniques these.
129 friend class MCContext;
130 MCSymbol(const StringMapEntry<bool> *Name, bool isTemporary)
131 : Name(Name), Section(nullptr), Value(nullptr), IsTemporary(isTemporary),
132 IsRedefinable(false), IsUsed(false), HasData(false), Index(0) {
136 MCSymbol(const MCSymbol &) = delete;
137 void operator=(const MCSymbol &) = delete;
138 MCSection *getSectionPtr() const {
139 if (Section || !Value)
141 return Section = Value->FindAssociatedSection();
145 /// getName - Get the symbol name.
146 StringRef getName() const { return Name ? Name->first() : ""; }
148 bool hasData() const { return HasData; }
150 /// Get associated symbol data.
151 MCSymbolData &getData() const {
152 assert(HasData && "Missing symbol data!");
156 /// Initialize symbol data.
158 /// Nothing really to do here, but this is enables an assertion that \a
159 /// MCAssembler::getOrCreateSymbolData() has actually been called before
160 /// anyone calls \a getData().
161 void initializeData() const { HasData = true; }
166 /// isTemporary - Check if this is an assembler temporary symbol.
167 bool isTemporary() const { return IsTemporary; }
169 /// isUsed - Check if this is used.
170 bool isUsed() const { return IsUsed; }
171 void setUsed(bool Value) const { IsUsed = Value; }
173 /// \brief Check if this symbol is redefinable.
174 bool isRedefinable() const { return IsRedefinable; }
175 /// \brief Mark this symbol as redefinable.
176 void setRedefinable(bool Value) { IsRedefinable = Value; }
177 /// \brief Prepare this symbol to be redefined.
178 void redefineIfPossible() {
182 IsRedefinable = false;
187 /// \name Associated Sections
190 /// isDefined - Check if this symbol is defined (i.e., it has an address).
192 /// Defined symbols are either absolute or in some section.
193 bool isDefined() const { return getSectionPtr() != nullptr; }
195 /// isInSection - Check if this symbol is defined in some section (i.e., it
196 /// is defined but not absolute).
197 bool isInSection() const { return isDefined() && !isAbsolute(); }
199 /// isUndefined - Check if this symbol undefined (i.e., implicitly defined).
200 bool isUndefined() const { return !isDefined(); }
202 /// isAbsolute - Check if this is an absolute symbol.
203 bool isAbsolute() const { return getSectionPtr() == AbsolutePseudoSection; }
205 /// Get the section associated with a defined, non-absolute symbol.
206 MCSection &getSection() const {
207 assert(isInSection() && "Invalid accessor!");
208 return *getSectionPtr();
211 /// Mark the symbol as defined in the section \p S.
212 void setSection(MCSection &S) {
213 assert(!isVariable() && "Cannot set section of variable");
217 /// setUndefined - Mark the symbol as undefined.
218 void setUndefined() { Section = nullptr; }
221 /// \name Variable Symbols
224 /// isVariable - Check if this is a variable symbol.
225 bool isVariable() const { return Value != nullptr; }
227 /// getVariableValue() - Get the value for variable symbols.
228 const MCExpr *getVariableValue() const {
229 assert(isVariable() && "Invalid accessor!");
234 void setVariableValue(const MCExpr *Value);
238 /// Get the (implementation defined) index.
239 uint64_t getIndex() const {
240 assert(HasData && "Uninitialized symbol data");
244 /// Set the (implementation defined) index.
245 void setIndex(uint64_t Value) const {
246 assert(HasData && "Uninitialized symbol data");
247 assert(!(Value >> 60) && "Not enough bits for value");
251 void setSize(const MCExpr *SS) { SymbolSize = SS; }
253 const MCExpr *getSize() const { return SymbolSize; }
255 uint64_t getOffset() const {
259 void setOffset(uint64_t Value) {
264 /// Return the size of a 'common' symbol.
265 uint64_t getCommonSize() const {
266 assert(isCommon() && "Not a 'common' symbol!");
270 /// Mark this symbol as being 'common'.
272 /// \param Size - The size of the symbol.
273 /// \param Align - The alignment of the symbol.
274 void setCommon(uint64_t Size, unsigned Align) {
275 assert(getOffset() == 0);
280 /// Return the alignment of a 'common' symbol.
281 unsigned getCommonAlignment() const {
282 assert(isCommon() && "Not a 'common' symbol!");
286 /// Is this a 'common' symbol.
287 bool isCommon() const { return CommonAlign != -1U; }
289 /// Get the (implementation defined) symbol flags.
290 uint32_t getFlags() const { return Flags; }
292 /// Set the (implementation defined) symbol flags.
293 void setFlags(uint32_t Value) const { Flags = Value; }
295 /// Modify the flags via a mask
296 void modifyFlags(uint32_t Value, uint32_t Mask) const {
297 Flags = (Flags & ~Mask) | Value;
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