c7d6213bc192257752b9924e9e1bbfcab7345037
[oota-llvm.git] / lib / MC / MCSymbol.cpp
1 //===- lib/MC/MCSymbol.cpp - MCSymbol implementation ----------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/MC/MCSymbol.h"
11 #include "llvm/MC/MCAsmInfo.h"
12 #include "llvm/MC/MCContext.h"
13 #include "llvm/MC/MCExpr.h"
14 #include "llvm/Support/Debug.h"
15 #include "llvm/Support/ErrorHandling.h"
16 #include "llvm/Support/raw_ostream.h"
17 using namespace llvm;
18
19 // Sentinel value for the absolute pseudo fragment.
20 MCFragment *MCSymbol::AbsolutePseudoFragment =
21     reinterpret_cast<MCFragment *>(4);
22
23 void *MCSymbol::operator new(size_t s, const StringMapEntry<bool> *Name,
24                              MCContext &Ctx) {
25   // We may need more space for a Name to account for alignment.  So allocate
26   // space for the storage type and not the name pointer.
27   size_t Size = s + (Name ? sizeof(NameEntryStorageTy) : 0);
28
29   // For safety, ensure that the alignment of a pointer is enough for an
30   // MCSymbol.  This also ensures we don't need padding between the name and
31   // symbol.
32   static_assert((unsigned)AlignOf<MCSymbol>::Alignment <=
33                 AlignOf<NameEntryStorageTy>::Alignment,
34                 "Bad alignment of MCSymbol");
35   void *Storage = Ctx.allocate(Size, alignOf<NameEntryStorageTy>());
36   NameEntryStorageTy *Start = static_cast<NameEntryStorageTy*>(Storage);
37   NameEntryStorageTy *End = Start + (Name ? 1 : 0);
38   return End;
39 }
40
41 void MCSymbol::setVariableValue(const MCExpr *Value) {
42   assert(!IsUsed && "Cannot set a variable that has already been used.");
43   assert(Value && "Invalid variable value!");
44   assert((SymbolContents == SymContentsUnset ||
45           SymbolContents == SymContentsVariable) &&
46          "Cannot give common/offset symbol a variable value");
47   this->Value = Value;
48   SymbolContents = SymContentsVariable;
49   setUndefined();
50 }
51
52 void MCSymbol::print(raw_ostream &OS, const MCAsmInfo *MAI) const {
53   // The name for this MCSymbol is required to be a valid target name.  However,
54   // some targets support quoting names with funny characters.  If the name
55   // contains a funny character, then print it quoted.
56   StringRef Name = getName();
57   if (!MAI || MAI->isValidUnquotedName(Name)) {
58     OS << Name;
59     return;
60   }
61
62   if (MAI && !MAI->supportsNameQuoting())
63     report_fatal_error("Symbol name with unsupported characters");
64
65   OS << '"';
66   for (char C : Name) {
67     if (C == '\n')
68       OS << "\\n";
69     else if (C == '"')
70       OS << "\\\"";
71     else
72       OS << C;
73   }
74   OS << '"';
75 }
76
77 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
78 void MCSymbol::dump() const { dbgs() << *this; }
79 #endif