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"
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"
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.
48 bool CSKYInstPrinter::applyTargetSpecificCLOption(StringRef Opt) {
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 
61 void CSKYInstPrinter::printInst(const MCInst *MI, uint64_t Address,
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 }
102