2009-08-14 03:41:23 +00: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-05 19:00:56 +00:00
|
|
|
#include "llvm/MC/MCExpr.h"
|
2010-01-05 01:28:10 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2009-08-14 03:41:23 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
2009-08-22 07:22:36 +00:00
|
|
|
// Sentinel value for the absolute pseudo section.
|
|
|
|
const MCSection *MCSymbol::AbsolutePseudoSection =
|
|
|
|
reinterpret_cast<const MCSection *>(1);
|
|
|
|
|
2009-09-13 18:04:46 +00:00
|
|
|
static bool isAcceptableChar(char C) {
|
|
|
|
if ((C < 'a' || C > 'z') &&
|
|
|
|
(C < 'A' || C > 'Z') &&
|
|
|
|
(C < '0' || C > '9') &&
|
|
|
|
C != '_' && C != '$' && C != '.' && C != '@')
|
2009-09-03 05:57:47 +00:00
|
|
|
return false;
|
2009-09-13 18:04:46 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-01-17 20:11:03 +00:00
|
|
|
/// NameNeedsQuoting - Return true if the identifier \arg Str needs quotes to be
|
|
|
|
/// syntactically correct.
|
|
|
|
static bool NameNeedsQuoting(StringRef Str) {
|
2009-09-13 18:04:46 +00:00
|
|
|
assert(!Str.empty() && "Cannot create an empty MCSymbol");
|
2009-09-03 05:57:47 +00:00
|
|
|
|
|
|
|
// If any of the characters in the string is an unacceptable character, force
|
|
|
|
// quotes.
|
2009-09-13 18:04:46 +00:00
|
|
|
for (unsigned i = 0, e = Str.size(); i != e; ++i)
|
|
|
|
if (!isAcceptableChar(Str[i]))
|
2009-08-14 03:41:23 +00:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-11-15 16:33:49 +00:00
|
|
|
const MCSymbol &MCSymbol::AliasedSymbol() const {
|
|
|
|
const MCSymbol *S = this;
|
|
|
|
while (S->isVariable()) {
|
|
|
|
const MCExpr *Value = S->getVariableValue();
|
|
|
|
if (Value->getKind() != MCExpr::SymbolRef)
|
|
|
|
return *S;
|
|
|
|
const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr*>(Value);
|
|
|
|
S = &Ref->getSymbol();
|
|
|
|
}
|
|
|
|
return *S;
|
|
|
|
}
|
|
|
|
|
2010-05-05 19:00:56 +00:00
|
|
|
void MCSymbol::setVariableValue(const MCExpr *Value) {
|
2010-11-15 14:40:36 +00:00
|
|
|
assert(!IsUsed && "Cannot set a variable that has already been used.");
|
2010-05-05 19:00:56 +00:00
|
|
|
assert(Value && "Invalid variable value!");
|
|
|
|
this->Value = Value;
|
2010-05-05 19:01:05 +00:00
|
|
|
|
2011-04-29 18:20:17 +00:00
|
|
|
// Variables should always be marked as in the same "section" as the value.
|
|
|
|
const MCSection *Section = Value->FindAssociatedSection();
|
2012-03-20 21:33:17 +00:00
|
|
|
if (Section)
|
2011-04-29 18:20:17 +00:00
|
|
|
setSection(*Section);
|
2012-03-20 21:33:17 +00:00
|
|
|
else
|
2011-04-29 18:20:17 +00:00
|
|
|
setUndefined();
|
2010-05-05 19:00:56 +00:00
|
|
|
}
|
|
|
|
|
2010-01-17 21:43:43 +00:00
|
|
|
void MCSymbol::print(raw_ostream &OS) const {
|
2010-01-17 19:23:46 +00: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.
|
2010-01-17 20:11:03 +00:00
|
|
|
if (!NameNeedsQuoting(getName())) {
|
2009-08-14 03:41:23 +00:00
|
|
|
OS << getName();
|
2009-09-13 18:04:46 +00:00
|
|
|
return;
|
|
|
|
}
|
2009-09-13 18:11:09 +00:00
|
|
|
|
|
|
|
OS << '"' << getName() << '"';
|
2009-08-14 03:41:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MCSymbol::dump() const {
|
2010-01-17 21:43:43 +00:00
|
|
|
print(dbgs());
|
2009-08-14 03:41:23 +00:00
|
|
|
}
|