6ea930bc394ce5f9c7b7dc828383835501a1d55f
[oota-llvm.git] / lib / Target / MSP430 / AsmPrinter / MSP430InstPrinter.cpp
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 "MSP430InstrInfo.h"
17 #include "MSP430InstPrinter.h"
18 #include "llvm/MC/MCInst.h"
19 #include "llvm/MC/MCAsmInfo.h"
20 #include "llvm/MC/MCExpr.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/FormattedStream.h"
23 using namespace llvm;
24
25
26 // Include the auto-generated portion of the assembly writer.
27 #define MachineInstr MCInst
28 #define NO_ASM_WRITER_BOILERPLATE
29 #include "MSP430GenAsmWriter.inc"
30 #undef MachineInstr
31
32 void MSP430InstPrinter::printInst(const MCInst *MI) {
33   printInstruction(MI);
34 }
35
36 void MSP430InstPrinter::printPCRelImmOperand(const MCInst *MI, unsigned OpNo) {
37   const MCOperand &Op = MI->getOperand(OpNo);
38   if (Op.isImm())
39     O << Op.getImm();
40   else {
41     assert(Op.isExpr() && "unknown pcrel immediate operand");
42     Op.getExpr()->print(O, &MAI);
43   }
44 }
45
46 void MSP430InstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
47                                      const char *Modifier) {
48   assert((Modifier == 0 || Modifier[0] == 0) && "No modifiers supported");
49   const MCOperand &Op = MI->getOperand(OpNo);
50   if (Op.isReg()) {
51     O << getRegisterName(Op.getReg());
52   } else if (Op.isImm()) {
53     O << '#' << Op.getImm();
54   } else {
55     assert(Op.isExpr() && "unknown operand kind in printOperand");
56     O << '#';
57     Op.getExpr()->print(O, &MAI);
58   }
59 }
60
61 void MSP430InstPrinter::printSrcMemOperand(const MCInst *MI, unsigned OpNo,
62                                            const char *Modifier) {
63   const MCOperand &Base = MI->getOperand(OpNo);
64   const MCOperand &Disp = MI->getOperand(OpNo+1);
65
66   // FIXME: move global to displacement field!
67   if (Base.isExpr()) {
68     O << '&';
69     Base.getExpr()->print(O, &MAI);
70   } else if (Disp.isImm() && !Base.isReg())
71     printOperand(MI, OpNo);
72   else if (Base.isReg()) {
73     if (Disp.getImm()) {
74       O << Disp.getImm() << '(';
75       printOperand(MI, OpNo);
76       O << ')';
77     } else {
78       O << '@';
79       printOperand(MI, OpNo);
80     }
81   } else {
82     Base.dump();
83     Disp.dump();
84     llvm_unreachable("Unsupported memory operand");
85   }
86 }
87
88 void MSP430InstPrinter::printCCOperand(const MCInst *MI, unsigned OpNo) {
89   unsigned CC = MI->getOperand(OpNo).getImm();
90
91   switch (CC) {
92   default:
93    llvm_unreachable("Unsupported CC code");
94    break;
95   case MSP430CC::COND_E:
96    O << "eq";
97    break;
98   case MSP430CC::COND_NE:
99    O << "ne";
100    break;
101   case MSP430CC::COND_HS:
102    O << "hs";
103    break;
104   case MSP430CC::COND_LO:
105    O << "lo";
106    break;
107   case MSP430CC::COND_GE:
108    O << "ge";
109    break;
110   case MSP430CC::COND_L:
111    O << 'l';
112    break;
113   }
114 }