blob: 7eba5e0317c6285bb9b77492742267f35c7e297e [file] [log] [blame]
Daniel Dunbar1a019d82009-08-14 03:41:23 +00001//===- 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"
Matt Arsenault8b643552015-06-09 00:31:39 +000011#include "llvm/MC/MCAsmInfo.h"
Pete Cooper234b8752015-06-09 18:36:13 +000012#include "llvm/MC/MCContext.h"
Daniel Dunbar85574262010-05-05 19:00:56 +000013#include "llvm/MC/MCExpr.h"
David Greenecdf04522010-01-05 01:28:10 +000014#include "llvm/Support/Debug.h"
Matt Arsenault8b643552015-06-09 00:31:39 +000015#include "llvm/Support/ErrorHandling.h"
Daniel Dunbar1a019d82009-08-14 03:41:23 +000016#include "llvm/Support/raw_ostream.h"
Daniel Dunbar1a019d82009-08-14 03:41:23 +000017using namespace llvm;
18
Daniel Dunbar6860ac72009-08-22 07:22:36 +000019// Sentinel value for the absolute pseudo section.
Rafael Espindola0709a7b2015-05-21 19:20:38 +000020MCSection *MCSymbol::AbsolutePseudoSection = reinterpret_cast<MCSection *>(1);
Daniel Dunbar6860ac72009-08-22 07:22:36 +000021
Pete Cooper234b8752015-06-09 18:36:13 +000022void *MCSymbol::operator new(size_t s, NameEntryTy *Name, MCContext &Ctx) {
23 size_t Size = s + (Name ? sizeof(Name) : 0);
24
25 // For safety, ensure that the alignment of a pointer is enough for an
26 // MCSymbol. This also ensures we don't need padding between the name and
27 // symbol.
28 assert(alignOf<MCSymbol>() <= alignof(NameEntryTy *) &&
29 "Bad alignment of MCSymbol");
30 void *Storage = Ctx.allocate(Size, alignof(NameEntryTy *));
31 NameEntryTy **Start = static_cast<NameEntryTy**>(Storage);
32 NameEntryTy **End = Start + (Name ? 1 : 0);
33 return End;
34}
35
Daniel Dunbar85574262010-05-05 19:00:56 +000036void MCSymbol::setVariableValue(const MCExpr *Value) {
Rafael Espindola46c79ef2010-11-15 14:40:36 +000037 assert(!IsUsed && "Cannot set a variable that has already been used.");
Daniel Dunbar85574262010-05-05 19:00:56 +000038 assert(Value && "Invalid variable value!");
Daniel Dunbar85574262010-05-05 19:00:56 +000039 this->Value = Value;
Pete Cooper11472c02015-06-08 18:41:57 +000040 SectionOrFragment = nullptr;
Daniel Dunbar85574262010-05-05 19:00:56 +000041}
42
Matt Arsenault8b643552015-06-09 00:31:39 +000043void MCSymbol::print(raw_ostream &OS, const MCAsmInfo *MAI) const {
Chris Lattner83e872e2010-01-17 19:23:46 +000044 // The name for this MCSymbol is required to be a valid target name. However,
45 // some targets support quoting names with funny characters. If the name
46 // contains a funny character, then print it quoted.
Rafael Espindolafe4e0882013-11-14 06:05:49 +000047 StringRef Name = getName();
Matt Arsenault8b643552015-06-09 00:31:39 +000048 if (!MAI || MAI->isValidUnquotedName(Name)) {
Rafael Espindolafe4e0882013-11-14 06:05:49 +000049 OS << Name;
Chris Lattner3d6c8eb2009-09-13 18:04:46 +000050 return;
51 }
Jim Grosbachdc1e36e2012-05-11 01:41:30 +000052
Matt Arsenault8b643552015-06-09 00:31:39 +000053 if (MAI && !MAI->supportsNameQuoting())
54 report_fatal_error("Symbol name with unsupported characters");
55
Rafael Espindolafe4e0882013-11-14 06:05:49 +000056 OS << '"';
Matt Arsenault8b643552015-06-09 00:31:39 +000057 for (char C : Name) {
Rafael Espindolafe4e0882013-11-14 06:05:49 +000058 if (C == '\n')
59 OS << "\\n";
60 else if (C == '"')
61 OS << "\\\"";
62 else
63 OS << C;
64 }
65 OS << '"';
Daniel Dunbar1a019d82009-08-14 03:41:23 +000066}
67
Manman Ren49d684e2012-09-12 05:06:18 +000068#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Rafael Espindolaf4a13652015-05-27 13:05:42 +000069void MCSymbol::dump() const { dbgs() << *this; }
Manman Renc3366cc2012-09-06 19:55:56 +000070#endif