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