2009-08-14 11:41:23 +08:00
|
|
|
//===- lib/MC/MCSymbol.cpp - MCSymbol implementation ----------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/MC/MCSymbol.h"
|
2010-05-06 03:00:56 +08:00
|
|
|
#include "llvm/MC/MCExpr.h"
|
2010-01-05 09:28:10 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2009-08-14 11:41:23 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
2009-08-22 15:22:36 +08:00
|
|
|
// Sentinel value for the absolute pseudo section.
|
2015-05-22 03:20:38 +08:00
|
|
|
MCSection *MCSymbol::AbsolutePseudoSection = reinterpret_cast<MCSection *>(1);
|
2009-08-22 15:22:36 +08:00
|
|
|
|
2009-09-14 02:04:46 +08:00
|
|
|
static bool isAcceptableChar(char C) {
|
2013-10-18 10:14:40 +08:00
|
|
|
if ((C < 'a' || C > 'z') &&
|
|
|
|
(C < 'A' || C > 'Z') &&
|
|
|
|
(C < '0' || C > '9') &&
|
|
|
|
C != '_' && C != '$' && C != '.' && C != '@')
|
|
|
|
return false;
|
|
|
|
return true;
|
2009-09-14 02:04:46 +08:00
|
|
|
}
|
|
|
|
|
2012-09-14 22:57:36 +08:00
|
|
|
/// NameNeedsQuoting - Return true if the identifier \p Str needs quotes to be
|
2010-01-18 04:11:03 +08:00
|
|
|
/// syntactically correct.
|
|
|
|
static bool NameNeedsQuoting(StringRef Str) {
|
2009-09-14 02:04:46 +08:00
|
|
|
assert(!Str.empty() && "Cannot create an empty MCSymbol");
|
2012-05-11 09:41:30 +08:00
|
|
|
|
2009-09-03 13:57:47 +08:00
|
|
|
// If any of the characters in the string is an unacceptable character, force
|
|
|
|
// quotes.
|
2009-09-14 02:04:46 +08:00
|
|
|
for (unsigned i = 0, e = Str.size(); i != e; ++i)
|
|
|
|
if (!isAcceptableChar(Str[i]))
|
2009-08-14 11:41:23 +08:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-05-06 03:00:56 +08:00
|
|
|
void MCSymbol::setVariableValue(const MCExpr *Value) {
|
2010-11-15 22:40:36 +08:00
|
|
|
assert(!IsUsed && "Cannot set a variable that has already been used.");
|
2010-05-06 03:00:56 +08:00
|
|
|
assert(Value && "Invalid variable value!");
|
|
|
|
this->Value = Value;
|
2015-06-09 02:41:57 +08:00
|
|
|
SectionOrFragment = nullptr;
|
2010-05-06 03:00:56 +08:00
|
|
|
}
|
|
|
|
|
2010-01-18 05:43:43 +08:00
|
|
|
void MCSymbol::print(raw_ostream &OS) const {
|
2010-01-18 03:23:46 +08:00
|
|
|
// The name for this MCSymbol is required to be a valid target name. However,
|
|
|
|
// some targets support quoting names with funny characters. If the name
|
|
|
|
// contains a funny character, then print it quoted.
|
2013-11-14 14:05:49 +08:00
|
|
|
StringRef Name = getName();
|
2015-05-27 13:12:37 +08:00
|
|
|
if (Name.empty()) {
|
|
|
|
OS << "\"\"";
|
|
|
|
return;
|
|
|
|
}
|
2015-04-24 07:34:51 +08:00
|
|
|
if (!NameNeedsQuoting(Name)) {
|
2013-11-14 14:05:49 +08:00
|
|
|
OS << Name;
|
2009-09-14 02:04:46 +08:00
|
|
|
return;
|
|
|
|
}
|
2012-05-11 09:41:30 +08:00
|
|
|
|
2013-11-14 14:05:49 +08:00
|
|
|
OS << '"';
|
|
|
|
for (unsigned I = 0, E = Name.size(); I != E; ++I) {
|
|
|
|
char C = Name[I];
|
|
|
|
if (C == '\n')
|
|
|
|
OS << "\\n";
|
|
|
|
else if (C == '"')
|
|
|
|
OS << "\\\"";
|
|
|
|
else
|
|
|
|
OS << C;
|
|
|
|
}
|
|
|
|
OS << '"';
|
2009-08-14 11:41:23 +08:00
|
|
|
}
|
|
|
|
|
2012-09-12 13:06:18 +08:00
|
|
|
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
|
2015-05-27 21:05:42 +08:00
|
|
|
void MCSymbol::dump() const { dbgs() << *this; }
|
2012-09-07 03:55:56 +08:00
|
|
|
#endif
|