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"
22using namespace llvm;
23
24#define DEBUG_TYPE "asm-printer"
25
26#include "XCoreGenAsmWriter.inc"
27
28void XCoreInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
29  OS << StringRef(getRegisterName(RegNo)).lower();
30}
31
32void XCoreInstPrinter::printInst(const MCInst *MI, raw_ostream &O,
33                                 StringRef Annot) {
34  printInstruction(MI, O);
35  printAnnotation(O, Annot);
36}
37
38void XCoreInstPrinter::
39printInlineJT(const MCInst *MI, int opNum, raw_ostream &O) {
40  report_fatal_error("can't handle InlineJT");
41}
42
43void XCoreInstPrinter::
44printInlineJT32(const MCInst *MI, int opNum, raw_ostream &O) {
45  report_fatal_error("can't handle InlineJT32");
46}
47
48static void printExpr(const MCExpr *Expr, raw_ostream &OS) {
49  int Offset = 0;
50  const MCSymbolRefExpr *SRE;
51
52  if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr)) {
53    SRE = dyn_cast<MCSymbolRefExpr>(BE->getLHS());
54    const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(BE->getRHS());
55    assert(SRE && CE && "Binary expression must be sym+const.");
56    Offset = CE->getValue();
57  } else {
58    SRE = dyn_cast<MCSymbolRefExpr>(Expr);
59    assert(SRE && "Unexpected MCExpr type.");
60  }
61  assert(SRE->getKind() == MCSymbolRefExpr::VK_None);
62
63  OS << SRE->getSymbol();
64
65  if (Offset) {
66    if (Offset > 0)
67      OS << '+';
68    OS << Offset;
69  }
70}
71
72void XCoreInstPrinter::
73printOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O) {
74  const MCOperand &Op = MI->getOperand(OpNo);
75  if (Op.isReg()) {
76    printRegName(O, Op.getReg());
77    return;
78  }
79
80  if (Op.isImm()) {
81    O << Op.getImm();
82    return;
83  }
84
85  assert(Op.isExpr() && "unknown operand kind in printOperand");
86  printExpr(Op.getExpr(), O);
87}
88