xref: /freebsd/contrib/llvm-project/llvm/lib/Target/CSKY/MCTargetDesc/CSKYInstPrinter.cpp (revision 5e801ac66d24704442eba426ed13c3effb8a34e7)
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/ADT/StringExtras.h"
15 #include "llvm/MC/MCAsmInfo.h"
16 #include "llvm/MC/MCExpr.h"
17 #include "llvm/MC/MCInst.h"
18 #include "llvm/MC/MCRegisterInfo.h"
19 #include "llvm/MC/MCSubtargetInfo.h"
20 #include "llvm/MC/MCSymbol.h"
21 #include "llvm/Support/CommandLine.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/FormattedStream.h"
24 
25 using namespace llvm;
26 
27 #define DEBUG_TYPE "csky-asm-printer"
28 
29 // Include the auto-generated portion of the assembly writer.
30 #define PRINT_ALIAS_INSTR
31 #include "CSKYGenAsmWriter.inc"
32 
33 static cl::opt<bool>
34     NoAliases("csky-no-aliases",
35               cl::desc("Disable the emission of assembler pseudo instructions"),
36               cl::init(false), cl::Hidden);
37 
38 static cl::opt<bool>
39     ArchRegNames("csky-arch-reg-names",
40                  cl::desc("Print architectural register names rather than the "
41                           "ABI names (such as r14 instead of sp)"),
42                  cl::init(false), cl::Hidden);
43 
44 // The command-line flags above are used by llvm-mc and llc. They can be used by
45 // `llvm-objdump`, but we override their values here to handle options passed to
46 // `llvm-objdump` with `-M` (which matches GNU objdump). There did not seem to
47 // be an easier way to allow these options in all these tools, without doing it
48 // this way.
49 bool CSKYInstPrinter::applyTargetSpecificCLOption(StringRef Opt) {
50   if (Opt == "no-aliases") {
51     NoAliases = true;
52     return true;
53   }
54   if (Opt == "numeric") {
55     ArchRegNames = true;
56     return true;
57   }
58 
59   return false;
60 }
61 
62 void CSKYInstPrinter::printInst(const MCInst *MI, uint64_t Address,
63                                 StringRef Annot, const MCSubtargetInfo &STI,
64                                 raw_ostream &O) {
65   const MCInst *NewMI = MI;
66 
67   if (NoAliases || !printAliasInstr(NewMI, Address, STI, O))
68     printInstruction(NewMI, Address, STI, O);
69   printAnnotation(O, Annot);
70 }
71 
72 void CSKYInstPrinter::printRegName(raw_ostream &O, unsigned RegNo) const {
73   O << getRegisterName(RegNo);
74 }
75 
76 void CSKYInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
77                                    const MCSubtargetInfo &STI, raw_ostream &O,
78                                    const char *Modifier) {
79   assert((Modifier == 0 || Modifier[0] == 0) && "No modifiers supported");
80   const MCOperand &MO = MI->getOperand(OpNo);
81 
82   if (MO.isReg()) {
83     if (MO.getReg() == CSKY::C)
84       O << "";
85     else
86       printRegName(O, MO.getReg());
87     return;
88   }
89 
90   if (MO.isImm()) {
91     O << formatImm(MO.getImm());
92     return;
93   }
94 
95   assert(MO.isExpr() && "Unknown operand kind in printOperand");
96   MO.getExpr()->print(O, &MAI);
97 }
98 
99 void CSKYInstPrinter::printDataSymbol(const MCInst *MI, unsigned OpNo,
100                                       const MCSubtargetInfo &STI,
101                                       raw_ostream &O) {
102   const MCOperand &MO = MI->getOperand(OpNo);
103 
104   O << "[";
105   if (MO.isImm())
106     O << MO.getImm();
107   else
108     MO.getExpr()->print(O, &MAI);
109   O << "]";
110 }
111 
112 void CSKYInstPrinter::printConstpool(const MCInst *MI, uint64_t Address,
113                                      unsigned OpNo, const MCSubtargetInfo &STI,
114                                      raw_ostream &O) {
115   const MCOperand &MO = MI->getOperand(OpNo);
116 
117   if (MO.isImm()) {
118     if (PrintBranchImmAsAddress) {
119       uint64_t Target = Address + MO.getImm();
120       Target &= 0xfffffffc;
121       O << formatHex(Target);
122     } else {
123       O << MO.getImm();
124     }
125     return;
126   }
127 
128   assert(MO.isExpr() && "Unknown operand kind in printConstpool");
129 
130   O << "[";
131   MO.getExpr()->print(O, &MAI);
132   O << "]";
133 }
134 
135 void CSKYInstPrinter::printCSKYSymbolOperand(const MCInst *MI, uint64_t Address,
136                                              unsigned OpNo,
137                                              const MCSubtargetInfo &STI,
138                                              raw_ostream &O) {
139   const MCOperand &MO = MI->getOperand(OpNo);
140   if (!MO.isImm()) {
141     return printOperand(MI, OpNo, STI, O);
142   }
143 
144   if (PrintBranchImmAsAddress) {
145     uint64_t Target = Address + MO.getImm();
146     Target &= 0xffffffff;
147     O << formatHex(Target);
148   } else {
149     O << MO.getImm();
150   }
151 }
152 
153 void CSKYInstPrinter::printRegisterSeq(const MCInst *MI, unsigned OpNum,
154                                        const MCSubtargetInfo &STI,
155                                        raw_ostream &O) {
156   printRegName(O, MI->getOperand(OpNum).getReg());
157   O << "-";
158   printRegName(O, MI->getOperand(OpNum + 1).getReg());
159 }
160 
161 void CSKYInstPrinter::printRegisterList(const MCInst *MI, unsigned OpNum,
162                                         const MCSubtargetInfo &STI,
163                                         raw_ostream &O) {
164   auto V = MI->getOperand(OpNum).getImm();
165   ListSeparator LS;
166 
167   if (V & 0xf) {
168     O << LS;
169     printRegName(O, CSKY::R4);
170     auto Offset = (V & 0xf) - 1;
171     if (Offset) {
172       O << "-";
173       printRegName(O, CSKY::R4 + Offset);
174     }
175   }
176 
177   if ((V >> 4) & 0x1) {
178     O << LS;
179     printRegName(O, CSKY::R15);
180   }
181 
182   if ((V >> 5) & 0x7) {
183     O << LS;
184     printRegName(O, CSKY::R16);
185 
186     auto Offset = ((V >> 5) & 0x7) - 1;
187 
188     if (Offset) {
189       O << "-";
190       printRegName(O, CSKY::R16 + Offset);
191     }
192   }
193 
194   if ((V >> 8) & 0x1) {
195     O << LS;
196     printRegName(O, CSKY::R28);
197   }
198 }
199 
200 const char *CSKYInstPrinter::getRegisterName(unsigned RegNo) {
201   return getRegisterName(RegNo, ArchRegNames ? CSKY::NoRegAltName
202                                              : CSKY::ABIRegAltName);
203 }
204