blob: f386c3bc90aca85fc65e8cf1ae96cee334dfcfd7 [file] [log] [blame]
Daniel Dunbar1689e0c2009-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 Dunbar8d627d32010-05-05 19:00:56 +000011#include "llvm/MC/MCExpr.h"
David Greenef24dd5c2010-01-05 01:28:10 +000012#include "llvm/Support/Debug.h"
Daniel Dunbar1689e0c2009-08-14 03:41:23 +000013#include "llvm/Support/raw_ostream.h"
Daniel Dunbar1689e0c2009-08-14 03:41:23 +000014using namespace llvm;
15
Daniel Dunbar8906ff12009-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 Lattnere19f9782009-09-13 18:04:46 +000020static bool isAcceptableChar(char C) {
Hans Wennborgb74b88e2013-10-17 01:13:02 +000021 return (C >= 'a' && C <= 'z') ||
22 (C >= 'A' && C <= 'Z') ||
23 (C >= '0' && C <= '9') ||
24 C == '_' || C == '$' || C == '.';
Chris Lattnere19f9782009-09-13 18:04:46 +000025}
26
Dmitri Gribenkoc5252da2012-09-14 14:57:36 +000027/// NameNeedsQuoting - Return true if the identifier \p Str needs quotes to be
Chris Lattner6edec7b2010-01-17 20:11:03 +000028/// syntactically correct.
29static bool NameNeedsQuoting(StringRef Str) {
Chris Lattnere19f9782009-09-13 18:04:46 +000030 assert(!Str.empty() && "Cannot create an empty MCSymbol");
Jim Grosbach2684d9e2012-05-11 01:41:30 +000031
Chris Lattneraadb35f2009-09-03 05:57:47 +000032 // If any of the characters in the string is an unacceptable character, force
33 // quotes.
Chris Lattnere19f9782009-09-13 18:04:46 +000034 for (unsigned i = 0, e = Str.size(); i != e; ++i)
35 if (!isAcceptableChar(Str[i]))
Daniel Dunbar1689e0c2009-08-14 03:41:23 +000036 return true;
Daniel Dunbar1689e0c2009-08-14 03:41:23 +000037 return false;
38}
39
Rafael Espindola94ed5fc2010-11-15 16:33:49 +000040const MCSymbol &MCSymbol::AliasedSymbol() const {
41 const MCSymbol *S = this;
42 while (S->isVariable()) {
43 const MCExpr *Value = S->getVariableValue();
44 if (Value->getKind() != MCExpr::SymbolRef)
45 return *S;
46 const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr*>(Value);
47 S = &Ref->getSymbol();
48 }
49 return *S;
50}
51
Daniel Dunbar8d627d32010-05-05 19:00:56 +000052void MCSymbol::setVariableValue(const MCExpr *Value) {
Rafael Espindoladb9835d2010-11-15 14:40:36 +000053 assert(!IsUsed && "Cannot set a variable that has already been used.");
Daniel Dunbar8d627d32010-05-05 19:00:56 +000054 assert(Value && "Invalid variable value!");
Daniel Dunbar8d627d32010-05-05 19:00:56 +000055 this->Value = Value;
Daniel Dunbar2d7fd612010-05-05 19:01:05 +000056
Daniel Dunbar90604ab2011-04-29 18:20:17 +000057 // Variables should always be marked as in the same "section" as the value.
58 const MCSection *Section = Value->FindAssociatedSection();
Jim Grosbach93a1a0d2012-03-20 21:33:17 +000059 if (Section)
Daniel Dunbar90604ab2011-04-29 18:20:17 +000060 setSection(*Section);
Jim Grosbach93a1a0d2012-03-20 21:33:17 +000061 else
Daniel Dunbar90604ab2011-04-29 18:20:17 +000062 setUndefined();
Daniel Dunbar8d627d32010-05-05 19:00:56 +000063}
64
Chris Lattner10b318b2010-01-17 21:43:43 +000065void MCSymbol::print(raw_ostream &OS) const {
Chris Lattneracd03ae2010-01-17 19:23:46 +000066 // The name for this MCSymbol is required to be a valid target name. However,
67 // some targets support quoting names with funny characters. If the name
68 // contains a funny character, then print it quoted.
Chris Lattner6edec7b2010-01-17 20:11:03 +000069 if (!NameNeedsQuoting(getName())) {
Daniel Dunbar1689e0c2009-08-14 03:41:23 +000070 OS << getName();
Chris Lattnere19f9782009-09-13 18:04:46 +000071 return;
72 }
Jim Grosbach2684d9e2012-05-11 01:41:30 +000073
Chris Lattner4afcedf2009-09-13 18:11:09 +000074 OS << '"' << getName() << '"';
Daniel Dunbar1689e0c2009-08-14 03:41:23 +000075}
76
Manman Ren286c4dc2012-09-12 05:06:18 +000077#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Daniel Dunbar1689e0c2009-08-14 03:41:23 +000078void MCSymbol::dump() const {
Chris Lattner10b318b2010-01-17 21:43:43 +000079 print(dbgs());
Daniel Dunbar1689e0c2009-08-14 03:41:23 +000080}
Manman Rencc77eec2012-09-06 19:55:56 +000081#endif