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 /// 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.
59 /// Index - Index field, for use by the object file implementation.
63 MCSymbolData() { Offset = 0; }
65 MCFragment *getFragment() const { return Fragment.getPointer(); }
66 void setFragment(MCFragment *Value) { Fragment.setPointer(Value); }
68 uint64_t getOffset() const {
72 void setOffset(uint64_t Value) {
78 /// \name Symbol Attributes
81 bool isExternal() const { return Fragment.getInt() & 1; }
82 void setExternal(bool Value) {
83 Fragment.setInt((Fragment.getInt() & ~1) | unsigned(Value));
86 bool isPrivateExtern() const { return Fragment.getInt() & 2; }
87 void setPrivateExtern(bool Value) {
88 Fragment.setInt((Fragment.getInt() & ~2) | (unsigned(Value) << 1));
91 /// isCommon - Is this a 'common' symbol.
92 bool isCommon() const { return CommonAlign != -1U; }
94 /// setCommon - Mark this symbol as being 'common'.
96 /// \param Size - The size of the symbol.
97 /// \param Align - The alignment of the symbol.
98 void setCommon(uint64_t Size, unsigned Align) {
99 assert(getOffset() == 0);
104 /// getCommonSize - Return the size of a 'common' symbol.
105 uint64_t getCommonSize() const {
106 assert(isCommon() && "Not a 'common' symbol!");
110 void setSize(const MCExpr *SS) { SymbolSize = SS; }
112 const MCExpr *getSize() const { return SymbolSize; }
114 /// getCommonAlignment - Return the alignment of a 'common' symbol.
115 unsigned getCommonAlignment() const {
116 assert(isCommon() && "Not a 'common' symbol!");
120 /// getFlags - Get the (implementation defined) symbol flags.
121 uint32_t getFlags() const { return Flags; }
123 /// setFlags - Set the (implementation defined) symbol flags.
124 void setFlags(uint32_t Value) { Flags = Value; }
126 /// modifyFlags - Modify the flags via a mask
127 void modifyFlags(uint32_t Value, uint32_t Mask) {
128 Flags = (Flags & ~Mask) | Value;
131 /// getIndex - Get the (implementation defined) index.
132 uint64_t getIndex() const { return Index; }
134 /// setIndex - Set the (implementation defined) index.
135 void setIndex(uint64_t Value) { Index = Value; }
142 /// MCSymbol - Instances of this class represent a symbol name in the MC file,
143 /// and MCSymbols are created and uniqued by the MCContext class. MCSymbols
144 /// should only be constructed with valid names for the object file.
146 /// If the symbol is defined/emitted into the current translation unit, the
147 /// Section member is set to indicate what section it lives in. Otherwise, if
148 /// it is a reference to an external entity, it has a null section.
150 // Special sentinal value for the absolute pseudo section.
152 // FIXME: Use a PointerInt wrapper for this?
153 static MCSection *AbsolutePseudoSection;
155 /// Name - The name of the symbol. The referred-to string data is actually
156 /// held by the StringMap that lives in MCContext.
159 /// The section the symbol is defined in. This is null for undefined symbols,
160 /// and the special AbsolutePseudoSection value for absolute symbols. If this
161 /// is a variable symbol, this caches the variable value's section.
162 mutable MCSection *Section;
164 /// Value - If non-null, the value for a variable symbol.
167 /// IsTemporary - True if this is an assembler temporary label, which
168 /// typically does not survive in the .o file's symbol table. Usually
169 /// "Lfoo" or ".foo".
170 unsigned IsTemporary : 1;
172 /// \brief True if this symbol can be redefined.
173 unsigned IsRedefinable : 1;
175 /// IsUsed - True if this symbol has been used.
176 mutable unsigned IsUsed : 1;
178 mutable bool HasData : 1;
179 mutable MCSymbolData Data;
181 private: // MCContext creates and uniques these.
183 friend class MCContext;
184 MCSymbol(StringRef name, bool isTemporary)
185 : Name(name), Section(nullptr), Value(nullptr), IsTemporary(isTemporary),
186 IsRedefinable(false), IsUsed(false), HasData(false) {}
188 MCSymbol(const MCSymbol &) = delete;
189 void operator=(const MCSymbol &) = delete;
190 MCSection *getSectionPtr() const {
191 if (Section || !Value)
193 return Section = Value->FindAssociatedSection();
197 /// getName - Get the symbol name.
198 StringRef getName() const { return Name; }
200 bool hasData() const { return HasData; }
202 /// Get associated symbol data.
203 MCSymbolData &getData() const {
204 assert(HasData && "Missing symbol data!");
208 /// Initialize symbol data.
210 /// Nothing really to do here, but this is enables an assertion that \a
211 /// MCAssembler::getOrCreateSymbolData() has actually been called before
212 /// anyone calls \a getData().
213 void initializeData() const { HasData = true; }
218 /// isTemporary - Check if this is an assembler temporary symbol.
219 bool isTemporary() const { return IsTemporary; }
221 /// isUsed - Check if this is used.
222 bool isUsed() const { return IsUsed; }
223 void setUsed(bool Value) const { IsUsed = Value; }
225 /// \brief Check if this symbol is redefinable.
226 bool isRedefinable() const { return IsRedefinable; }
227 /// \brief Mark this symbol as redefinable.
228 void setRedefinable(bool Value) { IsRedefinable = Value; }
229 /// \brief Prepare this symbol to be redefined.
230 void redefineIfPossible() {
234 IsRedefinable = false;
239 /// \name Associated Sections
242 /// isDefined - Check if this symbol is defined (i.e., it has an address).
244 /// Defined symbols are either absolute or in some section.
245 bool isDefined() const { return getSectionPtr() != nullptr; }
247 /// isInSection - Check if this symbol is defined in some section (i.e., it
248 /// is defined but not absolute).
249 bool isInSection() const { return isDefined() && !isAbsolute(); }
251 /// isUndefined - Check if this symbol undefined (i.e., implicitly defined).
252 bool isUndefined() const { return !isDefined(); }
254 /// isAbsolute - Check if this is an absolute symbol.
255 bool isAbsolute() const { return getSectionPtr() == AbsolutePseudoSection; }
257 /// Get the section associated with a defined, non-absolute symbol.
258 MCSection &getSection() const {
259 assert(isInSection() && "Invalid accessor!");
260 return *getSectionPtr();
263 /// Mark the symbol as defined in the section \p S.
264 void setSection(MCSection &S) {
265 assert(!isVariable() && "Cannot set section of variable");
269 /// setUndefined - Mark the symbol as undefined.
270 void setUndefined() { Section = nullptr; }
273 /// \name Variable Symbols
276 /// isVariable - Check if this is a variable symbol.
277 bool isVariable() const { return Value != nullptr; }
279 /// getVariableValue() - Get the value for variable symbols.
280 const MCExpr *getVariableValue() const {
281 assert(isVariable() && "Invalid accessor!");
286 void setVariableValue(const MCExpr *Value);
290 /// print - Print the value to the stream \p OS.
291 void print(raw_ostream &OS) const;
293 /// dump - Print the value to stderr.
297 inline raw_ostream &operator<<(raw_ostream &OS, const MCSymbol &Sym) {
301 } // end namespace llvm