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 /// Offset - The offset to apply to the fragment address to form this
42 /// CommonSize - The size of the symbol, if it is 'common'.
46 /// SymbolSize - An expression describing how to calculate the size of
47 /// a symbol. If a symbol has no size this field will be NULL.
48 const MCExpr *SymbolSize = nullptr;
50 /// CommonAlign - The alignment of the symbol, if it is 'common', or -1.
52 // FIXME: Pack this in with other fields?
53 unsigned CommonAlign = -1U;
55 /// Flags - The Flags field is used by object file implementations to store
56 /// additional per symbol information which is not easily classified.
60 MCSymbolData() { Offset = 0; }
62 MCFragment *getFragment() const { return Fragment.getPointer(); }
63 void setFragment(MCFragment *Value) { Fragment.setPointer(Value); }
65 uint64_t getOffset() const {
69 void setOffset(uint64_t Value) {
75 /// \name Symbol Attributes
78 bool isExternal() const { return Fragment.getInt() & 1; }
79 void setExternal(bool Value) {
80 Fragment.setInt((Fragment.getInt() & ~1) | unsigned(Value));
83 bool isPrivateExtern() const { return Fragment.getInt() & 2; }
84 void setPrivateExtern(bool Value) {
85 Fragment.setInt((Fragment.getInt() & ~2) | (unsigned(Value) << 1));
88 /// isCommon - Is this a 'common' symbol.
89 bool isCommon() const { return CommonAlign != -1U; }
91 /// setCommon - Mark this symbol as being 'common'.
93 /// \param Size - The size of the symbol.
94 /// \param Align - The alignment of the symbol.
95 void setCommon(uint64_t Size, unsigned Align) {
96 assert(getOffset() == 0);
101 /// getCommonSize - Return the size of a 'common' symbol.
102 uint64_t getCommonSize() const {
103 assert(isCommon() && "Not a 'common' symbol!");
107 void setSize(const MCExpr *SS) { SymbolSize = SS; }
109 const MCExpr *getSize() const { return SymbolSize; }
111 /// getCommonAlignment - Return the alignment of a 'common' symbol.
112 unsigned getCommonAlignment() const {
113 assert(isCommon() && "Not a 'common' symbol!");
117 /// getFlags - Get the (implementation defined) symbol flags.
118 uint32_t getFlags() const { return Flags; }
120 /// setFlags - Set the (implementation defined) symbol flags.
121 void setFlags(uint32_t Value) { Flags = Value; }
123 /// modifyFlags - Modify the flags via a mask
124 void modifyFlags(uint32_t Value, uint32_t Mask) {
125 Flags = (Flags & ~Mask) | Value;
133 /// MCSymbol - Instances of this class represent a symbol name in the MC file,
134 /// and MCSymbols are created and uniqued by the MCContext class. MCSymbols
135 /// should only be constructed with valid names for the object file.
137 /// If the symbol is defined/emitted into the current translation unit, the
138 /// Section member is set to indicate what section it lives in. Otherwise, if
139 /// it is a reference to an external entity, it has a null section.
141 // Special sentinal value for the absolute pseudo section.
143 // FIXME: Use a PointerInt wrapper for this?
144 static MCSection *AbsolutePseudoSection;
146 /// Name - The name of the symbol. The referred-to string data is actually
147 /// held by the StringMap that lives in MCContext.
148 const StringMapEntry<bool> *Name;
150 /// The section the symbol is defined in. This is null for undefined symbols,
151 /// and the special AbsolutePseudoSection value for absolute symbols. If this
152 /// is a variable symbol, this caches the variable value's section.
153 mutable MCSection *Section;
155 /// Value - If non-null, the value for a variable symbol.
158 /// IsTemporary - True if this is an assembler temporary label, which
159 /// typically does not survive in the .o file's symbol table. Usually
160 /// "Lfoo" or ".foo".
161 unsigned IsTemporary : 1;
163 /// \brief True if this symbol can be redefined.
164 unsigned IsRedefinable : 1;
166 /// IsUsed - True if this symbol has been used.
167 mutable unsigned IsUsed : 1;
169 mutable bool HasData : 1;
171 /// Index field, for use by the object file implementation.
172 mutable uint64_t Index : 60;
174 mutable MCSymbolData Data;
176 private: // MCContext creates and uniques these.
178 friend class MCContext;
179 MCSymbol(const StringMapEntry<bool> *Name, bool isTemporary)
180 : Name(Name), Section(nullptr), Value(nullptr), IsTemporary(isTemporary),
181 IsRedefinable(false), IsUsed(false), HasData(false), Index(0) {}
183 MCSymbol(const MCSymbol &) = delete;
184 void operator=(const MCSymbol &) = delete;
185 MCSection *getSectionPtr() const {
186 if (Section || !Value)
188 return Section = Value->FindAssociatedSection();
192 /// getName - Get the symbol name.
193 StringRef getName() const { return Name ? Name->first() : ""; }
195 bool hasData() const { return HasData; }
197 /// Get associated symbol data.
198 MCSymbolData &getData() const {
199 assert(HasData && "Missing symbol data!");
203 /// Initialize symbol data.
205 /// Nothing really to do here, but this is enables an assertion that \a
206 /// MCAssembler::getOrCreateSymbolData() has actually been called before
207 /// anyone calls \a getData().
208 void initializeData() const { HasData = true; }
213 /// isTemporary - Check if this is an assembler temporary symbol.
214 bool isTemporary() const { return IsTemporary; }
216 /// isUsed - Check if this is used.
217 bool isUsed() const { return IsUsed; }
218 void setUsed(bool Value) const { IsUsed = Value; }
220 /// \brief Check if this symbol is redefinable.
221 bool isRedefinable() const { return IsRedefinable; }
222 /// \brief Mark this symbol as redefinable.
223 void setRedefinable(bool Value) { IsRedefinable = Value; }
224 /// \brief Prepare this symbol to be redefined.
225 void redefineIfPossible() {
229 IsRedefinable = false;
234 /// \name Associated Sections
237 /// isDefined - Check if this symbol is defined (i.e., it has an address).
239 /// Defined symbols are either absolute or in some section.
240 bool isDefined() const { return getSectionPtr() != nullptr; }
242 /// isInSection - Check if this symbol is defined in some section (i.e., it
243 /// is defined but not absolute).
244 bool isInSection() const { return isDefined() && !isAbsolute(); }
246 /// isUndefined - Check if this symbol undefined (i.e., implicitly defined).
247 bool isUndefined() const { return !isDefined(); }
249 /// isAbsolute - Check if this is an absolute symbol.
250 bool isAbsolute() const { return getSectionPtr() == AbsolutePseudoSection; }
252 /// Get the section associated with a defined, non-absolute symbol.
253 MCSection &getSection() const {
254 assert(isInSection() && "Invalid accessor!");
255 return *getSectionPtr();
258 /// Mark the symbol as defined in the section \p S.
259 void setSection(MCSection &S) {
260 assert(!isVariable() && "Cannot set section of variable");
264 /// setUndefined - Mark the symbol as undefined.
265 void setUndefined() { Section = nullptr; }
268 /// \name Variable Symbols
271 /// isVariable - Check if this is a variable symbol.
272 bool isVariable() const { return Value != nullptr; }
274 /// getVariableValue() - Get the value for variable symbols.
275 const MCExpr *getVariableValue() const {
276 assert(isVariable() && "Invalid accessor!");
281 void setVariableValue(const MCExpr *Value);
285 /// Get the (implementation defined) index.
286 uint64_t getIndex() const {
287 assert(HasData && "Uninitialized symbol data");
291 /// Set the (implementation defined) index.
292 void setIndex(uint64_t Value) const {
293 assert(HasData && "Uninitialized symbol data");
294 assert(!(Value >> 60) && "Not enough bits for value");
298 /// print - Print the value to the stream \p OS.
299 void print(raw_ostream &OS) const;
301 /// dump - Print the value to stderr.
305 inline raw_ostream &operator<<(raw_ostream &OS, const MCSymbol &Sym) {
309 } // end namespace llvm