LLVM API Documentation

XCoreInstPrinter.cpp
Go to the documentation of this file.
00001 //===-- XCoreInstPrinter.cpp - Convert XCore MCInst to assembly syntax ----===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This class prints an XCore MCInst to a .s file.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "XCoreInstPrinter.h"
00015 #include "llvm/ADT/StringExtras.h"
00016 #include "llvm/MC/MCExpr.h"
00017 #include "llvm/MC/MCInst.h"
00018 #include "llvm/MC/MCInstrInfo.h"
00019 #include "llvm/MC/MCSymbol.h"
00020 #include "llvm/Support/ErrorHandling.h"
00021 #include "llvm/Support/raw_ostream.h"
00022 using namespace llvm;
00023 
00024 #define DEBUG_TYPE "asm-printer"
00025 
00026 #include "XCoreGenAsmWriter.inc"
00027 
00028 void XCoreInstPrinter::printRegName(raw_ostream &OS, unsigned RegNo) const {
00029   OS << StringRef(getRegisterName(RegNo)).lower();
00030 }
00031 
00032 void XCoreInstPrinter::printInst(const MCInst *MI, raw_ostream &O,
00033                                  StringRef Annot) {
00034   printInstruction(MI, O);
00035   printAnnotation(O, Annot);
00036 }
00037 
00038 void XCoreInstPrinter::
00039 printInlineJT(const MCInst *MI, int opNum, raw_ostream &O) {
00040   report_fatal_error("can't handle InlineJT");
00041 }
00042 
00043 void XCoreInstPrinter::
00044 printInlineJT32(const MCInst *MI, int opNum, raw_ostream &O) {
00045   report_fatal_error("can't handle InlineJT32");
00046 }
00047 
00048 static void printExpr(const MCExpr *Expr, raw_ostream &OS) {
00049   int Offset = 0;
00050   const MCSymbolRefExpr *SRE;
00051 
00052   if (const MCBinaryExpr *BE = dyn_cast<MCBinaryExpr>(Expr)) {
00053     SRE = dyn_cast<MCSymbolRefExpr>(BE->getLHS());
00054     const MCConstantExpr *CE = dyn_cast<MCConstantExpr>(BE->getRHS());
00055     assert(SRE && CE && "Binary expression must be sym+const.");
00056     Offset = CE->getValue();
00057   } else {
00058     SRE = dyn_cast<MCSymbolRefExpr>(Expr);
00059     assert(SRE && "Unexpected MCExpr type.");
00060   }
00061   assert(SRE->getKind() == MCSymbolRefExpr::VK_None);
00062 
00063   OS << SRE->getSymbol();
00064 
00065   if (Offset) {
00066     if (Offset > 0)
00067       OS << '+';
00068     OS << Offset;
00069   }
00070 }
00071 
00072 void XCoreInstPrinter::
00073 printOperand(const MCInst *MI, unsigned OpNo, raw_ostream &O) {
00074   const MCOperand &Op = MI->getOperand(OpNo);
00075   if (Op.isReg()) {
00076     printRegName(O, Op.getReg());
00077     return;
00078   }
00079 
00080   if (Op.isImm()) {
00081     O << Op.getImm();
00082     return;
00083   }
00084 
00085   assert(Op.isExpr() && "unknown operand kind in printOperand");
00086   printExpr(Op.getExpr(), O);
00087 }