LLVM  14.0.0git
CSKYInstPrinter.cpp
Go to the documentation of this file.
1 //===-- CSKYInstPrinter.cpp - Convert CSKY MCInst to asm syntax ---------===//
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 // This class prints an CSKY MCInst to a .s file.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "CSKYInstPrinter.h"
14 #include "llvm/MC/MCAsmInfo.h"
15 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCInst.h"
17 #include "llvm/MC/MCRegisterInfo.h"
19 #include "llvm/MC/MCSymbol.h"
23 
24 using namespace llvm;
25 
26 #define DEBUG_TYPE "csky-asm-printer"
27 
28 // Include the auto-generated portion of the assembly writer.
29 #define PRINT_ALIAS_INSTR
30 #include "CSKYGenAsmWriter.inc"
31 
32 static cl::opt<bool>
33  NoAliases("csky-no-aliases",
34  cl::desc("Disable the emission of assembler pseudo instructions"),
35  cl::init(false), cl::Hidden);
36 
37 static cl::opt<bool>
38  ArchRegNames("csky-arch-reg-names",
39  cl::desc("Print architectural register names rather than the "
40  "ABI names (such as r14 instead of sp)"),
41  cl::init(false), cl::Hidden);
42 
43 // The command-line flags above are used by llvm-mc and llc. They can be used by
44 // `llvm-objdump`, but we override their values here to handle options passed to
45 // `llvm-objdump` with `-M` (which matches GNU objdump). There did not seem to
46 // be an easier way to allow these options in all these tools, without doing it
47 // this way.
49  if (Opt == "no-aliases") {
50  NoAliases = true;
51  return true;
52  }
53  if (Opt == "numeric") {
54  ArchRegNames = true;
55  return true;
56  }
57 
58  return false;
59 }
60 
62  StringRef Annot, const MCSubtargetInfo &STI,
63  raw_ostream &O) {
64  const MCInst *NewMI = MI;
65 
66  if (NoAliases || !printAliasInstr(NewMI, Address, STI, O))
67  printInstruction(NewMI, Address, STI, O);
68  printAnnotation(O, Annot);
69 }
70 
71 void CSKYInstPrinter::printRegName(raw_ostream &O, unsigned RegNo) const {
72  O << getRegisterName(RegNo);
73 }
74 
75 void CSKYInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
76  const MCSubtargetInfo &STI, raw_ostream &O,
77  const char *Modifier) {
78  assert((Modifier == 0 || Modifier[0] == 0) && "No modifiers supported");
79  const MCOperand &MO = MI->getOperand(OpNo);
80 
81  if (MO.isReg()) {
82  if (MO.getReg() == CSKY::C)
83  O << "";
84  else
85  printRegName(O, MO.getReg());
86  return;
87  }
88 
89  if (MO.isImm()) {
90  O << formatImm(MO.getImm());
91  return;
92  }
93 
94  assert(MO.isExpr() && "Unknown operand kind in printOperand");
95  MO.getExpr()->print(O, &MAI);
96 }
97 
98 const char *CSKYInstPrinter::getRegisterName(unsigned RegNo) {
99  return getRegisterName(RegNo, ArchRegNames ? CSKY::NoRegAltName
100  : CSKY::ABIRegAltName);
101 }
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:103
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::MCOperand::isReg
bool isReg() const
Definition: MCInst.h:61
ErrorHandling.h
llvm::CSKYInstPrinter::printInstruction
void printInstruction(const MCInst *MI, uint64_t Address, const MCSubtargetInfo &STI, raw_ostream &O)
llvm::cl::Hidden
@ Hidden
Definition: CommandLine.h:143
llvm::MCInst
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:184
llvm::CSKYInstPrinter::printAliasInstr
bool printAliasInstr(const MCInst *MI, uint64_t Address, const MCSubtargetInfo &STI, raw_ostream &O)
CommandLine.h
FormattedStream.h
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::MCOperand::getImm
int64_t getImm() const
Definition: MCInst.h:80
MCSymbol.h
ArchRegNames
static cl::opt< bool > ArchRegNames("csky-arch-reg-names", cl::desc("Print architectural register names rather than the " "ABI names (such as r14 instead of sp)"), cl::init(false), cl::Hidden)
MCInst.h
MCSubtargetInfo.h
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::MCInstPrinter::printAnnotation
void printAnnotation(raw_ostream &OS, StringRef Annot)
Utility function for printing annotations.
Definition: MCInstPrinter.cpp:49
llvm::cl::opt< bool >
llvm::CSKYInstPrinter::applyTargetSpecificCLOption
bool applyTargetSpecificCLOption(StringRef Opt) override
Customize the printer according to a command line option.
Definition: CSKYInstPrinter.cpp:48
llvm::RISCVFenceField::O
@ O
Definition: RISCVBaseInfo.h:192
llvm::MCOperand::isImm
bool isImm() const
Definition: MCInst.h:62
uint64_t
llvm::cl::init
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:443
MCRegisterInfo.h
llvm::CSKYInstPrinter::getRegisterName
static const char * getRegisterName(unsigned RegNo)
Definition: CSKYInstPrinter.cpp:98
llvm::HighlightColor::Address
@ Address
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::MCInstPrinter::formatImm
format_object< int64_t > formatImm(int64_t Value) const
Utility function to print immediates in decimal or hex.
Definition: MCInstPrinter.h:134
MCAsmInfo.h
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
CSKYInstPrinter.h
llvm::CSKYInstPrinter::printOperand
void printOperand(const MCInst *MI, unsigned OpNo, const MCSubtargetInfo &STI, raw_ostream &O, const char *Modifier=nullptr)
Definition: CSKYInstPrinter.cpp:75
llvm::MCOperand::getExpr
const MCExpr * getExpr() const
Definition: MCInst.h:114
llvm::CSKYInstPrinter::printRegName
void printRegName(raw_ostream &O, unsigned RegNo) const override
Print the assembler register name.
Definition: CSKYInstPrinter.cpp:71
llvm::MCOperand::isExpr
bool isExpr() const
Definition: MCInst.h:65
llvm::MCInstPrinter::MAI
const MCAsmInfo & MAI
Definition: MCInstPrinter.h:49
llvm::MCExpr::print
void print(raw_ostream &OS, const MCAsmInfo *MAI, bool InParens=false) const
Definition: MCExpr.cpp:42
NoAliases
static cl::opt< bool > NoAliases("csky-no-aliases", cl::desc("Disable the emission of assembler pseudo instructions"), cl::init(false), cl::Hidden)
llvm::MCOperand
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:36
llvm::CSKYInstPrinter::printInst
void printInst(const MCInst *MI, uint64_t Address, StringRef Annot, const MCSubtargetInfo &STI, raw_ostream &O) override
Print the specified MCInst to the specified raw_ostream.
Definition: CSKYInstPrinter.cpp:61
llvm::cl::desc
Definition: CommandLine.h:414
MCExpr.h
llvm::MCSubtargetInfo
Generic base class for all target subtargets.
Definition: MCSubtargetInfo.h:75
llvm::MCOperand::getReg
unsigned getReg() const
Returns the register number.
Definition: MCInst.h:69