LLVM API Documentation
00001 //===-- MSP430InstPrinter.cpp - Convert MSP430 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 MSP430 MCInst to a .s file. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "MSP430InstPrinter.h" 00015 #include "MSP430.h" 00016 #include "llvm/MC/MCAsmInfo.h" 00017 #include "llvm/MC/MCExpr.h" 00018 #include "llvm/MC/MCInst.h" 00019 #include "llvm/Support/ErrorHandling.h" 00020 #include "llvm/Support/FormattedStream.h" 00021 using namespace llvm; 00022 00023 #define DEBUG_TYPE "asm-printer" 00024 00025 00026 // Include the auto-generated portion of the assembly writer. 00027 #include "MSP430GenAsmWriter.inc" 00028 00029 void MSP430InstPrinter::printInst(const MCInst *MI, raw_ostream &O, 00030 StringRef Annot) { 00031 printInstruction(MI, O); 00032 printAnnotation(O, Annot); 00033 } 00034 00035 void MSP430InstPrinter::printPCRelImmOperand(const MCInst *MI, unsigned OpNo, 00036 raw_ostream &O) { 00037 const MCOperand &Op = MI->getOperand(OpNo); 00038 if (Op.isImm()) 00039 O << Op.getImm(); 00040 else { 00041 assert(Op.isExpr() && "unknown pcrel immediate operand"); 00042 O << *Op.getExpr(); 00043 } 00044 } 00045 00046 void MSP430InstPrinter::printOperand(const MCInst *MI, unsigned OpNo, 00047 raw_ostream &O, const char *Modifier) { 00048 assert((Modifier == nullptr || Modifier[0] == 0) && "No modifiers supported"); 00049 const MCOperand &Op = MI->getOperand(OpNo); 00050 if (Op.isReg()) { 00051 O << getRegisterName(Op.getReg()); 00052 } else if (Op.isImm()) { 00053 O << '#' << Op.getImm(); 00054 } else { 00055 assert(Op.isExpr() && "unknown operand kind in printOperand"); 00056 O << '#' << *Op.getExpr(); 00057 } 00058 } 00059 00060 void MSP430InstPrinter::printSrcMemOperand(const MCInst *MI, unsigned OpNo, 00061 raw_ostream &O, 00062 const char *Modifier) { 00063 const MCOperand &Base = MI->getOperand(OpNo); 00064 const MCOperand &Disp = MI->getOperand(OpNo+1); 00065 00066 // Print displacement first 00067 00068 // If the global address expression is a part of displacement field with a 00069 // register base, we should not emit any prefix symbol here, e.g. 00070 // mov.w &foo, r1 00071 // vs 00072 // mov.w glb(r1), r2 00073 // Otherwise (!) msp430-as will silently miscompile the output :( 00074 if (!Base.getReg()) 00075 O << '&'; 00076 00077 if (Disp.isExpr()) 00078 O << *Disp.getExpr(); 00079 else { 00080 assert(Disp.isImm() && "Expected immediate in displacement field"); 00081 O << Disp.getImm(); 00082 } 00083 00084 // Print register base field 00085 if (Base.getReg()) 00086 O << '(' << getRegisterName(Base.getReg()) << ')'; 00087 } 00088 00089 void MSP430InstPrinter::printCCOperand(const MCInst *MI, unsigned OpNo, 00090 raw_ostream &O) { 00091 unsigned CC = MI->getOperand(OpNo).getImm(); 00092 00093 switch (CC) { 00094 default: 00095 llvm_unreachable("Unsupported CC code"); 00096 case MSP430CC::COND_E: 00097 O << "eq"; 00098 break; 00099 case MSP430CC::COND_NE: 00100 O << "ne"; 00101 break; 00102 case MSP430CC::COND_HS: 00103 O << "hs"; 00104 break; 00105 case MSP430CC::COND_LO: 00106 O << "lo"; 00107 break; 00108 case MSP430CC::COND_GE: 00109 O << "ge"; 00110 break; 00111 case MSP430CC::COND_L: 00112 O << 'l'; 00113 break; 00114 } 00115 }