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   OS << '%' << StringRef(getRegisterName(RegNo)).lower();
40 }
41 
42 void VEInstPrinter::printInst(const MCInst *MI, uint64_t Address,
43                               StringRef Annot, const MCSubtargetInfo &STI,
44                               raw_ostream &OS) {
45   if (!printAliasInstr(MI, Address, STI, OS))
46     printInstruction(MI, Address, STI, OS);
47   printAnnotation(OS, Annot);
48 }
49 
50 void VEInstPrinter::printOperand(const MCInst *MI, int opNum,
51                                  const MCSubtargetInfo &STI, raw_ostream &O) {
52   const MCOperand &MO = MI->getOperand(opNum);
53 
54   if (MO.isReg()) {
55     printRegName(O, MO.getReg());
56     return;
57   }
58 
59   if (MO.isImm()) {
60     switch (MI->getOpcode()) {
61     default:
62       // Expects signed 32bit literals
63       int32_t TruncatedImm = static_cast<int32_t>(MO.getImm());
64       O << TruncatedImm;
65       return;
66     }
67   }
68 
69   assert(MO.isExpr() && "Unknown operand kind in printOperand");
70   MO.getExpr()->print(O, &MAI);
71 }
72 
73 void VEInstPrinter::printMemASXOperand(const MCInst *MI, int opNum,
74                                        const MCSubtargetInfo &STI,
75                                        raw_ostream &O, const char *Modifier) {
76   // If this is an ADD operand, emit it like normal operands.
77   if (Modifier && !strcmp(Modifier, "arith")) {
78     printOperand(MI, opNum, STI, O);
79     O << ", ";
80     printOperand(MI, opNum + 1, STI, O);
81     return;
82   }
83 
84   const MCOperand &MO = MI->getOperand(opNum + 1);
85   if (!MO.isImm() || MO.getImm() != 0) {
86     printOperand(MI, opNum + 1, STI, O);
87   }
88   O << "(,";
89   printOperand(MI, opNum, STI, O);
90   O << ")";
91 }
92 
93 void VEInstPrinter::printMemASOperand(const MCInst *MI, int opNum,
94                                       const MCSubtargetInfo &STI,
95                                       raw_ostream &O, const char *Modifier) {
96   // If this is an ADD operand, emit it like normal operands.
97   if (Modifier && !strcmp(Modifier, "arith")) {
98     printOperand(MI, opNum, STI, O);
99     O << ", ";
100     printOperand(MI, opNum + 1, STI, O);
101     return;
102   }
103 
104   const MCOperand &MO = MI->getOperand(opNum + 1);
105   if (!MO.isImm() || MO.getImm() != 0) {
106     printOperand(MI, opNum + 1, STI, O);
107   }
108   O << "(";
109   printOperand(MI, opNum, STI, O);
110   O << ")";
111 }
112 
113 void VEInstPrinter::printCCOperand(const MCInst *MI, int opNum,
114                                    const MCSubtargetInfo &STI, raw_ostream &O) {
115   int CC = (int)MI->getOperand(opNum).getImm();
116   O << VECondCodeToString((VECC::CondCode)CC);
117 }
118