xref: /freebsd-src/contrib/llvm-project/llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.cpp (revision fe6060f10f634930ff71b7c50291ddc610da2475)
1*fe6060f1SDimitry Andric //===-- CSKYInstPrinter.cpp - Convert CSKY MCInst to asm syntax ---------===//
2*fe6060f1SDimitry Andric //
3*fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*fe6060f1SDimitry Andric //
7*fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
8*fe6060f1SDimitry Andric //
9*fe6060f1SDimitry Andric // This class prints an CSKY MCInst to a .s file.
10*fe6060f1SDimitry Andric //
11*fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
12*fe6060f1SDimitry Andric 
13*fe6060f1SDimitry Andric #include "CSKYInstPrinter.h"
14*fe6060f1SDimitry Andric #include "llvm/MC/MCAsmInfo.h"
15*fe6060f1SDimitry Andric #include "llvm/MC/MCExpr.h"
16*fe6060f1SDimitry Andric #include "llvm/MC/MCInst.h"
17*fe6060f1SDimitry Andric #include "llvm/MC/MCRegisterInfo.h"
18*fe6060f1SDimitry Andric #include "llvm/MC/MCSubtargetInfo.h"
19*fe6060f1SDimitry Andric #include "llvm/MC/MCSymbol.h"
20*fe6060f1SDimitry Andric #include "llvm/Support/CommandLine.h"
21*fe6060f1SDimitry Andric #include "llvm/Support/ErrorHandling.h"
22*fe6060f1SDimitry Andric #include "llvm/Support/FormattedStream.h"
23*fe6060f1SDimitry Andric 
24*fe6060f1SDimitry Andric using namespace llvm;
25*fe6060f1SDimitry Andric 
26*fe6060f1SDimitry Andric #define DEBUG_TYPE "csky-asm-printer"
27*fe6060f1SDimitry Andric 
28*fe6060f1SDimitry Andric // Include the auto-generated portion of the assembly writer.
29*fe6060f1SDimitry Andric #define PRINT_ALIAS_INSTR
30*fe6060f1SDimitry Andric #include "CSKYGenAsmWriter.inc"
31*fe6060f1SDimitry Andric 
32*fe6060f1SDimitry Andric static cl::opt<bool>
33*fe6060f1SDimitry Andric     NoAliases("csky-no-aliases",
34*fe6060f1SDimitry Andric               cl::desc("Disable the emission of assembler pseudo instructions"),
35*fe6060f1SDimitry Andric               cl::init(false), cl::Hidden);
36*fe6060f1SDimitry Andric 
37*fe6060f1SDimitry Andric static cl::opt<bool>
38*fe6060f1SDimitry Andric     ArchRegNames("csky-arch-reg-names",
39*fe6060f1SDimitry Andric                  cl::desc("Print architectural register names rather than the "
40*fe6060f1SDimitry Andric                           "ABI names (such as r14 instead of sp)"),
41*fe6060f1SDimitry Andric                  cl::init(false), cl::Hidden);
42*fe6060f1SDimitry Andric 
43*fe6060f1SDimitry Andric // The command-line flags above are used by llvm-mc and llc. They can be used by
44*fe6060f1SDimitry Andric // `llvm-objdump`, but we override their values here to handle options passed to
45*fe6060f1SDimitry Andric // `llvm-objdump` with `-M` (which matches GNU objdump). There did not seem to
46*fe6060f1SDimitry Andric // be an easier way to allow these options in all these tools, without doing it
47*fe6060f1SDimitry Andric // this way.
48*fe6060f1SDimitry Andric bool CSKYInstPrinter::applyTargetSpecificCLOption(StringRef Opt) {
49*fe6060f1SDimitry Andric   if (Opt == "no-aliases") {
50*fe6060f1SDimitry Andric     NoAliases = true;
51*fe6060f1SDimitry Andric     return true;
52*fe6060f1SDimitry Andric   }
53*fe6060f1SDimitry Andric   if (Opt == "numeric") {
54*fe6060f1SDimitry Andric     ArchRegNames = true;
55*fe6060f1SDimitry Andric     return true;
56*fe6060f1SDimitry Andric   }
57*fe6060f1SDimitry Andric 
58*fe6060f1SDimitry Andric   return false;
59*fe6060f1SDimitry Andric }
60*fe6060f1SDimitry Andric 
61*fe6060f1SDimitry Andric void CSKYInstPrinter::printInst(const MCInst *MI, uint64_t Address,
62*fe6060f1SDimitry Andric                                 StringRef Annot, const MCSubtargetInfo &STI,
63*fe6060f1SDimitry Andric                                 raw_ostream &O) {
64*fe6060f1SDimitry Andric   const MCInst *NewMI = MI;
65*fe6060f1SDimitry Andric 
66*fe6060f1SDimitry Andric   if (NoAliases || !printAliasInstr(NewMI, Address, STI, O))
67*fe6060f1SDimitry Andric     printInstruction(NewMI, Address, STI, O);
68*fe6060f1SDimitry Andric   printAnnotation(O, Annot);
69*fe6060f1SDimitry Andric }
70*fe6060f1SDimitry Andric 
71*fe6060f1SDimitry Andric void CSKYInstPrinter::printRegName(raw_ostream &O, unsigned RegNo) const {
72*fe6060f1SDimitry Andric   O << getRegisterName(RegNo);
73*fe6060f1SDimitry Andric }
74*fe6060f1SDimitry Andric 
75*fe6060f1SDimitry Andric void CSKYInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
76*fe6060f1SDimitry Andric                                    const MCSubtargetInfo &STI, raw_ostream &O,
77*fe6060f1SDimitry Andric                                    const char *Modifier) {
78*fe6060f1SDimitry Andric   assert((Modifier == 0 || Modifier[0] == 0) && "No modifiers supported");
79*fe6060f1SDimitry Andric   const MCOperand &MO = MI->getOperand(OpNo);
80*fe6060f1SDimitry Andric 
81*fe6060f1SDimitry Andric   if (MO.isReg()) {
82*fe6060f1SDimitry Andric     if (MO.getReg() == CSKY::C)
83*fe6060f1SDimitry Andric       O << "";
84*fe6060f1SDimitry Andric     else
85*fe6060f1SDimitry Andric       printRegName(O, MO.getReg());
86*fe6060f1SDimitry Andric     return;
87*fe6060f1SDimitry Andric   }
88*fe6060f1SDimitry Andric 
89*fe6060f1SDimitry Andric   if (MO.isImm()) {
90*fe6060f1SDimitry Andric     O << formatImm(MO.getImm());
91*fe6060f1SDimitry Andric     return;
92*fe6060f1SDimitry Andric   }
93*fe6060f1SDimitry Andric 
94*fe6060f1SDimitry Andric   assert(MO.isExpr() && "Unknown operand kind in printOperand");
95*fe6060f1SDimitry Andric   MO.getExpr()->print(O, &MAI);
96*fe6060f1SDimitry Andric }
97*fe6060f1SDimitry Andric 
98*fe6060f1SDimitry Andric const char *CSKYInstPrinter::getRegisterName(unsigned RegNo) {
99*fe6060f1SDimitry Andric   return getRegisterName(RegNo, ArchRegNames ? CSKY::NoRegAltName
100*fe6060f1SDimitry Andric                                              : CSKY::ABIRegAltName);
101*fe6060f1SDimitry Andric }
102