blob: 07751f7298443e5de4e9e0ea432a954be66114bb [file] [log] [blame]
Daniel Dunbara31e6302009-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 Dunbarf58d7e82010-05-05 19:00:56 +000011#include "llvm/MC/MCExpr.h"
David Greene13abb932010-01-05 01:28:10 +000012#include "llvm/Support/Debug.h"
Daniel Dunbara31e6302009-08-14 03:41:23 +000013#include "llvm/Support/raw_ostream.h"
Daniel Dunbara31e6302009-08-14 03:41:23 +000014using namespace llvm;
15
Daniel Dunbar1c2ee9f2009-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 Lattnere66b5a52009-09-13 18:04:46 +000020static bool isAcceptableChar(char C) {
21 if ((C < 'a' || C > 'z') &&
22 (C < 'A' || C > 'Z') &&
23 (C < '0' || C > '9') &&
24 C != '_' && C != '$' && C != '.' && C != '@')
Chris Lattner648353a2009-09-03 05:57:47 +000025 return false;
Chris Lattnere66b5a52009-09-13 18:04:46 +000026 return true;
27}
28
Chris Lattner165c3a22010-01-17 20:11:03 +000029/// NameNeedsQuoting - Return true if the identifier \arg Str needs quotes to be
30/// syntactically correct.
31static bool NameNeedsQuoting(StringRef Str) {
Chris Lattnere66b5a52009-09-13 18:04:46 +000032 assert(!Str.empty() && "Cannot create an empty MCSymbol");
Chris Lattner648353a2009-09-03 05:57:47 +000033
Chris Lattner648353a2009-09-03 05:57:47 +000034 // If any of the characters in the string is an unacceptable character, force
35 // quotes.
Chris Lattnere66b5a52009-09-13 18:04:46 +000036 for (unsigned i = 0, e = Str.size(); i != e; ++i)
37 if (!isAcceptableChar(Str[i]))
Daniel Dunbara31e6302009-08-14 03:41:23 +000038 return true;
Daniel Dunbara31e6302009-08-14 03:41:23 +000039 return false;
40}
41
Daniel Dunbarf58d7e82010-05-05 19:00:56 +000042void MCSymbol::setVariableValue(const MCExpr *Value) {
43 assert(Value && "Invalid variable value!");
44 assert((isUndefined() || (isAbsolute() && isa<MCConstantExpr>(Value))) &&
45 "Invalid redefinition!");
46 this->Value = Value;
Daniel Dunbar61e53fe2010-05-05 19:01:05 +000047
48 // Mark the variable as absolute as appropriate.
49 if (isa<MCConstantExpr>(Value))
50 setAbsolute();
Daniel Dunbarf58d7e82010-05-05 19:00:56 +000051}
52
Chris Lattnerce409842010-01-17 21:43:43 +000053void MCSymbol::print(raw_ostream &OS) const {
Chris Lattnere295e552010-01-17 19:23:46 +000054 // The name for this MCSymbol is required to be a valid target name. However,
55 // some targets support quoting names with funny characters. If the name
56 // contains a funny character, then print it quoted.
Chris Lattner165c3a22010-01-17 20:11:03 +000057 if (!NameNeedsQuoting(getName())) {
Daniel Dunbara31e6302009-08-14 03:41:23 +000058 OS << getName();
Chris Lattnere66b5a52009-09-13 18:04:46 +000059 return;
60 }
Chris Lattneradfec702009-09-13 18:11:09 +000061
62 OS << '"' << getName() << '"';
Daniel Dunbara31e6302009-08-14 03:41:23 +000063}
64
65void MCSymbol::dump() const {
Chris Lattnerce409842010-01-17 21:43:43 +000066 print(dbgs());
Daniel Dunbara31e6302009-08-14 03:41:23 +000067}