xref: /minix3/external/bsd/llvm/dist/llvm/lib/Target/SystemZ/InstPrinter/SystemZInstPrinter.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===-- SystemZInstPrinter.cpp - Convert SystemZ MCInst to assembly syntax ===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc 
10f4a2713aSLionel Sambuc #include "SystemZInstPrinter.h"
11f4a2713aSLionel Sambuc #include "llvm/MC/MCExpr.h"
12f4a2713aSLionel Sambuc #include "llvm/MC/MCInstrInfo.h"
13f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h"
14f4a2713aSLionel Sambuc 
15f4a2713aSLionel Sambuc using namespace llvm;
16f4a2713aSLionel Sambuc 
17*0a6a1f1dSLionel Sambuc #define DEBUG_TYPE "asm-printer"
18*0a6a1f1dSLionel Sambuc 
19f4a2713aSLionel Sambuc #include "SystemZGenAsmWriter.inc"
20f4a2713aSLionel Sambuc 
printAddress(unsigned Base,int64_t Disp,unsigned Index,raw_ostream & O)21f4a2713aSLionel Sambuc void SystemZInstPrinter::printAddress(unsigned Base, int64_t Disp,
22f4a2713aSLionel Sambuc                                       unsigned Index, raw_ostream &O) {
23f4a2713aSLionel Sambuc   O << Disp;
24f4a2713aSLionel Sambuc   if (Base) {
25f4a2713aSLionel Sambuc     O << '(';
26f4a2713aSLionel Sambuc     if (Index)
27f4a2713aSLionel Sambuc       O << '%' << getRegisterName(Index) << ',';
28f4a2713aSLionel Sambuc     O << '%' << getRegisterName(Base) << ')';
29f4a2713aSLionel Sambuc   } else
30f4a2713aSLionel Sambuc     assert(!Index && "Shouldn't have an index without a base");
31f4a2713aSLionel Sambuc }
32f4a2713aSLionel Sambuc 
printOperand(const MCOperand & MO,raw_ostream & O)33f4a2713aSLionel Sambuc void SystemZInstPrinter::printOperand(const MCOperand &MO, raw_ostream &O) {
34f4a2713aSLionel Sambuc   if (MO.isReg())
35f4a2713aSLionel Sambuc     O << '%' << getRegisterName(MO.getReg());
36f4a2713aSLionel Sambuc   else if (MO.isImm())
37f4a2713aSLionel Sambuc     O << MO.getImm();
38f4a2713aSLionel Sambuc   else if (MO.isExpr())
39f4a2713aSLionel Sambuc     O << *MO.getExpr();
40f4a2713aSLionel Sambuc   else
41f4a2713aSLionel Sambuc     llvm_unreachable("Invalid operand");
42f4a2713aSLionel Sambuc }
43f4a2713aSLionel Sambuc 
printInst(const MCInst * MI,raw_ostream & O,StringRef Annot)44f4a2713aSLionel Sambuc void SystemZInstPrinter::printInst(const MCInst *MI, raw_ostream &O,
45f4a2713aSLionel Sambuc                                    StringRef Annot) {
46f4a2713aSLionel Sambuc   printInstruction(MI, O);
47f4a2713aSLionel Sambuc   printAnnotation(O, Annot);
48f4a2713aSLionel Sambuc }
49f4a2713aSLionel Sambuc 
printRegName(raw_ostream & O,unsigned RegNo) const50f4a2713aSLionel Sambuc void SystemZInstPrinter::printRegName(raw_ostream &O, unsigned RegNo) const {
51f4a2713aSLionel Sambuc   O << '%' << getRegisterName(RegNo);
52f4a2713aSLionel Sambuc }
53f4a2713aSLionel Sambuc 
printU4ImmOperand(const MCInst * MI,int OpNum,raw_ostream & O)54f4a2713aSLionel Sambuc void SystemZInstPrinter::printU4ImmOperand(const MCInst *MI, int OpNum,
55f4a2713aSLionel Sambuc                                            raw_ostream &O) {
56f4a2713aSLionel Sambuc   int64_t Value = MI->getOperand(OpNum).getImm();
57f4a2713aSLionel Sambuc   assert(isUInt<4>(Value) && "Invalid u4imm argument");
58f4a2713aSLionel Sambuc   O << Value;
59f4a2713aSLionel Sambuc }
60f4a2713aSLionel Sambuc 
printU6ImmOperand(const MCInst * MI,int OpNum,raw_ostream & O)61f4a2713aSLionel Sambuc void SystemZInstPrinter::printU6ImmOperand(const MCInst *MI, int OpNum,
62f4a2713aSLionel Sambuc                                            raw_ostream &O) {
63f4a2713aSLionel Sambuc   int64_t Value = MI->getOperand(OpNum).getImm();
64f4a2713aSLionel Sambuc   assert(isUInt<6>(Value) && "Invalid u6imm argument");
65f4a2713aSLionel Sambuc   O << Value;
66f4a2713aSLionel Sambuc }
67f4a2713aSLionel Sambuc 
printS8ImmOperand(const MCInst * MI,int OpNum,raw_ostream & O)68f4a2713aSLionel Sambuc void SystemZInstPrinter::printS8ImmOperand(const MCInst *MI, int OpNum,
69f4a2713aSLionel Sambuc                                            raw_ostream &O) {
70f4a2713aSLionel Sambuc   int64_t Value = MI->getOperand(OpNum).getImm();
71f4a2713aSLionel Sambuc   assert(isInt<8>(Value) && "Invalid s8imm argument");
72f4a2713aSLionel Sambuc   O << Value;
73f4a2713aSLionel Sambuc }
74f4a2713aSLionel Sambuc 
printU8ImmOperand(const MCInst * MI,int OpNum,raw_ostream & O)75f4a2713aSLionel Sambuc void SystemZInstPrinter::printU8ImmOperand(const MCInst *MI, int OpNum,
76f4a2713aSLionel Sambuc                                            raw_ostream &O) {
77f4a2713aSLionel Sambuc   int64_t Value = MI->getOperand(OpNum).getImm();
78f4a2713aSLionel Sambuc   assert(isUInt<8>(Value) && "Invalid u8imm argument");
79f4a2713aSLionel Sambuc   O << Value;
80f4a2713aSLionel Sambuc }
81f4a2713aSLionel Sambuc 
printS16ImmOperand(const MCInst * MI,int OpNum,raw_ostream & O)82f4a2713aSLionel Sambuc void SystemZInstPrinter::printS16ImmOperand(const MCInst *MI, int OpNum,
83f4a2713aSLionel Sambuc                                             raw_ostream &O) {
84f4a2713aSLionel Sambuc   int64_t Value = MI->getOperand(OpNum).getImm();
85f4a2713aSLionel Sambuc   assert(isInt<16>(Value) && "Invalid s16imm argument");
86f4a2713aSLionel Sambuc   O << Value;
87f4a2713aSLionel Sambuc }
88f4a2713aSLionel Sambuc 
printU16ImmOperand(const MCInst * MI,int OpNum,raw_ostream & O)89f4a2713aSLionel Sambuc void SystemZInstPrinter::printU16ImmOperand(const MCInst *MI, int OpNum,
90f4a2713aSLionel Sambuc                                             raw_ostream &O) {
91f4a2713aSLionel Sambuc   int64_t Value = MI->getOperand(OpNum).getImm();
92f4a2713aSLionel Sambuc   assert(isUInt<16>(Value) && "Invalid u16imm argument");
93f4a2713aSLionel Sambuc   O << Value;
94f4a2713aSLionel Sambuc }
95f4a2713aSLionel Sambuc 
printS32ImmOperand(const MCInst * MI,int OpNum,raw_ostream & O)96f4a2713aSLionel Sambuc void SystemZInstPrinter::printS32ImmOperand(const MCInst *MI, int OpNum,
97f4a2713aSLionel Sambuc                                             raw_ostream &O) {
98f4a2713aSLionel Sambuc   int64_t Value = MI->getOperand(OpNum).getImm();
99f4a2713aSLionel Sambuc   assert(isInt<32>(Value) && "Invalid s32imm argument");
100f4a2713aSLionel Sambuc   O << Value;
101f4a2713aSLionel Sambuc }
102f4a2713aSLionel Sambuc 
printU32ImmOperand(const MCInst * MI,int OpNum,raw_ostream & O)103f4a2713aSLionel Sambuc void SystemZInstPrinter::printU32ImmOperand(const MCInst *MI, int OpNum,
104f4a2713aSLionel Sambuc                                             raw_ostream &O) {
105f4a2713aSLionel Sambuc   int64_t Value = MI->getOperand(OpNum).getImm();
106f4a2713aSLionel Sambuc   assert(isUInt<32>(Value) && "Invalid u32imm argument");
107f4a2713aSLionel Sambuc   O << Value;
108f4a2713aSLionel Sambuc }
109f4a2713aSLionel Sambuc 
printAccessRegOperand(const MCInst * MI,int OpNum,raw_ostream & O)110f4a2713aSLionel Sambuc void SystemZInstPrinter::printAccessRegOperand(const MCInst *MI, int OpNum,
111f4a2713aSLionel Sambuc                                                raw_ostream &O) {
112f4a2713aSLionel Sambuc   uint64_t Value = MI->getOperand(OpNum).getImm();
113f4a2713aSLionel Sambuc   assert(Value < 16 && "Invalid access register number");
114f4a2713aSLionel Sambuc   O << "%a" << (unsigned int)Value;
115f4a2713aSLionel Sambuc }
116f4a2713aSLionel Sambuc 
printPCRelOperand(const MCInst * MI,int OpNum,raw_ostream & O)117f4a2713aSLionel Sambuc void SystemZInstPrinter::printPCRelOperand(const MCInst *MI, int OpNum,
118f4a2713aSLionel Sambuc                                            raw_ostream &O) {
119f4a2713aSLionel Sambuc   const MCOperand &MO = MI->getOperand(OpNum);
120f4a2713aSLionel Sambuc   if (MO.isImm()) {
121f4a2713aSLionel Sambuc     O << "0x";
122f4a2713aSLionel Sambuc     O.write_hex(MO.getImm());
123f4a2713aSLionel Sambuc   } else
124f4a2713aSLionel Sambuc     O << *MO.getExpr();
125f4a2713aSLionel Sambuc }
126f4a2713aSLionel Sambuc 
printOperand(const MCInst * MI,int OpNum,raw_ostream & O)127f4a2713aSLionel Sambuc void SystemZInstPrinter::printOperand(const MCInst *MI, int OpNum,
128f4a2713aSLionel Sambuc                                       raw_ostream &O) {
129f4a2713aSLionel Sambuc   printOperand(MI->getOperand(OpNum), O);
130f4a2713aSLionel Sambuc }
131f4a2713aSLionel Sambuc 
printBDAddrOperand(const MCInst * MI,int OpNum,raw_ostream & O)132f4a2713aSLionel Sambuc void SystemZInstPrinter::printBDAddrOperand(const MCInst *MI, int OpNum,
133f4a2713aSLionel Sambuc                                             raw_ostream &O) {
134f4a2713aSLionel Sambuc   printAddress(MI->getOperand(OpNum).getReg(),
135f4a2713aSLionel Sambuc                MI->getOperand(OpNum + 1).getImm(), 0, O);
136f4a2713aSLionel Sambuc }
137f4a2713aSLionel Sambuc 
printBDXAddrOperand(const MCInst * MI,int OpNum,raw_ostream & O)138f4a2713aSLionel Sambuc void SystemZInstPrinter::printBDXAddrOperand(const MCInst *MI, int OpNum,
139f4a2713aSLionel Sambuc                                              raw_ostream &O) {
140f4a2713aSLionel Sambuc   printAddress(MI->getOperand(OpNum).getReg(),
141f4a2713aSLionel Sambuc                MI->getOperand(OpNum + 1).getImm(),
142f4a2713aSLionel Sambuc                MI->getOperand(OpNum + 2).getReg(), O);
143f4a2713aSLionel Sambuc }
144f4a2713aSLionel Sambuc 
printBDLAddrOperand(const MCInst * MI,int OpNum,raw_ostream & O)145f4a2713aSLionel Sambuc void SystemZInstPrinter::printBDLAddrOperand(const MCInst *MI, int OpNum,
146f4a2713aSLionel Sambuc                                              raw_ostream &O) {
147f4a2713aSLionel Sambuc   unsigned Base = MI->getOperand(OpNum).getReg();
148f4a2713aSLionel Sambuc   uint64_t Disp = MI->getOperand(OpNum + 1).getImm();
149f4a2713aSLionel Sambuc   uint64_t Length = MI->getOperand(OpNum + 2).getImm();
150f4a2713aSLionel Sambuc   O << Disp << '(' << Length;
151f4a2713aSLionel Sambuc   if (Base)
152f4a2713aSLionel Sambuc     O << ",%" << getRegisterName(Base);
153f4a2713aSLionel Sambuc   O << ')';
154f4a2713aSLionel Sambuc }
155f4a2713aSLionel Sambuc 
printCond4Operand(const MCInst * MI,int OpNum,raw_ostream & O)156f4a2713aSLionel Sambuc void SystemZInstPrinter::printCond4Operand(const MCInst *MI, int OpNum,
157f4a2713aSLionel Sambuc                                            raw_ostream &O) {
158f4a2713aSLionel Sambuc   static const char *const CondNames[] = {
159f4a2713aSLionel Sambuc     "o", "h", "nle", "l", "nhe", "lh", "ne",
160f4a2713aSLionel Sambuc     "e", "nlh", "he", "nl", "le", "nh", "no"
161f4a2713aSLionel Sambuc   };
162f4a2713aSLionel Sambuc   uint64_t Imm = MI->getOperand(OpNum).getImm();
163f4a2713aSLionel Sambuc   assert(Imm > 0 && Imm < 15 && "Invalid condition");
164f4a2713aSLionel Sambuc   O << CondNames[Imm - 1];
165f4a2713aSLionel Sambuc }
166