1 //===-- XCoreInstPrinter.cpp - Convert XCore MCInst to assembly syntax ----===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This class prints an XCore MCInst to a .s file.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "XCoreInstPrinter.h"
15 #include "llvm/ADT/StringExtras.h"
16 #include "llvm/MC/MCExpr.h"
17 #include "llvm/MC/MCInst.h"
18 #include "llvm/MC/MCInstrInfo.h"
19 #include "llvm/MC/MCSymbol.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/raw_ostream.h"
22 using namespace llvm;
23 
24 #define DEBUG_TYPE "asm-printer"
25 
26 #include "XCoreGenAsmWriter.inc"
27 
printRegName(raw_ostream & OS,unsigned RegNo) const28 void XCoreInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
29   OS << StringRef(getRegisterName(RegNo)).lower();
30 }
31 
printInst(const MCInst * MI,raw_ostream & O,StringRef Annot,const MCSubtargetInfo & STI)32 void XCoreInstPrinter::printInst(const MCInst *MI, raw_ostream &O,
33                                  StringRef Annot, const MCSubtargetInfo &STI) {
34   printInstruction(MI, O);
35   printAnnotation(O, Annot);
36 }
37 
38 void XCoreInstPrinter::
printInlineJT(const MCInst * MI,int opNum,raw_ostream & O)39 printInlineJT(const MCInst *MI, int opNum, raw_ostream &O) {
40   report_fatal_error("can't handle InlineJT");
41 }
42 
43 void XCoreInstPrinter::
printInlineJT32(const MCInst * MI,int opNum,raw_ostream & O)44 printInlineJT32(const MCInst *MI, int opNum, raw_ostream &O) {
45   report_fatal_error("can't handle InlineJT32");
46 }
47 
printExpr(const MCExpr * Expr,const MCAsmInfo * MAI,raw_ostream & OS)48 static void printExpr(const MCExpr *Expr, const MCAsmInfo *MAI,
49                       raw_ostream &OS) {
50   int Offset = 0;
51   const MCSymbolRefExpr *SRE;
52 
53   if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr)) {
54     SRE = dyn_cast<MCSymbolRefExpr>(BE->getLHS());
55     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(BE->getRHS());
56     assert(SRE && CE && "Binary expression must be sym+const.");
57     Offset = CE->getValue();
58   } else {
59     SRE = dyn_cast<MCSymbolRefExpr>(Expr);
60     assert(SRE && "Unexpected MCExpr type.");
61   }
62   assert(SRE->getKind() == MCSymbolRefExpr::VK_None);
63 
64   SRE->getSymbol().print(OS, MAI);
65 
66   if (Offset) {
67     if (Offset > 0)
68       OS << '+';
69     OS << Offset;
70   }
71 }
72 
73 void XCoreInstPrinter::
printOperand(const MCInst * MI,unsigned OpNo,raw_ostream & O)74 printOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O) {
75   const MCOperand &Op = MI->getOperand(OpNo);
76   if (Op.isReg()) {
77     printRegName(O, Op.getReg());
78     return;
79   }
80 
81   if (Op.isImm()) {
82     O << Op.getImm();
83     return;
84   }
85 
86   assert(Op.isExpr() && "unknown operand kind in printOperand");
87   printExpr(Op.getExpr(), &MAI, O);
88 }
89