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/StringRef.h"
19 #include "llvm/MC/MCExpr.h"
20 #include "llvm/Support/Compiler.h"
30 // TODO: Merge completely with MCSymbol.
32 const MCSymbol *Symbol;
34 /// Fragment - The fragment this symbol's value is relative to, if any. Also
35 /// stores if this symbol is visible outside this translation unit (bit 0) or
36 /// if it is private extern (bit 1).
37 PointerIntPair<MCFragment *, 2> Fragment;
40 /// Offset - The offset to apply to the fragment address to form this
44 /// CommonSize - The size of the symbol, if it is 'common'.
48 /// SymbolSize - An expression describing how to calculate the size of
49 /// a symbol. If a symbol has no size this field will be NULL.
50 const MCExpr *SymbolSize;
52 /// CommonAlign - The alignment of the symbol, if it is 'common', or -1.
54 // FIXME: Pack this in with other fields?
57 /// Flags - The Flags field is used by object file implementations to store
58 /// additional per symbol information which is not easily classified.
61 /// Index - Index field, for use by the object file implementation.
65 // Only for use as sentinel.
67 void initialize(const MCSymbol &Symbol, MCFragment *Fragment,
72 bool isInitialized() const { return Symbol; }
74 MCFragment *getFragment() const { return Fragment.getPointer(); }
75 void setFragment(MCFragment *Value) { Fragment.setPointer(Value); }
77 uint64_t getOffset() const {
81 void setOffset(uint64_t Value) {
87 /// \name Symbol Attributes
90 bool isExternal() const { return Fragment.getInt() & 1; }
91 void setExternal(bool Value) {
92 Fragment.setInt((Fragment.getInt() & ~1) | unsigned(Value));
95 bool isPrivateExtern() const { return Fragment.getInt() & 2; }
96 void setPrivateExtern(bool Value) {
97 Fragment.setInt((Fragment.getInt() & ~2) | (unsigned(Value) << 1));
100 /// isCommon - Is this a 'common' symbol.
101 bool isCommon() const { return CommonAlign != -1U; }
103 /// setCommon - Mark this symbol as being 'common'.
105 /// \param Size - The size of the symbol.
106 /// \param Align - The alignment of the symbol.
107 void setCommon(uint64_t Size, unsigned Align) {
108 assert(getOffset() == 0);
113 /// getCommonSize - Return the size of a 'common' symbol.
114 uint64_t getCommonSize() const {
115 assert(isCommon() && "Not a 'common' symbol!");
119 void setSize(const MCExpr *SS) { SymbolSize = SS; }
121 const MCExpr *getSize() const { return SymbolSize; }
123 /// getCommonAlignment - Return the alignment of a 'common' symbol.
124 unsigned getCommonAlignment() const {
125 assert(isCommon() && "Not a 'common' symbol!");
129 /// getFlags - Get the (implementation defined) symbol flags.
130 uint32_t getFlags() const { return Flags; }
132 /// setFlags - Set the (implementation defined) symbol flags.
133 void setFlags(uint32_t Value) { Flags = Value; }
135 /// modifyFlags - Modify the flags via a mask
136 void modifyFlags(uint32_t Value, uint32_t Mask) {
137 Flags = (Flags & ~Mask) | Value;
140 /// getIndex - Get the (implementation defined) index.
141 uint64_t getIndex() const { return Index; }
143 /// setIndex - Set the (implementation defined) index.
144 void setIndex(uint64_t Value) { Index = Value; }
151 /// MCSymbol - Instances of this class represent a symbol name in the MC file,
152 /// and MCSymbols are created and uniqued by the MCContext class. MCSymbols
153 /// should only be constructed with valid names for the object file.
155 /// If the symbol is defined/emitted into the current translation unit, the
156 /// Section member is set to indicate what section it lives in. Otherwise, if
157 /// it is a reference to an external entity, it has a null section.
159 // Special sentinal value for the absolute pseudo section.
161 // FIXME: Use a PointerInt wrapper for this?
162 static const MCSection *AbsolutePseudoSection;
164 /// Name - The name of the symbol. The referred-to string data is actually
165 /// held by the StringMap that lives in MCContext.
168 /// Section - The section the symbol is defined in. This is null for
169 /// undefined symbols, and the special AbsolutePseudoSection value for
170 /// absolute symbols. If this is a variable symbol, this caches the
171 /// variable value's section.
172 mutable const MCSection *Section;
174 /// Value - If non-null, the value for a variable symbol.
177 /// IsTemporary - True if this is an assembler temporary label, which
178 /// typically does not survive in the .o file's symbol table. Usually
179 /// "Lfoo" or ".foo".
180 unsigned IsTemporary : 1;
182 /// \brief True if this symbol can be redefined.
183 unsigned IsRedefinable : 1;
185 /// IsUsed - True if this symbol has been used.
186 mutable unsigned IsUsed : 1;
188 mutable MCSymbolData Data;
190 private: // MCContext creates and uniques these.
192 friend class MCContext;
193 MCSymbol(StringRef name, bool isTemporary)
194 : Name(name), Section(nullptr), Value(nullptr), IsTemporary(isTemporary),
195 IsRedefinable(false), IsUsed(false) {}
197 MCSymbol(const MCSymbol &) = delete;
198 void operator=(const MCSymbol &) = delete;
199 const MCSection *getSectionPtr() const {
200 if (Section || !Value)
202 return Section = Value->FindAssociatedSection();
206 /// getName - Get the symbol name.
207 StringRef getName() const { return Name; }
209 /// Get associated symbol data.
210 MCSymbolData &getData() const {
211 assert(Data.isInitialized() && "Missing symbol data!");
215 /// Get unsafe symbol data (even if uninitialized).
217 /// Don't assert on uninitialized data; just return it.
218 MCSymbolData &getUnsafeData() const { return Data; }
223 /// isTemporary - Check if this is an assembler temporary symbol.
224 bool isTemporary() const { return IsTemporary; }
226 /// isUsed - Check if this is used.
227 bool isUsed() const { return IsUsed; }
228 void setUsed(bool Value) const { IsUsed = Value; }
230 /// \brief Check if this symbol is redefinable.
231 bool isRedefinable() const { return IsRedefinable; }
232 /// \brief Mark this symbol as redefinable.
233 void setRedefinable(bool Value) { IsRedefinable = Value; }
234 /// \brief Prepare this symbol to be redefined.
235 void redefineIfPossible() {
239 IsRedefinable = false;
244 /// \name Associated Sections
247 /// isDefined - Check if this symbol is defined (i.e., it has an address).
249 /// Defined symbols are either absolute or in some section.
250 bool isDefined() const { return getSectionPtr() != nullptr; }
252 /// isInSection - Check if this symbol is defined in some section (i.e., it
253 /// is defined but not absolute).
254 bool isInSection() const { return isDefined() && !isAbsolute(); }
256 /// isUndefined - Check if this symbol undefined (i.e., implicitly defined).
257 bool isUndefined() const { return !isDefined(); }
259 /// isAbsolute - Check if this is an absolute symbol.
260 bool isAbsolute() const { return getSectionPtr() == AbsolutePseudoSection; }
262 /// getSection - Get the section associated with a defined, non-absolute
264 const MCSection &getSection() const {
265 assert(isInSection() && "Invalid accessor!");
266 return *getSectionPtr();
269 /// setSection - Mark the symbol as defined in the section \p S.
270 void setSection(const MCSection &S) {
271 assert(!isVariable() && "Cannot set section of variable");
275 /// setUndefined - Mark the symbol as undefined.
276 void setUndefined() { Section = nullptr; }
279 /// \name Variable Symbols
282 /// isVariable - Check if this is a variable symbol.
283 bool isVariable() const { return Value != nullptr; }
285 /// getVariableValue() - Get the value for variable symbols.
286 const MCExpr *getVariableValue() const {
287 assert(isVariable() && "Invalid accessor!");
292 void setVariableValue(const MCExpr *Value);
296 /// print - Print the value to the stream \p OS.
297 void print(raw_ostream &OS) const;
299 /// dump - Print the value to stderr.
303 inline raw_ostream &operator<<(raw_ostream &OS, const MCSymbol &Sym) {
307 } // end namespace llvm