Fix LLVM build when the user specifies CPPFLAGS on the make command line.
[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 #include "MSP430GenAsmWriter.inc"
29 #undef MachineInstr
30
31 void MSP430InstPrinter::printInst(const MCInst *MI) {
32   printInstruction(MI);
33 }
34
35 void MSP430InstPrinter::printPCRelImmOperand(const MCInst *MI, unsigned OpNo) {
36   const MCOperand &Op = MI->getOperand(OpNo);
37   if (Op.isImm())
38     O << Op.getImm();
39   else {
40     assert(Op.isExpr() && "unknown pcrel immediate operand");
41     O << *Op.getExpr();
42   }
43 }
44
45 void MSP430InstPrinter::printOperand(const MCInst *MI, unsigned OpNo,
46                                      const char *Modifier) {
47   assert((Modifier == 0 || Modifier[0] == 0) && "No modifiers supported");
48   const MCOperand &Op = MI->getOperand(OpNo);
49   if (Op.isReg()) {
50     O << getRegisterName(Op.getReg());
51   } else if (Op.isImm()) {
52     O << '#' << Op.getImm();
53   } else {
54     assert(Op.isExpr() && "unknown operand kind in printOperand");
55     O << '#' << *Op.getExpr();
56   }
57 }
58
59 void MSP430InstPrinter::printSrcMemOperand(const MCInst *MI, unsigned OpNo,
60                                            const char *Modifier) {
61   const MCOperand &Base = MI->getOperand(OpNo);
62   const MCOperand &Disp = MI->getOperand(OpNo+1);
63
64   // Print displacement first
65
66   // If the global address expression is a part of displacement field with a
67   // register base, we should not emit any prefix symbol here, e.g.
68   //   mov.w &foo, r1
69   // vs
70   //   mov.w glb(r1), r2
71   // Otherwise (!) msp430-as will silently miscompile the output :(
72   if (!Base.getReg())
73     O << '&';
74
75   if (Disp.isExpr())
76     O << *Disp.getExpr();
77   else {
78     assert(Disp.isImm() && "Expected immediate in displacement field");
79     O << Disp.getImm();
80   }
81
82   // Print register base field
83   if (Base.getReg())
84     O << '(' << getRegisterName(Base.getReg()) << ')';
85 }
86
87 void MSP430InstPrinter::printCCOperand(const MCInst *MI, unsigned OpNo) {
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 }