LLVM  15.0.0git
MCSymbol.cpp
Go to the documentation of this file.
1 //===- lib/MC/MCSymbol.cpp - MCSymbol implementation ----------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/MC/MCSymbol.h"
10 #include "llvm/ADT/StringRef.h"
11 #include "llvm/Config/llvm-config.h"
12 #include "llvm/MC/MCAsmInfo.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCFragment.h"
15 #include "llvm/Support/Compiler.h"
16 #include "llvm/Support/Debug.h"
19 #include <cassert>
20 #include <cstddef>
21 
22 using namespace llvm;
23 
24 // Only the address of this fragment is ever actually used.
25 static MCDummyFragment SentinelFragment(nullptr);
26 
27 // Sentinel value for the absolute pseudo fragment.
29 
30 void *MCSymbol::operator new(size_t s, const StringMapEntry<bool> *Name,
31  MCContext &Ctx) {
32  // We may need more space for a Name to account for alignment. So allocate
33  // space for the storage type and not the name pointer.
34  size_t Size = s + (Name ? sizeof(NameEntryStorageTy) : 0);
35 
36  // For safety, ensure that the alignment of a pointer is enough for an
37  // MCSymbol. This also ensures we don't need padding between the name and
38  // symbol.
39  static_assert((unsigned)alignof(MCSymbol) <= alignof(NameEntryStorageTy),
40  "Bad alignment of MCSymbol");
41  void *Storage = Ctx.allocate(Size, alignof(NameEntryStorageTy));
42  NameEntryStorageTy *Start = static_cast<NameEntryStorageTy*>(Storage);
43  NameEntryStorageTy *End = Start + (Name ? 1 : 0);
44  return End;
45 }
46 
48  assert(!IsUsed && "Cannot set a variable that has already been used.");
49  assert(Value && "Invalid variable value!");
52  "Cannot give common/offset symbol a variable value");
53  this->Value = Value;
55  setUndefined();
56 }
57 
58 void MCSymbol::print(raw_ostream &OS, const MCAsmInfo *MAI) const {
59  // The name for this MCSymbol is required to be a valid target name. However,
60  // some targets support quoting names with funny characters. If the name
61  // contains a funny character, then print it quoted.
63  if (!MAI || MAI->isValidUnquotedName(Name)) {
64  OS << Name;
65  return;
66  }
67 
68  if (MAI && !MAI->supportsNameQuoting())
69  report_fatal_error("Symbol name with unsupported characters");
70 
71  OS << '"';
72  for (char C : Name) {
73  if (C == '\n')
74  OS << "\\n";
75  else if (C == '"')
76  OS << "\\\"";
77  else
78  OS << C;
79  }
80  OS << '"';
81 }
82 
83 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
85  dbgs() << *this;
86 }
87 #endif
llvm::Check::Size
@ Size
Definition: FileCheck.h:77
LLVM_DUMP_METHOD
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition: Compiler.h:494
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::MCSymbol
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
llvm::StringMapEntry
StringMapEntry - This is used to represent one value that is inserted into a StringMap.
Definition: StringMapEntry.h:101
llvm::MCContext
Context object for machine code objects.
Definition: MCContext.h:76
StringRef.h
llvm::MCAsmInfo
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:56
llvm::MCSymbol::AbsolutePseudoFragment
static MCFragment * AbsolutePseudoFragment
Definition: MCSymbol.h:66
ErrorHandling.h
MCFragment.h
llvm::MCSymbol::SymContentsUnset
@ SymContentsUnset
Definition: MCSymbol.h:58
llvm::MCSymbol::setUndefined
void setUndefined()
Mark the symbol as undefined.
Definition: MCSymbol.h:274
llvm::MCSymbol::IsUsed
unsigned IsUsed
IsUsed - True if this symbol has been used.
Definition: MCSymbol.h:94
llvm::MCSymbol::SymbolContents
unsigned SymbolContents
This is actually a Contents enumerator, but is unsigned to avoid sign extension and achieve better bi...
Definition: MCSymbol.h:114
llvm::MCSymbol::setVariableValue
void setVariableValue(const MCExpr *Value)
Definition: MCSymbol.cpp:47
llvm::MCSymbol::dump
void dump() const
dump - Print the value to stderr.
Definition: MCSymbol.cpp:84
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
llvm::MCFragment
Definition: MCFragment.h:30
llvm::MCDummyFragment
Definition: MCFragment.h:114
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
MCContext.h
MCSymbol.h
llvm::report_fatal_error
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:143
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:54
llvm::MCSymbol::getName
StringRef getName() const
getName - Get the symbol name.
Definition: MCSymbol.h:198
llvm::MCSymbol::print
void print(raw_ostream &OS, const MCAsmInfo *MAI) const
print - Print the value to the stream OS.
Definition: MCSymbol.cpp:58
llvm::MCSymbol::Value
const MCExpr * Value
If non-null, the value for a variable symbol.
Definition: MCSymbol.h:140
llvm::MCSymbol::NameEntryStorageTy
union { const StringMapEntry< bool > *NameEntry NameEntryStorageTy
The name for a symbol.
Definition: MCSymbol.h:152
s
multiplies can be turned into SHL s
Definition: README.txt:370
llvm::MCAsmInfo::isValidUnquotedName
virtual bool isValidUnquotedName(StringRef Name) const
Return true if the identifier Name does not need quotes to be syntactically correct.
Definition: MCAsmInfo.cpp:123
llvm::MCAsmInfo::supportsNameQuoting
bool supportsNameQuoting() const
Definition: MCAsmInfo.h:699
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
MCAsmInfo.h
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
Compiler.h
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:50
llvm::MCSymbol::SymContentsVariable
@ SymContentsVariable
Definition: MCSymbol.h:60
raw_ostream.h
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::MCExpr
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
Debug.h
SentinelFragment
static MCDummyFragment SentinelFragment(nullptr)