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 const MCSymbol &getSymbol() const { return *Symbol; }
76 MCFragment *getFragment() const { return Fragment.getPointer(); }
77 void setFragment(MCFragment *Value) { Fragment.setPointer(Value); }
79 uint64_t getOffset() const {
83 void setOffset(uint64_t Value) {
89 /// \name Symbol Attributes
92 bool isExternal() const { return Fragment.getInt() & 1; }
93 void setExternal(bool Value) {
94 Fragment.setInt((Fragment.getInt() & ~1) | unsigned(Value));
97 bool isPrivateExtern() const { return Fragment.getInt() & 2; }
98 void setPrivateExtern(bool Value) {
99 Fragment.setInt((Fragment.getInt() & ~2) | (unsigned(Value) << 1));
102 /// isCommon - Is this a 'common' symbol.
103 bool isCommon() const { return CommonAlign != -1U; }
105 /// setCommon - Mark this symbol as being 'common'.
107 /// \param Size - The size of the symbol.
108 /// \param Align - The alignment of the symbol.
109 void setCommon(uint64_t Size, unsigned Align) {
110 assert(getOffset() == 0);
115 /// getCommonSize - Return the size of a 'common' symbol.
116 uint64_t getCommonSize() const {
117 assert(isCommon() && "Not a 'common' symbol!");
121 void setSize(const MCExpr *SS) { SymbolSize = SS; }
123 const MCExpr *getSize() const { return SymbolSize; }
125 /// getCommonAlignment - Return the alignment of a 'common' symbol.
126 unsigned getCommonAlignment() const {
127 assert(isCommon() && "Not a 'common' symbol!");
131 /// getFlags - Get the (implementation defined) symbol flags.
132 uint32_t getFlags() const { return Flags; }
134 /// setFlags - Set the (implementation defined) symbol flags.
135 void setFlags(uint32_t Value) { Flags = Value; }
137 /// modifyFlags - Modify the flags via a mask
138 void modifyFlags(uint32_t Value, uint32_t Mask) {
139 Flags = (Flags & ~Mask) | Value;
142 /// getIndex - Get the (implementation defined) index.
143 uint64_t getIndex() const { return Index; }
145 /// setIndex - Set the (implementation defined) index.
146 void setIndex(uint64_t Value) { Index = Value; }
153 /// MCSymbol - Instances of this class represent a symbol name in the MC file,
154 /// and MCSymbols are created and uniqued by the MCContext class. MCSymbols
155 /// should only be constructed with valid names for the object file.
157 /// If the symbol is defined/emitted into the current translation unit, the
158 /// Section member is set to indicate what section it lives in. Otherwise, if
159 /// it is a reference to an external entity, it has a null section.
161 // Special sentinal value for the absolute pseudo section.
163 // FIXME: Use a PointerInt wrapper for this?
164 static const MCSection *AbsolutePseudoSection;
166 /// Name - The name of the symbol. The referred-to string data is actually
167 /// held by the StringMap that lives in MCContext.
170 /// Section - The section the symbol is defined in. This is null for
171 /// undefined symbols, and the special AbsolutePseudoSection value for
172 /// absolute symbols. If this is a variable symbol, this caches the
173 /// variable value's section.
174 mutable const MCSection *Section;
176 /// Value - If non-null, the value for a variable symbol.
179 /// IsTemporary - True if this is an assembler temporary label, which
180 /// typically does not survive in the .o file's symbol table. Usually
181 /// "Lfoo" or ".foo".
182 unsigned IsTemporary : 1;
184 /// \brief True if this symbol can be redefined.
185 unsigned IsRedefinable : 1;
187 /// IsUsed - True if this symbol has been used.
188 mutable unsigned IsUsed : 1;
190 mutable MCSymbolData Data;
192 private: // MCContext creates and uniques these.
194 friend class MCContext;
195 MCSymbol(StringRef name, bool isTemporary)
196 : Name(name), Section(nullptr), Value(nullptr), IsTemporary(isTemporary),
197 IsRedefinable(false), IsUsed(false) {}
199 MCSymbol(const MCSymbol &) = delete;
200 void operator=(const MCSymbol &) = delete;
201 const MCSection *getSectionPtr() const {
202 if (Section || !Value)
204 return Section = Value->FindAssociatedSection();
208 /// getName - Get the symbol name.
209 StringRef getName() const { return Name; }
211 /// Get associated symbol data.
212 MCSymbolData &getData() const {
213 assert(Data.isInitialized() && "Missing symbol data!");
217 /// Get unsafe symbol data (even if uninitialized).
219 /// Don't assert on uninitialized data; just return it.
220 MCSymbolData &getUnsafeData() const { return Data; }
225 /// isTemporary - Check if this is an assembler temporary symbol.
226 bool isTemporary() const { return IsTemporary; }
228 /// isUsed - Check if this is used.
229 bool isUsed() const { return IsUsed; }
230 void setUsed(bool Value) const { IsUsed = Value; }
232 /// \brief Check if this symbol is redefinable.
233 bool isRedefinable() const { return IsRedefinable; }
234 /// \brief Mark this symbol as redefinable.
235 void setRedefinable(bool Value) { IsRedefinable = Value; }
236 /// \brief Prepare this symbol to be redefined.
237 void redefineIfPossible() {
241 IsRedefinable = false;
246 /// \name Associated Sections
249 /// isDefined - Check if this symbol is defined (i.e., it has an address).
251 /// Defined symbols are either absolute or in some section.
252 bool isDefined() const { return getSectionPtr() != nullptr; }
254 /// isInSection - Check if this symbol is defined in some section (i.e., it
255 /// is defined but not absolute).
256 bool isInSection() const { return isDefined() && !isAbsolute(); }
258 /// isUndefined - Check if this symbol undefined (i.e., implicitly defined).
259 bool isUndefined() const { return !isDefined(); }
261 /// isAbsolute - Check if this is an absolute symbol.
262 bool isAbsolute() const { return getSectionPtr() == AbsolutePseudoSection; }
264 /// getSection - Get the section associated with a defined, non-absolute
266 const MCSection &getSection() const {
267 assert(isInSection() && "Invalid accessor!");
268 return *getSectionPtr();
271 /// setSection - Mark the symbol as defined in the section \p S.
272 void setSection(const MCSection &S) {
273 assert(!isVariable() && "Cannot set section of variable");
277 /// setUndefined - Mark the symbol as undefined.
278 void setUndefined() { Section = nullptr; }
281 /// \name Variable Symbols
284 /// isVariable - Check if this is a variable symbol.
285 bool isVariable() const { return Value != nullptr; }
287 /// getVariableValue() - Get the value for variable symbols.
288 const MCExpr *getVariableValue() const {
289 assert(isVariable() && "Invalid accessor!");
294 void setVariableValue(const MCExpr *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