18ba622baSZi Xuan Wu //===-- CSKYInstPrinter.cpp - Convert CSKY MCInst to asm syntax ---------===//
28ba622baSZi Xuan Wu //
38ba622baSZi Xuan Wu // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
48ba622baSZi Xuan Wu // See https://llvm.org/LICENSE.txt for license information.
58ba622baSZi Xuan Wu // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
68ba622baSZi Xuan Wu //
78ba622baSZi Xuan Wu //===----------------------------------------------------------------------===//
88ba622baSZi Xuan Wu //
98ba622baSZi Xuan Wu // This class prints an CSKY MCInst to a .s file.
108ba622baSZi Xuan Wu //
118ba622baSZi Xuan Wu //===----------------------------------------------------------------------===//
128ba622baSZi Xuan Wu #include "CSKYInstPrinter.h"
13*ec642ceeSZi Xuan Wu #include "MCTargetDesc/CSKYBaseInfo.h"
14*ec642ceeSZi Xuan Wu #include "MCTargetDesc/CSKYMCExpr.h"
15*ec642ceeSZi Xuan Wu #include "llvm/ADT/STLExtras.h"
16de10a02fSZi Xuan Wu #include "llvm/ADT/StringExtras.h"
178ba622baSZi Xuan Wu #include "llvm/MC/MCAsmInfo.h"
188ba622baSZi Xuan Wu #include "llvm/MC/MCExpr.h"
198ba622baSZi Xuan Wu #include "llvm/MC/MCInst.h"
20*ec642ceeSZi Xuan Wu #include "llvm/MC/MCInstrInfo.h"
218ba622baSZi Xuan Wu #include "llvm/MC/MCRegisterInfo.h"
22*ec642ceeSZi Xuan Wu #include "llvm/MC/MCSection.h"
238ba622baSZi Xuan Wu #include "llvm/MC/MCSubtargetInfo.h"
248ba622baSZi Xuan Wu #include "llvm/MC/MCSymbol.h"
258ba622baSZi Xuan Wu #include "llvm/Support/CommandLine.h"
26*ec642ceeSZi Xuan Wu #include "llvm/Support/Debug.h"
278ba622baSZi Xuan Wu #include "llvm/Support/ErrorHandling.h"
288ba622baSZi Xuan Wu #include "llvm/Support/FormattedStream.h"
298ba622baSZi Xuan Wu
308ba622baSZi Xuan Wu using namespace llvm;
318ba622baSZi Xuan Wu
328ba622baSZi Xuan Wu #define DEBUG_TYPE "csky-asm-printer"
338ba622baSZi Xuan Wu
348ba622baSZi Xuan Wu // Include the auto-generated portion of the assembly writer.
358ba622baSZi Xuan Wu #define PRINT_ALIAS_INSTR
368ba622baSZi Xuan Wu #include "CSKYGenAsmWriter.inc"
378ba622baSZi Xuan Wu
388ba622baSZi Xuan Wu static cl::opt<bool>
398ba622baSZi Xuan Wu NoAliases("csky-no-aliases",
408ba622baSZi Xuan Wu cl::desc("Disable the emission of assembler pseudo instructions"),
418ba622baSZi Xuan Wu cl::init(false), cl::Hidden);
428ba622baSZi Xuan Wu
438ba622baSZi Xuan Wu static cl::opt<bool>
448ba622baSZi Xuan Wu ArchRegNames("csky-arch-reg-names",
458ba622baSZi Xuan Wu cl::desc("Print architectural register names rather than the "
468ba622baSZi Xuan Wu "ABI names (such as r14 instead of sp)"),
478ba622baSZi Xuan Wu cl::init(false), cl::Hidden);
488ba622baSZi Xuan Wu
498ba622baSZi Xuan Wu // The command-line flags above are used by llvm-mc and llc. They can be used by
508ba622baSZi Xuan Wu // `llvm-objdump`, but we override their values here to handle options passed to
518ba622baSZi Xuan Wu // `llvm-objdump` with `-M` (which matches GNU objdump). There did not seem to
528ba622baSZi Xuan Wu // be an easier way to allow these options in all these tools, without doing it
538ba622baSZi Xuan Wu // this way.
applyTargetSpecificCLOption(StringRef Opt)548ba622baSZi Xuan Wu bool CSKYInstPrinter::applyTargetSpecificCLOption(StringRef Opt) {
558ba622baSZi Xuan Wu if (Opt == "no-aliases") {
568ba622baSZi Xuan Wu NoAliases = true;
578ba622baSZi Xuan Wu return true;
588ba622baSZi Xuan Wu }
598ba622baSZi Xuan Wu if (Opt == "numeric") {
608ba622baSZi Xuan Wu ArchRegNames = true;
618ba622baSZi Xuan Wu return true;
628ba622baSZi Xuan Wu }
63*ec642ceeSZi Xuan Wu if (Opt == "debug") {
64*ec642ceeSZi Xuan Wu DebugFlag = true;
65*ec642ceeSZi Xuan Wu return true;
66*ec642ceeSZi Xuan Wu }
67*ec642ceeSZi Xuan Wu if (Opt == "abi-names") {
68*ec642ceeSZi Xuan Wu ABIRegNames = true;
69*ec642ceeSZi Xuan Wu return true;
70*ec642ceeSZi Xuan Wu }
718ba622baSZi Xuan Wu
728ba622baSZi Xuan Wu return false;
738ba622baSZi Xuan Wu }
748ba622baSZi Xuan Wu
printInst(const MCInst * MI,uint64_t Address,StringRef Annot,const MCSubtargetInfo & STI,raw_ostream & O)758ba622baSZi Xuan Wu void CSKYInstPrinter::printInst(const MCInst *MI, uint64_t Address,
768ba622baSZi Xuan Wu StringRef Annot, const MCSubtargetInfo &STI,
778ba622baSZi Xuan Wu raw_ostream &O) {
788ba622baSZi Xuan Wu const MCInst *NewMI = MI;
798ba622baSZi Xuan Wu
808ba622baSZi Xuan Wu if (NoAliases || !printAliasInstr(NewMI, Address, STI, O))
818ba622baSZi Xuan Wu printInstruction(NewMI, Address, STI, O);
828ba622baSZi Xuan Wu printAnnotation(O, Annot);
838ba622baSZi Xuan Wu }
848ba622baSZi Xuan Wu
printRegName(raw_ostream & O,unsigned RegNo) const858ba622baSZi Xuan Wu void CSKYInstPrinter::printRegName(raw_ostream &O, unsigned RegNo) const {
86*ec642ceeSZi Xuan Wu if (PrintBranchImmAsAddress)
87*ec642ceeSZi Xuan Wu O << getRegisterName(RegNo, ABIRegNames ? CSKY::ABIRegAltName
88*ec642ceeSZi Xuan Wu : CSKY::NoRegAltName);
89*ec642ceeSZi Xuan Wu else
908ba622baSZi Xuan Wu O << getRegisterName(RegNo);
918ba622baSZi Xuan Wu }
928ba622baSZi Xuan Wu
printFPRRegName(raw_ostream & O,unsigned RegNo) const934ad517e6SZi Xuan Wu void CSKYInstPrinter::printFPRRegName(raw_ostream &O, unsigned RegNo) const {
944ad517e6SZi Xuan Wu if (PrintBranchImmAsAddress)
954ad517e6SZi Xuan Wu O << getRegisterName(RegNo, CSKY::NoRegAltName);
964ad517e6SZi Xuan Wu else
974ad517e6SZi Xuan Wu O << getRegisterName(RegNo);
984ad517e6SZi Xuan Wu }
994ad517e6SZi Xuan Wu
printOperand(const MCInst * MI,unsigned OpNo,const MCSubtargetInfo & STI,raw_ostream & O,const char * Modifier)1008ba622baSZi Xuan Wu void CSKYInstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
1018ba622baSZi Xuan Wu const MCSubtargetInfo &STI, raw_ostream &O,
1028ba622baSZi Xuan Wu const char *Modifier) {
1038ba622baSZi Xuan Wu assert((Modifier == 0 || Modifier[0] == 0) && "No modifiers supported");
1048ba622baSZi Xuan Wu const MCOperand &MO = MI->getOperand(OpNo);
1058ba622baSZi Xuan Wu
1068ba622baSZi Xuan Wu if (MO.isReg()) {
107*ec642ceeSZi Xuan Wu unsigned Reg = MO.getReg();
108*ec642ceeSZi Xuan Wu bool useABIName = false;
109*ec642ceeSZi Xuan Wu if (PrintBranchImmAsAddress)
110*ec642ceeSZi Xuan Wu useABIName = ABIRegNames;
1118ba622baSZi Xuan Wu else
112*ec642ceeSZi Xuan Wu useABIName = !ArchRegNames;
113*ec642ceeSZi Xuan Wu
114*ec642ceeSZi Xuan Wu if (Reg == CSKY::C)
115*ec642ceeSZi Xuan Wu O << "";
116*ec642ceeSZi Xuan Wu else if (STI.getFeatureBits()[CSKY::FeatureJAVA]) {
117*ec642ceeSZi Xuan Wu if (Reg == CSKY::R23)
118*ec642ceeSZi Xuan Wu O << (useABIName ? "fp" : "r23");
119*ec642ceeSZi Xuan Wu else if (Reg == CSKY::R24)
120*ec642ceeSZi Xuan Wu O << (useABIName ? "top" : "r24");
121*ec642ceeSZi Xuan Wu else if (Reg == CSKY::R25)
122*ec642ceeSZi Xuan Wu O << (useABIName ? "bsp" : "r25");
123*ec642ceeSZi Xuan Wu else
124*ec642ceeSZi Xuan Wu printRegName(O, Reg);
125*ec642ceeSZi Xuan Wu } else
126*ec642ceeSZi Xuan Wu printRegName(O, Reg);
127*ec642ceeSZi Xuan Wu
1288ba622baSZi Xuan Wu return;
1298ba622baSZi Xuan Wu }
1308ba622baSZi Xuan Wu
1318ba622baSZi Xuan Wu if (MO.isImm()) {
132*ec642ceeSZi Xuan Wu uint64_t TSFlags = MII.get(MI->getOpcode()).TSFlags;
133*ec642ceeSZi Xuan Wu
134*ec642ceeSZi Xuan Wu if (((TSFlags & CSKYII::AddrModeMask) != CSKYII::AddrModeNone) &&
135*ec642ceeSZi Xuan Wu PrintBranchImmAsAddress)
136*ec642ceeSZi Xuan Wu O << formatHex(MO.getImm());
137*ec642ceeSZi Xuan Wu else
138*ec642ceeSZi Xuan Wu O << MO.getImm();
1398ba622baSZi Xuan Wu return;
1408ba622baSZi Xuan Wu }
1418ba622baSZi Xuan Wu
1428ba622baSZi Xuan Wu assert(MO.isExpr() && "Unknown operand kind in printOperand");
1438ba622baSZi Xuan Wu MO.getExpr()->print(O, &MAI);
1448ba622baSZi Xuan Wu }
1458ba622baSZi Xuan Wu
printDataSymbol(const MCInst * MI,unsigned OpNo,const MCSubtargetInfo & STI,raw_ostream & O)146de10a02fSZi Xuan Wu void CSKYInstPrinter::printDataSymbol(const MCInst *MI, unsigned OpNo,
147de10a02fSZi Xuan Wu const MCSubtargetInfo &STI,
148de10a02fSZi Xuan Wu raw_ostream &O) {
149de10a02fSZi Xuan Wu const MCOperand &MO = MI->getOperand(OpNo);
150de10a02fSZi Xuan Wu
151de10a02fSZi Xuan Wu O << "[";
152de10a02fSZi Xuan Wu if (MO.isImm())
153de10a02fSZi Xuan Wu O << MO.getImm();
154de10a02fSZi Xuan Wu else
155de10a02fSZi Xuan Wu MO.getExpr()->print(O, &MAI);
156de10a02fSZi Xuan Wu O << "]";
157de10a02fSZi Xuan Wu }
158de10a02fSZi Xuan Wu
printConstpool(const MCInst * MI,uint64_t Address,unsigned OpNo,const MCSubtargetInfo & STI,raw_ostream & O)159de10a02fSZi Xuan Wu void CSKYInstPrinter::printConstpool(const MCInst *MI, uint64_t Address,
160de10a02fSZi Xuan Wu unsigned OpNo, const MCSubtargetInfo &STI,
161de10a02fSZi Xuan Wu raw_ostream &O) {
162de10a02fSZi Xuan Wu const MCOperand &MO = MI->getOperand(OpNo);
163de10a02fSZi Xuan Wu
164de10a02fSZi Xuan Wu if (MO.isImm()) {
165de10a02fSZi Xuan Wu if (PrintBranchImmAsAddress) {
166de10a02fSZi Xuan Wu uint64_t Target = Address + MO.getImm();
167de10a02fSZi Xuan Wu Target &= 0xfffffffc;
168de10a02fSZi Xuan Wu O << formatHex(Target);
169de10a02fSZi Xuan Wu } else {
170de10a02fSZi Xuan Wu O << MO.getImm();
171de10a02fSZi Xuan Wu }
172de10a02fSZi Xuan Wu return;
173de10a02fSZi Xuan Wu }
174de10a02fSZi Xuan Wu
175de10a02fSZi Xuan Wu assert(MO.isExpr() && "Unknown operand kind in printConstpool");
176de10a02fSZi Xuan Wu
177de10a02fSZi Xuan Wu O << "[";
178de10a02fSZi Xuan Wu MO.getExpr()->print(O, &MAI);
179de10a02fSZi Xuan Wu O << "]";
180de10a02fSZi Xuan Wu }
181de10a02fSZi Xuan Wu
printCSKYSymbolOperand(const MCInst * MI,uint64_t Address,unsigned OpNo,const MCSubtargetInfo & STI,raw_ostream & O)182de10a02fSZi Xuan Wu void CSKYInstPrinter::printCSKYSymbolOperand(const MCInst *MI, uint64_t Address,
183de10a02fSZi Xuan Wu unsigned OpNo,
184de10a02fSZi Xuan Wu const MCSubtargetInfo &STI,
185de10a02fSZi Xuan Wu raw_ostream &O) {
186de10a02fSZi Xuan Wu const MCOperand &MO = MI->getOperand(OpNo);
187de10a02fSZi Xuan Wu if (!MO.isImm()) {
188de10a02fSZi Xuan Wu return printOperand(MI, OpNo, STI, O);
189de10a02fSZi Xuan Wu }
190de10a02fSZi Xuan Wu
191de10a02fSZi Xuan Wu if (PrintBranchImmAsAddress) {
192de10a02fSZi Xuan Wu uint64_t Target = Address + MO.getImm();
193de10a02fSZi Xuan Wu Target &= 0xffffffff;
194de10a02fSZi Xuan Wu O << formatHex(Target);
195de10a02fSZi Xuan Wu } else {
196de10a02fSZi Xuan Wu O << MO.getImm();
197de10a02fSZi Xuan Wu }
198de10a02fSZi Xuan Wu }
199de10a02fSZi Xuan Wu
printPSRFlag(const MCInst * MI,unsigned OpNo,const MCSubtargetInfo & STI,raw_ostream & O)200*ec642ceeSZi Xuan Wu void CSKYInstPrinter::printPSRFlag(const MCInst *MI, unsigned OpNo,
201*ec642ceeSZi Xuan Wu const MCSubtargetInfo &STI, raw_ostream &O) {
202*ec642ceeSZi Xuan Wu auto V = MI->getOperand(OpNo).getImm();
203*ec642ceeSZi Xuan Wu
204*ec642ceeSZi Xuan Wu ListSeparator LS;
205*ec642ceeSZi Xuan Wu
206*ec642ceeSZi Xuan Wu if ((V >> 3) & 0x1)
207*ec642ceeSZi Xuan Wu O << LS << "ee";
208*ec642ceeSZi Xuan Wu if ((V >> 2) & 0x1)
209*ec642ceeSZi Xuan Wu O << LS << "ie";
210*ec642ceeSZi Xuan Wu if ((V >> 1) & 0x1)
211*ec642ceeSZi Xuan Wu O << LS << "fe";
212*ec642ceeSZi Xuan Wu if ((V >> 0) & 0x1)
213*ec642ceeSZi Xuan Wu O << LS << "af";
214*ec642ceeSZi Xuan Wu }
215*ec642ceeSZi Xuan Wu
printRegisterSeq(const MCInst * MI,unsigned OpNum,const MCSubtargetInfo & STI,raw_ostream & O)216de10a02fSZi Xuan Wu void CSKYInstPrinter::printRegisterSeq(const MCInst *MI, unsigned OpNum,
217de10a02fSZi Xuan Wu const MCSubtargetInfo &STI,
218de10a02fSZi Xuan Wu raw_ostream &O) {
219de10a02fSZi Xuan Wu printRegName(O, MI->getOperand(OpNum).getReg());
220de10a02fSZi Xuan Wu O << "-";
221de10a02fSZi Xuan Wu printRegName(O, MI->getOperand(OpNum + 1).getReg());
222de10a02fSZi Xuan Wu }
223de10a02fSZi Xuan Wu
printRegisterList(const MCInst * MI,unsigned OpNum,const MCSubtargetInfo & STI,raw_ostream & O)224de10a02fSZi Xuan Wu void CSKYInstPrinter::printRegisterList(const MCInst *MI, unsigned OpNum,
225de10a02fSZi Xuan Wu const MCSubtargetInfo &STI,
226de10a02fSZi Xuan Wu raw_ostream &O) {
227de10a02fSZi Xuan Wu auto V = MI->getOperand(OpNum).getImm();
228de10a02fSZi Xuan Wu ListSeparator LS;
229de10a02fSZi Xuan Wu
230de10a02fSZi Xuan Wu if (V & 0xf) {
231de10a02fSZi Xuan Wu O << LS;
232de10a02fSZi Xuan Wu printRegName(O, CSKY::R4);
233de10a02fSZi Xuan Wu auto Offset = (V & 0xf) - 1;
234de10a02fSZi Xuan Wu if (Offset) {
235de10a02fSZi Xuan Wu O << "-";
236de10a02fSZi Xuan Wu printRegName(O, CSKY::R4 + Offset);
237de10a02fSZi Xuan Wu }
238de10a02fSZi Xuan Wu }
239de10a02fSZi Xuan Wu
240de10a02fSZi Xuan Wu if ((V >> 4) & 0x1) {
241de10a02fSZi Xuan Wu O << LS;
242de10a02fSZi Xuan Wu printRegName(O, CSKY::R15);
243de10a02fSZi Xuan Wu }
244de10a02fSZi Xuan Wu
245de10a02fSZi Xuan Wu if ((V >> 5) & 0x7) {
246de10a02fSZi Xuan Wu O << LS;
247de10a02fSZi Xuan Wu printRegName(O, CSKY::R16);
248de10a02fSZi Xuan Wu
249de10a02fSZi Xuan Wu auto Offset = ((V >> 5) & 0x7) - 1;
250de10a02fSZi Xuan Wu
251de10a02fSZi Xuan Wu if (Offset) {
252de10a02fSZi Xuan Wu O << "-";
253de10a02fSZi Xuan Wu printRegName(O, CSKY::R16 + Offset);
254de10a02fSZi Xuan Wu }
255de10a02fSZi Xuan Wu }
256de10a02fSZi Xuan Wu
257de10a02fSZi Xuan Wu if ((V >> 8) & 0x1) {
258de10a02fSZi Xuan Wu O << LS;
259de10a02fSZi Xuan Wu printRegName(O, CSKY::R28);
260de10a02fSZi Xuan Wu }
261de10a02fSZi Xuan Wu }
262de10a02fSZi Xuan Wu
getRegisterName(unsigned RegNo)2638ba622baSZi Xuan Wu const char *CSKYInstPrinter::getRegisterName(unsigned RegNo) {
2648ba622baSZi Xuan Wu return getRegisterName(RegNo, ArchRegNames ? CSKY::NoRegAltName
2658ba622baSZi Xuan Wu : CSKY::ABIRegAltName);
2668ba622baSZi Xuan Wu }
2674ad517e6SZi Xuan Wu
printFPR(const MCInst * MI,unsigned OpNo,const MCSubtargetInfo & STI,raw_ostream & O)2684ad517e6SZi Xuan Wu void CSKYInstPrinter::printFPR(const MCInst *MI, unsigned OpNo,
2694ad517e6SZi Xuan Wu const MCSubtargetInfo &STI, raw_ostream &O) {
2704ad517e6SZi Xuan Wu const MCOperand &MO = MI->getOperand(OpNo);
2714ad517e6SZi Xuan Wu assert(MO.isReg());
2724ad517e6SZi Xuan Wu
2734ad517e6SZi Xuan Wu printFPRRegName(O, MO.getReg());
2744ad517e6SZi Xuan Wu }
275