Home | History | Annotate | Download | only in InstPrinter
      1 //===-- MSP430InstPrinter.cpp - Convert MSP430 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 MSP430 MCInst to a .s file.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #define DEBUG_TYPE "asm-printer"
     15 #include "MSP430.h"
     16 #include "MSP430InstPrinter.h"
     17 #include "llvm/MC/MCInst.h"
     18 #include "llvm/MC/MCAsmInfo.h"
     19 #include "llvm/MC/MCExpr.h"
     20 #include "llvm/Support/ErrorHandling.h"
     21 #include "llvm/Support/FormattedStream.h"
     22 using namespace llvm;
     23 
     24 
     25 // Include the auto-generated portion of the assembly writer.
     26 #include "MSP430GenAsmWriter.inc"
     27 
     28 void MSP430InstPrinter::printInst(const MCInst *MI, raw_ostream &O) {
     29   printInstruction(MI, O);
     30 }
     31 
     32 void MSP430InstPrinter::printPCRelImmOperand(const MCInst *MI, unsigned OpNo,
     33                                              raw_ostream &O) {
     34   const MCOperand &Op = MI->getOperand(OpNo);
     35   if (Op.isImm())
     36     O << Op.getImm();
     37   else {
     38     assert(Op.isExpr() && "unknown pcrel immediate operand");
     39     O << *Op.getExpr();
     40   }
     41 }
     42 
     43 void MSP430InstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
     44                                      raw_ostream &O, const char *Modifier) {
     45   assert((Modifier == 0 || Modifier[0] == 0) && "No modifiers supported");
     46   const MCOperand &Op = MI->getOperand(OpNo);
     47   if (Op.isReg()) {
     48     O << getRegisterName(Op.getReg());
     49   } else if (Op.isImm()) {
     50     O << '#' << Op.getImm();
     51   } else {
     52     assert(Op.isExpr() && "unknown operand kind in printOperand");
     53     O << '#' << *Op.getExpr();
     54   }
     55 }
     56 
     57 void MSP430InstPrinter::printSrcMemOperand(const MCInst *MI, unsigned OpNo,
     58                                            raw_ostream &O,
     59                                            const char *Modifier) {
     60   const MCOperand &Base = MI->getOperand(OpNo);
     61   const MCOperand &Disp = MI->getOperand(OpNo+1);
     62 
     63   // Print displacement first
     64 
     65   // If the global address expression is a part of displacement field with a
     66   // register base, we should not emit any prefix symbol here, e.g.
     67   //   mov.w &foo, r1
     68   // vs
     69   //   mov.w glb(r1), r2
     70   // Otherwise (!) msp430-as will silently miscompile the output :(
     71   if (!Base.getReg())
     72     O << '&';
     73 
     74   if (Disp.isExpr())
     75     O << *Disp.getExpr();
     76   else {
     77     assert(Disp.isImm() && "Expected immediate in displacement field");
     78     O << Disp.getImm();
     79   }
     80 
     81   // Print register base field
     82   if (Base.getReg())
     83     O << '(' << getRegisterName(Base.getReg()) << ')';
     84 }
     85 
     86 void MSP430InstPrinter::printCCOperand(const MCInst *MI, unsigned OpNo,
     87                                        raw_ostream &O) {
     88   unsigned CC = MI->getOperand(OpNo).getImm();
     89 
     90   switch (CC) {
     91   default:
     92    llvm_unreachable("Unsupported CC code");
     93    break;
     94   case MSP430CC::COND_E:
     95    O << "eq";
     96    break;
     97   case MSP430CC::COND_NE:
     98    O << "ne";
     99    break;
    100   case MSP430CC::COND_HS:
    101    O << "hs";
    102    break;
    103   case MSP430CC::COND_LO:
    104    O << "lo";
    105    break;
    106   case MSP430CC::COND_GE:
    107    O << "ge";
    108    break;
    109   case MSP430CC::COND_L:
    110    O << 'l';
    111    break;
    112   }
    113 }
    114