LLVM  9.0.0svn
XCoreInstPrinter.cpp
Go to the documentation of this file.
1 //===-- XCoreInstPrinter.cpp - Convert XCore MCInst to assembly syntax ----===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This class prints an XCore MCInst to a .s file.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "XCoreInstPrinter.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCInst.h"
17 #include "llvm/MC/MCSymbol.h"
18 #include "llvm/Support/Casting.h"
21 #include <cassert>
22 
23 using namespace llvm;
24 
25 #define DEBUG_TYPE "asm-printer"
26 
27 #include "XCoreGenAsmWriter.inc"
28 
29 void XCoreInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
30  OS << StringRef(getRegisterName(RegNo)).lower();
31 }
32 
34  StringRef Annot, const MCSubtargetInfo &STI) {
35  printInstruction(MI, O);
36  printAnnotation(O, Annot);
37 }
38 
39 void XCoreInstPrinter::
40 printInlineJT(const MCInst *MI, int opNum, raw_ostream &O) {
41  report_fatal_error("can't handle InlineJT");
42 }
43 
44 void XCoreInstPrinter::
45 printInlineJT32(const MCInst *MI, int opNum, raw_ostream &O) {
46  report_fatal_error("can't handle InlineJT32");
47 }
48 
49 static void printExpr(const MCExpr *Expr, const MCAsmInfo *MAI,
50  raw_ostream &OS) {
51  int Offset = 0;
52  const MCSymbolRefExpr *SRE;
53 
54  if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr)) {
55  SRE = dyn_cast<MCSymbolRefExpr>(BE->getLHS());
56  const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(BE->getRHS());
57  assert(SRE && CE && "Binary expression must be sym+const.");
58  Offset = CE->getValue();
59  } else {
60  SRE = dyn_cast<MCSymbolRefExpr>(Expr);
61  assert(SRE && "Unexpected MCExpr type.");
62  }
64 
65  SRE->getSymbol().print(OS, MAI);
66 
67  if (Offset) {
68  if (Offset > 0)
69  OS << '+';
70  OS << Offset;
71  }
72 }
73 
74 void XCoreInstPrinter::
75 printOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O) {
76  const MCOperand &Op = MI->getOperand(OpNo);
77  if (Op.isReg()) {
78  printRegName(O, Op.getReg());
79  return;
80  }
81 
82  if (Op.isImm()) {
83  O << Op.getImm();
84  return;
85  }
86 
87  assert(Op.isExpr() && "unknown operand kind in printOperand");
88  printExpr(Op.getExpr(), &MAI, O);
89 }
bool isImm() const
Definition: MCInst.h:58
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:139
This class represents lattice values for constants.
Definition: AllocatorList.h:23
VariantKind getKind() const
Definition: MCExpr.h:338
bool isReg() const
Definition: MCInst.h:57
void printInst(const MCInst *MI, raw_ostream &O, StringRef Annot, const MCSubtargetInfo &STI) override
Print the specified MCInst to the specified raw_ostream.
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:35
Represent a reference to a symbol from inside an expression.
Definition: MCExpr.h:165
unsigned getReg() const
Returns the register number.
Definition: MCInst.h:64
void printRegName(raw_ostream &OS, unsigned RegNo) const override
Print the assembler register name.
const MCExpr * getExpr() const
Definition: MCInst.h:95
Instances of this class represent a single low-level machine instruction.
Definition: MCInst.h:158
This class is intended to be used as a base class for asm properties and features specific to the tar...
Definition: MCAsmInfo.h:56
int64_t getImm() const
Definition: MCInst.h:75
void printInstruction(const MCInst *MI, raw_ostream &O)
bool isExpr() const
Definition: MCInst.h:60
Binary assembler expressions.
Definition: MCExpr.h:417
const MCSymbol & getSymbol() const
Definition: MCExpr.h:336
const MCOperand & getOperand(unsigned i) const
Definition: MCInst.h:179
static void printExpr(const MCExpr *Expr, const MCAsmInfo *MAI, raw_ostream &OS)
const MCAsmInfo & MAI
Definition: MCInstPrinter.h:45
Generic base class for all target subtargets.
LLVM_NODISCARD 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:332
This file contains the declaration of the XCoreInstPrinter class, which is used to print XCore MCInst...
LLVM_NODISCARD std::string lower() const
Definition: StringRef.cpp:107
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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:45
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
Instances of this class represent operands of the MCInst class.
Definition: MCInst.h:34
static const char * getRegisterName(unsigned RegNo)
void print(raw_ostream &OS, const MCAsmInfo *MAI) const
print - Print the value to the stream OS.
Definition: MCSymbol.cpp:59