LLVM  3.7.0
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 #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"
22 using namespace llvm;
23 
24 #define DEBUG_TYPE "asm-printer"
25 
26 #include "XCoreGenAsmWriter.inc"
27 
28 void XCoreInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
29  OS << StringRef(getRegisterName(RegNo)).lower();
30 }
31 
33  StringRef Annot, const MCSubtargetInfo &STI) {
34  printInstruction(MI, O);
35  printAnnotation(O, Annot);
36 }
37 
38 void XCoreInstPrinter::
39 printInlineJT(const MCInst *MI, int opNum, raw_ostream &O) {
40  report_fatal_error("can't handle InlineJT");
41 }
42 
43 void XCoreInstPrinter::
44 printInlineJT32(const MCInst *MI, int opNum, raw_ostream &O) {
45  report_fatal_error("can't handle InlineJT32");
46 }
47 
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::
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 }
const MCSymbol & getSymbol() const
Definition: MCExpr.h:328
void print(raw_ostream &OS, const MCAsmInfo *MAI) const
print - Print the value to the stream OS.
Definition: MCSymbol.cpp:51
bool isReg() const
Definition: MCInst.h:56
void printInst(const MCInst *MI, raw_ostream &O, StringRef Annot, const MCSubtargetInfo &STI) override
Print the specified MCInst to the specified raw_ostream.
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const char *reason, bool gen_crash_diag=true)
Reports a serious error, calling any installed error handler.
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:33
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:159
unsigned getReg() const
Returns the register number.
Definition: MCInst.h:63
void printRegName(raw_ostream &OS, unsigned RegNo) const override
Print the assembler register name.
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:150
bool isImm() const
Definition: MCInst.h:57
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:58
const MCExpr * getExpr() const
Definition: MCInst.h:93
void printInstruction(const MCInst *MI, raw_ostream &O)
bool isExpr() const
Definition: MCInst.h:59
Binary assembler expressions.
Definition: MCExpr.h:405
static void printExpr(const MCExpr *Expr, const MCAsmInfo *MAI, raw_ostream &OS)
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:285
int64_t getImm() const
Definition: MCInst.h:74
const MCAsmInfo & MAI
Definition: MCInstPrinter.h:44
MCSubtargetInfo - Generic base class for all target subtargets.
This file contains the declaration of the XCoreInstPrinter class, which is used to print XCore MCInst...
VariantKind getKind() const
Definition: MCExpr.h:330
void printAnnotation(raw_ostream &OS, StringRef Annot)
Utility function for printing annotations.
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:38
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:33
std::string lower() const
Definition: StringRef.cpp:117
const MCOperand & getOperand(unsigned i) const
Definition: MCInst.h:164
static const char * getRegisterName(unsigned RegNo)