the various ConstantExpr::get*Ty methods existed to work with issues around
[llvm/stm8.git] / lib / MC / MCSymbol.cpp
blobc2fad1674aa4308e769ae6db771771ea1db3d34a
1 //===- 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 //===----------------------------------------------------------------------===//
10 #include "llvm/MC/MCSymbol.h"
11 #include "llvm/MC/MCExpr.h"
12 #include "llvm/Support/Debug.h"
13 #include "llvm/Support/raw_ostream.h"
14 using namespace llvm;
16 // Sentinel value for the absolute pseudo section.
17 const MCSection *MCSymbol::AbsolutePseudoSection =
18 reinterpret_cast<const MCSection *>(1);
20 static 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 != '@')
25 return false;
26 return true;
29 /// NameNeedsQuoting - Return true if the identifier \arg Str needs quotes to be
30 /// syntactically correct.
31 static bool NameNeedsQuoting(StringRef Str) {
32 assert(!Str.empty() && "Cannot create an empty MCSymbol");
34 // If any of the characters in the string is an unacceptable character, force
35 // quotes.
36 for (unsigned i = 0, e = Str.size(); i != e; ++i)
37 if (!isAcceptableChar(Str[i]))
38 return true;
39 return false;
42 const 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();
51 return *S;
54 void MCSymbol::setVariableValue(const MCExpr *Value) {
55 assert(!IsUsed && "Cannot set a variable that has already been used.");
56 assert(Value && "Invalid variable value!");
57 assert((isUndefined() || (isAbsolute() && isa<MCConstantExpr>(Value))) &&
58 "Invalid redefinition!");
59 this->Value = Value;
61 // 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();
70 void MCSymbol::print(raw_ostream &OS) const {
71 // 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.
74 if (!NameNeedsQuoting(getName())) {
75 OS << getName();
76 return;
79 OS << '"' << getName() << '"';
82 void MCSymbol::dump() const {
83 print(dbgs());