1 //===-- VEInstPrinter.cpp - Convert VE MCInst to assembly 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 VE MCInst to a .s file.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "VEInstPrinter.h"
14 #include "VE.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/raw_ostream.h"
21 
22 using namespace llvm;
23 
24 #define DEBUG_TYPE "ve-asmprinter"
25 
26 // The generated AsmMatcher VEGenAsmWriter uses "VE" as the target
27 // namespace.
28 namespace llvm {
29 namespace VE {
30 using namespace VE;
31 }
32 } // namespace llvm
33 
34 #define GET_INSTRUCTION_NAME
35 #define PRINT_ALIAS_INSTR
36 #include "VEGenAsmWriter.inc"
37 
38 void VEInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
39   // Generic registers have identical register name among register classes.
40   unsigned AltIdx = VE::AsmName;
41   // Misc registers have each own name, so no use alt-names.
42   if (MRI.getRegClass(VE::MISCRegClassID).contains(RegNo))
43     AltIdx = VE::NoRegAltName;
44   OS << '%' << getRegisterName(RegNo, AltIdx);
45 }
46 
47 void VEInstPrinter::printInst(const MCInst *MI, uint64_t Address,
48                               StringRef Annot, const MCSubtargetInfo &STI,
49                               raw_ostream &OS) {
50   if (!printAliasInstr(MI, Address, STI, OS))
51     printInstruction(MI, Address, STI, OS);
52   printAnnotation(OS, Annot);
53 }
54 
55 void VEInstPrinter::printOperand(const MCInst *MI, int OpNum,
56                                  const MCSubtargetInfo &STI, raw_ostream &O) {
57   const MCOperand &MO = MI->getOperand(OpNum);
58 
59   if (MO.isReg()) {
60     printRegName(O, MO.getReg());
61     return;
62   }
63 
64   if (MO.isImm()) {
65     // Expects signed 32bit literals.
66     int32_t TruncatedImm = static_cast<int32_t>(MO.getImm());
67     O << TruncatedImm;
68     return;
69   }
70 
71   assert(MO.isExpr() && "Unknown operand kind in printOperand");
72   MO.getExpr()->print(O, &MAI);
73 }
74 
75 void VEInstPrinter::printMemASXOperand(const MCInst *MI, int OpNum,
76                                        const MCSubtargetInfo &STI,
77                                        raw_ostream &O, const char *Modifier) {
78   // If this is an ADD operand, emit it like normal operands.
79   if (Modifier && !strcmp(Modifier, "arith")) {
80     printOperand(MI, OpNum, STI, O);
81     O << ", ";
82     printOperand(MI, OpNum + 1, STI, O);
83     return;
84   }
85 
86   if (MI->getOperand(OpNum + 2).isImm() &&
87       MI->getOperand(OpNum + 2).getImm() == 0) {
88     // don't print "+0"
89   } else {
90     printOperand(MI, OpNum + 2, STI, O);
91   }
92   if (MI->getOperand(OpNum + 1).isImm() &&
93       MI->getOperand(OpNum + 1).getImm() == 0 &&
94       MI->getOperand(OpNum).isImm() && MI->getOperand(OpNum).getImm() == 0) {
95     if (MI->getOperand(OpNum + 2).isImm() &&
96         MI->getOperand(OpNum + 2).getImm() == 0) {
97       O << "0";
98     } else {
99       // don't print "+0,+0"
100     }
101   } else {
102     O << "(";
103     if (MI->getOperand(OpNum + 1).isImm() &&
104         MI->getOperand(OpNum + 1).getImm() == 0) {
105       // don't print "+0"
106     } else {
107       printOperand(MI, OpNum + 1, STI, O);
108     }
109     if (MI->getOperand(OpNum).isImm() && MI->getOperand(OpNum).getImm() == 0) {
110       // don't print "+0"
111     } else {
112       O << ", ";
113       printOperand(MI, OpNum, STI, O);
114     }
115     O << ")";
116   }
117 }
118 
119 void VEInstPrinter::printMemASOperandASX(const MCInst *MI, int OpNum,
120                                          const MCSubtargetInfo &STI,
121                                          raw_ostream &O, const char *Modifier) {
122   // If this is an ADD operand, emit it like normal operands.
123   if (Modifier && !strcmp(Modifier, "arith")) {
124     printOperand(MI, OpNum, STI, O);
125     O << ", ";
126     printOperand(MI, OpNum + 1, STI, O);
127     return;
128   }
129 
130   if (MI->getOperand(OpNum + 1).isImm() &&
131       MI->getOperand(OpNum + 1).getImm() == 0) {
132     // don't print "+0"
133   } else {
134     printOperand(MI, OpNum + 1, STI, O);
135   }
136   if (MI->getOperand(OpNum).isImm() && MI->getOperand(OpNum).getImm() == 0) {
137     if (MI->getOperand(OpNum + 1).isImm() &&
138         MI->getOperand(OpNum + 1).getImm() == 0) {
139       O << "0";
140     } else {
141       // don't print "(0)"
142     }
143   } else {
144     O << "(, ";
145     printOperand(MI, OpNum, STI, O);
146     O << ")";
147   }
148 }
149 
150 void VEInstPrinter::printMemASOperandRRM(const MCInst *MI, int OpNum,
151                                          const MCSubtargetInfo &STI,
152                                          raw_ostream &O, const char *Modifier) {
153   // If this is an ADD operand, emit it like normal operands.
154   if (Modifier && !strcmp(Modifier, "arith")) {
155     printOperand(MI, OpNum, STI, O);
156     O << ", ";
157     printOperand(MI, OpNum + 1, STI, O);
158     return;
159   }
160 
161   if (MI->getOperand(OpNum + 1).isImm() &&
162       MI->getOperand(OpNum + 1).getImm() == 0) {
163     // don't print "+0"
164   } else {
165     printOperand(MI, OpNum + 1, STI, O);
166   }
167   if (MI->getOperand(OpNum).isImm() && MI->getOperand(OpNum).getImm() == 0) {
168     if (MI->getOperand(OpNum + 1).isImm() &&
169         MI->getOperand(OpNum + 1).getImm() == 0) {
170       O << "0";
171     } else {
172       // don't print "(0)"
173     }
174   } else {
175     O << "(";
176     printOperand(MI, OpNum, STI, O);
177     O << ")";
178   }
179 }
180 
181 void VEInstPrinter::printMemASOperandHM(const MCInst *MI, int OpNum,
182                                         const MCSubtargetInfo &STI,
183                                         raw_ostream &O, const char *Modifier) {
184   // If this is an ADD operand, emit it like normal operands.
185   if (Modifier && !strcmp(Modifier, "arith")) {
186     printOperand(MI, OpNum, STI, O);
187     O << ", ";
188     printOperand(MI, OpNum + 1, STI, O);
189     return;
190   }
191 
192   if (MI->getOperand(OpNum + 1).isImm() &&
193       MI->getOperand(OpNum + 1).getImm() == 0) {
194     // don't print "+0"
195   } else {
196     printOperand(MI, OpNum + 1, STI, O);
197   }
198   O << "(";
199   if (MI->getOperand(OpNum).isReg())
200     printOperand(MI, OpNum, STI, O);
201   O << ")";
202 }
203 
204 void VEInstPrinter::printMImmOperand(const MCInst *MI, int OpNum,
205                                      const MCSubtargetInfo &STI,
206                                      raw_ostream &O) {
207   int MImm = (int)MI->getOperand(OpNum).getImm() & 0x7f;
208   if (MImm > 63)
209     O << "(" << MImm - 64 << ")0";
210   else
211     O << "(" << MImm << ")1";
212 }
213 
214 void VEInstPrinter::printCCOperand(const MCInst *MI, int OpNum,
215                                    const MCSubtargetInfo &STI, raw_ostream &O) {
216   int CC = (int)MI->getOperand(OpNum).getImm();
217   O << VECondCodeToString((VECC::CondCode)CC);
218 }
219 
220 void VEInstPrinter::printRDOperand(const MCInst *MI, int OpNum,
221                                    const MCSubtargetInfo &STI, raw_ostream &O) {
222   int RD = (int)MI->getOperand(OpNum).getImm();
223   O << VERDToString((VERD::RoundingMode)RD);
224 }
225