blob: 8d07b7605ceaa9a6b00ac39e7d5e9ca197ae9296 [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"
Daniel Dunbar85574262010-05-05 19:00:56 +000012#include "llvm/MC/MCExpr.h"
David Greenecdf04522010-01-05 01:28:10 +000013#include "llvm/Support/Debug.h"
Matt Arsenault8b643552015-06-09 00:31:39 +000014#include "llvm/Support/ErrorHandling.h"
Daniel Dunbar1a019d82009-08-14 03:41:23 +000015#include "llvm/Support/raw_ostream.h"
Daniel Dunbar1a019d82009-08-14 03:41:23 +000016using namespace llvm;
17
Daniel Dunbar6860ac72009-08-22 07:22:36 +000018// Sentinel value for the absolute pseudo section.
Rafael Espindola0709a7b2015-05-21 19:20:38 +000019MCSection *MCSymbol::AbsolutePseudoSection = reinterpret_cast<MCSection *>(1);
Daniel Dunbar6860ac72009-08-22 07:22:36 +000020
Daniel Dunbar85574262010-05-05 19:00:56 +000021void MCSymbol::setVariableValue(const MCExpr *Value) {
Rafael Espindola46c79ef2010-11-15 14:40:36 +000022 assert(!IsUsed && "Cannot set a variable that has already been used.");
Daniel Dunbar85574262010-05-05 19:00:56 +000023 assert(Value && "Invalid variable value!");
Daniel Dunbar85574262010-05-05 19:00:56 +000024 this->Value = Value;
Pete Cooper11472c02015-06-08 18:41:57 +000025 SectionOrFragment = nullptr;
Daniel Dunbar85574262010-05-05 19:00:56 +000026}
27
Matt Arsenault8b643552015-06-09 00:31:39 +000028void MCSymbol::print(raw_ostream &OS, const MCAsmInfo *MAI) const {
Chris Lattner83e872e2010-01-17 19:23:46 +000029 // The name for this MCSymbol is required to be a valid target name. However,
30 // some targets support quoting names with funny characters. If the name
31 // contains a funny character, then print it quoted.
Rafael Espindolafe4e0882013-11-14 06:05:49 +000032 StringRef Name = getName();
Matt Arsenault8b643552015-06-09 00:31:39 +000033 if (!MAI || MAI->isValidUnquotedName(Name)) {
Rafael Espindolafe4e0882013-11-14 06:05:49 +000034 OS << Name;
Chris Lattner3d6c8eb2009-09-13 18:04:46 +000035 return;
36 }
Jim Grosbachdc1e36e2012-05-11 01:41:30 +000037
Matt Arsenault8b643552015-06-09 00:31:39 +000038 if (MAI && !MAI->supportsNameQuoting())
39 report_fatal_error("Symbol name with unsupported characters");
40
Rafael Espindolafe4e0882013-11-14 06:05:49 +000041 OS << '"';
Matt Arsenault8b643552015-06-09 00:31:39 +000042 for (char C : Name) {
Rafael Espindolafe4e0882013-11-14 06:05:49 +000043 if (C == '\n')
44 OS << "\\n";
45 else if (C == '"')
46 OS << "\\\"";
47 else
48 OS << C;
49 }
50 OS << '"';
Daniel Dunbar1a019d82009-08-14 03:41:23 +000051}
52
Manman Ren49d684e2012-09-12 05:06:18 +000053#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Rafael Espindolaf4a13652015-05-27 13:05:42 +000054void MCSymbol::dump() const { dbgs() << *this; }
Manman Renc3366cc2012-09-06 19:55:56 +000055#endif