LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
XCoreInstPrinter.cpp
Go to the documentation of this file.
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"
23 using namespace llvm;
24 
25 #include "XCoreGenAsmWriter.inc"
26 
27 void XCoreInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
28  OS << StringRef(getRegisterName(RegNo)).lower();
29 }
30 
32  StringRef Annot) {
33  printInstruction(MI, O);
34  printAnnotation(O, Annot);
35 }
36 
37 void XCoreInstPrinter::
38 printInlineJT(const MCInst *MI, int opNum, raw_ostream &O) {
39  report_fatal_error("can't handle InlineJT");
40 }
41 
42 void XCoreInstPrinter::
43 printInlineJT32(const MCInst *MI, int opNum, raw_ostream &O) {
44  report_fatal_error("can't handle InlineJT32");
45 }
46 
47 static 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 
71 void XCoreInstPrinter::
72 printOperand(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 }
const MCSymbol & getSymbol() const
Definition: MCExpr.h:283
bool isReg() const
Definition: MCInst.h:56
enable_if_c<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:266
virtual void printRegName(raw_ostream &OS, unsigned RegNo) const
printRegName - Print the assembler register name.
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const char *reason, bool gen_crash_diag=true)
virtual void printInst(const MCInst *MI, raw_ostream &O, StringRef Annot)
unsigned getReg() const
getReg - Returns the register number.
Definition: MCInst.h:63
static void printExpr(const MCExpr *Expr, raw_ostream &OS)
bool isImm() const
Definition: MCInst.h:57
const MCExpr * getExpr() const
Definition: MCInst.h:93
void printInstruction(const MCInst *MI, raw_ostream &O)
bool isExpr() const
Definition: MCInst.h:59
MCBinaryExpr - Binary assembler expressions.
Definition: MCExpr.h:356
int64_t getImm() const
Definition: MCInst.h:74
This file contains the declaration of the XCoreInstPrinter class, which is used to print XCore MCInst...
VariantKind getKind() const
Definition: MCExpr.h:285
void printAnnotation(raw_ostream &OS, StringRef Annot)
Utility function for printing annotations.
std::string lower() const
Definition: StringRef.cpp:118
const MCOperand & getOperand(unsigned i) const
Definition: MCInst.h:163
static const char * getRegisterName(unsigned RegNo)