1 //===-- CSKYInstPrinter.cpp - Convert CSKY MCInst to asm syntax ---------===//
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
7 //===----------------------------------------------------------------------===//
9 // This class prints an CSKY MCInst to a .s file.
11 //===----------------------------------------------------------------------===//
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"
18 #include "llvm/MC/MCSubtargetInfo.h"
19 #include "llvm/MC/MCSymbol.h"
20 #include "llvm/Support/CommandLine.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/FormattedStream.h"
26 #define DEBUG_TYPE "csky-asm-printer"
28 // Include the auto-generated portion of the assembly writer.
29 #define PRINT_ALIAS_INSTR
30 #include "CSKYGenAsmWriter.inc"
33 NoAliases("csky-no-aliases",
34 cl::desc("Disable the emission of assembler pseudo instructions"),
35 cl::init(false), cl::Hidden
);
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
);
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
48 bool CSKYInstPrinter::applyTargetSpecificCLOption(StringRef Opt
) {
49 if (Opt
== "no-aliases") {
53 if (Opt
== "numeric") {
61 void CSKYInstPrinter::printInst(const MCInst
*MI
, uint64_t Address
,
62 StringRef Annot
, const MCSubtargetInfo
&STI
,
64 const MCInst
*NewMI
= MI
;
66 if (NoAliases
|| !printAliasInstr(NewMI
, Address
, STI
, O
))
67 printInstruction(NewMI
, Address
, STI
, O
);
68 printAnnotation(O
, Annot
);
71 void CSKYInstPrinter::printRegName(raw_ostream
&O
, unsigned RegNo
) const {
72 O
<< getRegisterName(RegNo
);
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
);
82 if (MO
.getReg() == CSKY::C
)
85 printRegName(O
, MO
.getReg());
90 O
<< formatImm(MO
.getImm());
94 assert(MO
.isExpr() && "Unknown operand kind in printOperand");
95 MO
.getExpr()->print(O
, &MAI
);
98 const char *CSKYInstPrinter::getRegisterName(unsigned RegNo
) {
99 return getRegisterName(RegNo
, ArchRegNames
? CSKY::NoRegAltName
100 : CSKY::ABIRegAltName
);