blob: c2fad1674aa4308e769ae6db771771ea1db3d34a [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) {
21 if ((C < 'a' || C > 'z') &&
22 (C < 'A' || C > 'Z') &&
23 (C < '0' || C > '9') &&
24 C != '_' && C != '$' && C != '.' && C != '@')
Chris Lattneraadb35f2009-09-03 05:57:47 +000025 return false;
Chris Lattnere19f9782009-09-13 18:04:46 +000026 return true;
27}
28
Chris Lattner6edec7b2010-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 Lattnere19f9782009-09-13 18:04:46 +000032 assert(!Str.empty() && "Cannot create an empty MCSymbol");
Chris Lattneraadb35f2009-09-03 05:57:47 +000033
Chris Lattneraadb35f2009-09-03 05:57:47 +000034 // If any of the characters in the string is an unacceptable character, force
35 // quotes.
Chris Lattnere19f9782009-09-13 18:04:46 +000036 for (unsigned i = 0, e = Str.size(); i != e; ++i)
37 if (!isAcceptableChar(Str[i]))
Daniel Dunbar1689e0c2009-08-14 03:41:23 +000038 return true;
Daniel Dunbar1689e0c2009-08-14 03:41:23 +000039 return false;
40}
41
Rafael Espindola94ed5fc2010-11-15 16:33:49 +000042const MCSymbol &MCSymbol::AliasedSymbol() const {
43 const MCSymbol *S = this;
44 while (S->isVariable()) {
45 const MCExpr *Value = S->getVariableValue();
46 if (Value->getKind() != MCExpr::SymbolRef)
47 return *S;
48 const MCSymbolRefExpr *Ref = static_cast<const MCSymbolRefExpr*>(Value);
49 S = &Ref->getSymbol();
50 }
51 return *S;
52}
53
Daniel Dunbar8d627d32010-05-05 19:00:56 +000054void MCSymbol::setVariableValue(const MCExpr *Value) {
Rafael Espindoladb9835d2010-11-15 14:40:36 +000055 assert(!IsUsed && "Cannot set a variable that has already been used.");
Daniel Dunbar8d627d32010-05-05 19:00:56 +000056 assert(Value && "Invalid variable value!");
57 assert((isUndefined() || (isAbsolute() && isa<MCConstantExpr>(Value))) &&
58 "Invalid redefinition!");
59 this->Value = Value;
Daniel Dunbar2d7fd612010-05-05 19:01:05 +000060
Daniel Dunbar90604ab2011-04-29 18:20:17 +000061 // Variables should always be marked as in the same "section" as the value.
62 const MCSection *Section = Value->FindAssociatedSection();
63 if (Section) {
64 setSection(*Section);
65 } else {
66 setUndefined();
67 }
Daniel Dunbar8d627d32010-05-05 19:00:56 +000068}
69
Chris Lattner10b318b2010-01-17 21:43:43 +000070void MCSymbol::print(raw_ostream &OS) const {
Chris Lattneracd03ae2010-01-17 19:23:46 +000071 // The name for this MCSymbol is required to be a valid target name. However,
72 // some targets support quoting names with funny characters. If the name
73 // contains a funny character, then print it quoted.
Chris Lattner6edec7b2010-01-17 20:11:03 +000074 if (!NameNeedsQuoting(getName())) {
Daniel Dunbar1689e0c2009-08-14 03:41:23 +000075 OS << getName();
Chris Lattnere19f9782009-09-13 18:04:46 +000076 return;
77 }
Chris Lattner4afcedf2009-09-13 18:11:09 +000078
79 OS << '"' << getName() << '"';
Daniel Dunbar1689e0c2009-08-14 03:41:23 +000080}
81
82void MCSymbol::dump() const {
Chris Lattner10b318b2010-01-17 21:43:43 +000083 print(dbgs());
Daniel Dunbar1689e0c2009-08-14 03:41:23 +000084}