Fix PR 23525 - Separate header mass propagation in irregular loops.
[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 section.
20 MCSection *MCSymbol::AbsolutePseudoSection = reinterpret_cast<MCSection *>(1);
21
22 void *MCSymbol::operator new(size_t s, const StringMapEntry<bool> *Name,
23                              MCContext &Ctx) {
24   // We may need more space for a Name to account for alignment.  So allocate
25   // space for the storage type and not the name pointer.
26   size_t Size = s + (Name ? sizeof(NameEntryStorageTy) : 0);
27
28   // For safety, ensure that the alignment of a pointer is enough for an
29   // MCSymbol.  This also ensures we don't need padding between the name and
30   // symbol.
31   static_assert((unsigned)AlignOf<MCSymbol>::Alignment <=
32                 AlignOf<NameEntryStorageTy>::Alignment,
33                 "Bad alignment of MCSymbol");
34   void *Storage = Ctx.allocate(Size, alignOf<NameEntryStorageTy>());
35   NameEntryStorageTy *Start = static_cast<NameEntryStorageTy*>(Storage);
36   NameEntryStorageTy *End = Start + (Name ? 1 : 0);
37   return End;
38 }
39
40 void MCSymbol::setVariableValue(const MCExpr *Value) {
41   assert(!IsUsed && "Cannot set a variable that has already been used.");
42   assert(Value && "Invalid variable value!");
43   this->Value = Value;
44   SectionOrFragment = nullptr;
45 }
46
47 void MCSymbol::print(raw_ostream &OS, const MCAsmInfo *MAI) const {
48   // The name for this MCSymbol is required to be a valid target name.  However,
49   // some targets support quoting names with funny characters.  If the name
50   // contains a funny character, then print it quoted.
51   StringRef Name = getName();
52   if (!MAI || MAI->isValidUnquotedName(Name)) {
53     OS << Name;
54     return;
55   }
56
57   if (MAI && !MAI->supportsNameQuoting())
58     report_fatal_error("Symbol name with unsupported characters");
59
60   OS << '"';
61   for (char C : Name) {
62     if (C == '\n')
63       OS << "\\n";
64     else if (C == '"')
65       OS << "\\\"";
66     else
67       OS << C;
68   }
69   OS << '"';
70 }
71
72 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
73 void MCSymbol::dump() const { dbgs() << *this; }
74 #endif