blob: bcab23b9fc32599382a54996f039d4c084e88c39 [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;
Pete Cooper11472c02015-06-08 18:41:57 +000045 SectionOrFragment = 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();
Matthias Braune50dff02015-05-27 05:12:37 +000053 if (Name.empty()) {
54 OS << "\"\"";
55 return;
56 }
Matt Arsenault9897e032015-04-23 23:34:51 +000057 if (!NameNeedsQuoting(Name)) {
Rafael Espindolafe4e0882013-11-14 06:05:49 +000058 OS << Name;
Chris Lattner3d6c8eb2009-09-13 18:04:46 +000059 return;
60 }
Jim Grosbachdc1e36e2012-05-11 01:41:30 +000061
Rafael Espindolafe4e0882013-11-14 06:05:49 +000062 OS << '"';
63 for (unsigned I = 0, E = Name.size(); I != E; ++I) {
64 char C = Name[I];
65 if (C == '\n')
66 OS << "\\n";
67 else if (C == '"')
68 OS << "\\\"";
69 else
70 OS << C;
71 }
72 OS << '"';
Daniel Dunbar1a019d82009-08-14 03:41:23 +000073}
74
Manman Ren49d684e2012-09-12 05:06:18 +000075#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Rafael Espindolaf4a13652015-05-27 13:05:42 +000076void MCSymbol::dump() const { dbgs() << *this; }
Manman Renc3366cc2012-09-06 19:55:56 +000077#endif