blob: 0009decef9b41d580bf4bd79290f856645b4cb1d [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.
Rafael Espindola0709a7b2015-05-21 19:20:38 +000017MCSection *MCSymbol::AbsolutePseudoSection = reinterpret_cast<MCSection *>(1);
Daniel Dunbar6860ac72009-08-22 07:22:36 +000018
Chris Lattner3d6c8eb2009-09-13 18:04:46 +000019static bool isAcceptableChar(char C) {
Hans Wennborg7ddcdc82013-10-18 02:14:40 +000020 if ((C < 'a' || C > 'z') &&
21 (C < 'A' || C > 'Z') &&
22 (C < '0' || C > '9') &&
23 C != '_' && C != '$' && C != '.' && C != '@')
24 return false;
25 return true;
Chris Lattner3d6c8eb2009-09-13 18:04:46 +000026}
27
Dmitri Gribenko5485acd2012-09-14 14:57:36 +000028/// NameNeedsQuoting - Return true if the identifier \p Str needs quotes to be
Chris Lattner043e6562010-01-17 20:11:03 +000029/// syntactically correct.
30static bool NameNeedsQuoting(StringRef Str) {
Chris Lattner3d6c8eb2009-09-13 18:04:46 +000031 assert(!Str.empty() && "Cannot create an empty MCSymbol");
Jim Grosbachdc1e36e2012-05-11 01:41:30 +000032
Chris Lattner19454532009-09-03 05:57:47 +000033 // If any of the characters in the string is an unacceptable character, force
34 // quotes.
Chris Lattner3d6c8eb2009-09-13 18:04:46 +000035 for (unsigned i = 0, e = Str.size(); i != e; ++i)
36 if (!isAcceptableChar(Str[i]))
Daniel Dunbar1a019d82009-08-14 03:41:23 +000037 return true;
Daniel Dunbar1a019d82009-08-14 03:41:23 +000038 return false;
39}
40
Daniel Dunbar85574262010-05-05 19:00:56 +000041void MCSymbol::setVariableValue(const MCExpr *Value) {
Rafael Espindola46c79ef2010-11-15 14:40:36 +000042 assert(!IsUsed && "Cannot set a variable that has already been used.");
Daniel Dunbar85574262010-05-05 19:00:56 +000043 assert(Value && "Invalid variable value!");
Daniel Dunbar85574262010-05-05 19:00:56 +000044 this->Value = Value;
Peter Collingbourne10d362c2015-04-03 01:46:11 +000045 this->Section = nullptr;
Daniel Dunbar85574262010-05-05 19:00:56 +000046}
47
Chris Lattner8b5d55e2010-01-17 21:43:43 +000048void MCSymbol::print(raw_ostream &OS) const {
Chris Lattner83e872e2010-01-17 19:23:46 +000049 // The name for this MCSymbol is required to be a valid target name. However,
50 // some targets support quoting names with funny characters. If the name
51 // contains a funny character, then print it quoted.
Rafael Espindolafe4e0882013-11-14 06:05:49 +000052 StringRef Name = getName();
Matt Arsenault9897e032015-04-23 23:34:51 +000053 if (!NameNeedsQuoting(Name)) {
Rafael Espindolafe4e0882013-11-14 06:05:49 +000054 OS << Name;
Chris Lattner3d6c8eb2009-09-13 18:04:46 +000055 return;
56 }
Jim Grosbachdc1e36e2012-05-11 01:41:30 +000057
Rafael Espindolafe4e0882013-11-14 06:05:49 +000058 OS << '"';
59 for (unsigned I = 0, E = Name.size(); I != E; ++I) {
60 char C = Name[I];
61 if (C == '\n')
62 OS << "\\n";
63 else if (C == '"')
64 OS << "\\\"";
65 else
66 OS << C;
67 }
68 OS << '"';
Daniel Dunbar1a019d82009-08-14 03:41:23 +000069}
70
Manman Ren49d684e2012-09-12 05:06:18 +000071#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Daniel Dunbar1a019d82009-08-14 03:41:23 +000072void MCSymbol::dump() const {
Chris Lattner8b5d55e2010-01-17 21:43:43 +000073 print(dbgs());
Daniel Dunbar1a019d82009-08-14 03:41:23 +000074}
Manman Renc3366cc2012-09-06 19:55:56 +000075#endif