blob: ccb9f8def9ef8f31d33ca270b2877bdb198a2010 [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"
Daniel Dunbar85574262010-05-05 19:00:56 +000011#include "llvm/MC/MCExpr.h"
David Greenecdf04522010-01-05 01:28:10 +000012#include "llvm/Support/Debug.h"
Daniel Dunbar1a019d82009-08-14 03:41:23 +000013#include "llvm/Support/raw_ostream.h"
Daniel Dunbar1a019d82009-08-14 03:41:23 +000014using namespace llvm;
15
Daniel Dunbar6860ac72009-08-22 07:22:36 +000016// Sentinel value for the absolute pseudo section.
17const MCSection *MCSymbol::AbsolutePseudoSection =
18 reinterpret_cast<const MCSection *>(1);
19
Chris Lattner3d6c8eb2009-09-13 18:04:46 +000020static bool isAcceptableChar(char C) {
Hans Wennborg7ddcdc82013-10-18 02:14:40 +000021 if ((C < 'a' || C > 'z') &&
22 (C < 'A' || C > 'Z') &&
23 (C < '0' || C > '9') &&
24 C != '_' && C != '$' && C != '.' && C != '@')
25 return false;
26 return true;
Chris Lattner3d6c8eb2009-09-13 18:04:46 +000027}
28
Dmitri Gribenko5485acd2012-09-14 14:57:36 +000029/// NameNeedsQuoting - Return true if the identifier \p Str needs quotes to be
Chris Lattner043e6562010-01-17 20:11:03 +000030/// syntactically correct.
31static bool NameNeedsQuoting(StringRef Str) {
Chris Lattner3d6c8eb2009-09-13 18:04:46 +000032 assert(!Str.empty() && "Cannot create an empty MCSymbol");
Jim Grosbachdc1e36e2012-05-11 01:41:30 +000033
Chris Lattner19454532009-09-03 05:57:47 +000034 // If any of the characters in the string is an unacceptable character, force
35 // quotes.
Chris Lattner3d6c8eb2009-09-13 18:04:46 +000036 for (unsigned i = 0, e = Str.size(); i != e; ++i)
37 if (!isAcceptableChar(Str[i]))
Daniel Dunbar1a019d82009-08-14 03:41:23 +000038 return true;
Daniel Dunbar1a019d82009-08-14 03:41:23 +000039 return false;
40}
41
Daniel Dunbar85574262010-05-05 19:00:56 +000042void MCSymbol::setVariableValue(const MCExpr *Value) {
Rafael Espindola46c79ef2010-11-15 14:40:36 +000043 assert(!IsUsed && "Cannot set a variable that has already been used.");
Daniel Dunbar85574262010-05-05 19:00:56 +000044 assert(Value && "Invalid variable value!");
Daniel Dunbar85574262010-05-05 19:00:56 +000045 this->Value = Value;
Peter Collingbourne10d362c2015-04-03 01:46:11 +000046 this->Section = nullptr;
Daniel Dunbar85574262010-05-05 19:00:56 +000047}
48
Chris Lattner8b5d55e2010-01-17 21:43:43 +000049void MCSymbol::print(raw_ostream &OS) const {
Chris Lattner83e872e2010-01-17 19:23:46 +000050 // The name for this MCSymbol is required to be a valid target name. However,
51 // some targets support quoting names with funny characters. If the name
52 // contains a funny character, then print it quoted.
Rafael Espindolafe4e0882013-11-14 06:05:49 +000053 StringRef Name = getName();
Matt Arsenault9897e032015-04-23 23:34:51 +000054 if (!NameNeedsQuoting(Name)) {
Rafael Espindolafe4e0882013-11-14 06:05:49 +000055 OS << Name;
Chris Lattner3d6c8eb2009-09-13 18:04:46 +000056 return;
57 }
Jim Grosbachdc1e36e2012-05-11 01:41:30 +000058
Rafael Espindolafe4e0882013-11-14 06:05:49 +000059 OS << '"';
60 for (unsigned I = 0, E = Name.size(); I != E; ++I) {
61 char C = Name[I];
62 if (C == '\n')
63 OS << "\\n";
64 else if (C == '"')
65 OS << "\\\"";
66 else
67 OS << C;
68 }
69 OS << '"';
Daniel Dunbar1a019d82009-08-14 03:41:23 +000070}
71
Manman Ren49d684e2012-09-12 05:06:18 +000072#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Daniel Dunbar1a019d82009-08-14 03:41:23 +000073void MCSymbol::dump() const {
Chris Lattner8b5d55e2010-01-17 21:43:43 +000074 print(dbgs());
Daniel Dunbar1a019d82009-08-14 03:41:23 +000075}
Manman Renc3366cc2012-09-06 19:55:56 +000076#endif