Fix LLVM build when the user specifies CPPFLAGS on the make command line.
[oota-llvm.git] / lib / Target / MSP430 / AsmPrinter / MSP430AsmPrinter.cpp
1 //===-- MSP430AsmPrinter.cpp - MSP430 LLVM assembly writer ----------------===//
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 file contains a printer that converts from our internal representation
11 // of machine-dependent LLVM code to the MSP430 assembly language.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #define DEBUG_TYPE "asm-printer"
16 #include "MSP430.h"
17 #include "MSP430InstrInfo.h"
18 #include "MSP430InstPrinter.h"
19 #include "MSP430MCAsmInfo.h"
20 #include "MSP430MCInstLower.h"
21 #include "MSP430TargetMachine.h"
22 #include "llvm/Constants.h"
23 #include "llvm/DerivedTypes.h"
24 #include "llvm/Module.h"
25 #include "llvm/Assembly/Writer.h"
26 #include "llvm/CodeGen/AsmPrinter.h"
27 #include "llvm/CodeGen/DwarfWriter.h"
28 #include "llvm/CodeGen/MachineModuleInfo.h"
29 #include "llvm/CodeGen/MachineFunctionPass.h"
30 #include "llvm/CodeGen/MachineConstantPool.h"
31 #include "llvm/CodeGen/MachineInstr.h"
32 #include "llvm/MC/MCInst.h"
33 #include "llvm/MC/MCStreamer.h"
34 #include "llvm/MC/MCSymbol.h"
35 #include "llvm/Target/Mangler.h"
36 #include "llvm/Target/TargetData.h"
37 #include "llvm/Target/TargetLoweringObjectFile.h"
38 #include "llvm/Target/TargetRegistry.h"
39 #include "llvm/Support/FormattedStream.h"
40 using namespace llvm;
41
42 namespace {
43   class MSP430AsmPrinter : public AsmPrinter {
44   public:
45     MSP430AsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
46                      MCContext &Ctx, MCStreamer &Streamer,
47                      const MCAsmInfo *MAI)
48       : AsmPrinter(O, TM, Ctx, Streamer, MAI) {}
49
50     virtual const char *getPassName() const {
51       return "MSP430 Assembly Printer";
52     }
53
54     void printMCInst(const MCInst *MI) {
55       MSP430InstPrinter(O, *MAI).printInstruction(MI);
56     }
57     void printOperand(const MachineInstr *MI, int OpNum,
58                       const char* Modifier = 0);
59     void printPCRelImmOperand(const MachineInstr *MI, int OpNum) {
60       printOperand(MI, OpNum);
61     }
62     void printSrcMemOperand(const MachineInstr *MI, int OpNum,
63                             const char* Modifier = 0);
64     void printCCOperand(const MachineInstr *MI, int OpNum);
65     void printMachineInstruction(const MachineInstr * MI);
66     bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
67                          unsigned AsmVariant,
68                          const char *ExtraCode);
69     bool PrintAsmMemoryOperand(const MachineInstr *MI,
70                                unsigned OpNo, unsigned AsmVariant,
71                                const char *ExtraCode);
72     void EmitInstruction(const MachineInstr *MI);
73
74     void getAnalysisUsage(AnalysisUsage &AU) const {
75       AsmPrinter::getAnalysisUsage(AU);
76       AU.setPreservesAll();
77     }
78   };
79 } // end of anonymous namespace
80
81
82 void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
83                                     const char* Modifier) {
84   const MachineOperand &MO = MI->getOperand(OpNum);
85   switch (MO.getType()) {
86   default: assert(0 && "Not implemented yet!");
87   case MachineOperand::MO_Register:
88     O << MSP430InstPrinter::getRegisterName(MO.getReg());
89     return;
90   case MachineOperand::MO_Immediate:
91     if (!Modifier || strcmp(Modifier, "nohash"))
92       O << '#';
93     O << MO.getImm();
94     return;
95   case MachineOperand::MO_MachineBasicBlock:
96     O << *MO.getMBB()->getSymbol(OutContext);
97     return;
98   case MachineOperand::MO_GlobalAddress: {
99     bool isMemOp  = Modifier && !strcmp(Modifier, "mem");
100     uint64_t Offset = MO.getOffset();
101
102     // If the global address expression is a part of displacement field with a
103     // register base, we should not emit any prefix symbol here, e.g.
104     //   mov.w &foo, r1
105     // vs
106     //   mov.w glb(r1), r2
107     // Otherwise (!) msp430-as will silently miscompile the output :(
108     if (!Modifier || strcmp(Modifier, "nohash"))
109       O << (isMemOp ? '&' : '#');
110     if (Offset)
111       O << '(' << Offset << '+';
112
113     O << *Mang->getSymbol(MO.getGlobal());
114
115     if (Offset)
116       O << ')';
117
118     return;
119   }
120   case MachineOperand::MO_ExternalSymbol: {
121     bool isMemOp  = Modifier && !strcmp(Modifier, "mem");
122     O << (isMemOp ? '&' : '#');
123     O << MAI->getGlobalPrefix() << MO.getSymbolName();
124     return;
125   }
126   }
127 }
128
129 void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
130                                           const char* Modifier) {
131   const MachineOperand &Base = MI->getOperand(OpNum);
132   const MachineOperand &Disp = MI->getOperand(OpNum+1);
133
134   // Print displacement first
135
136   // Imm here is in fact global address - print extra modifier.
137   if (Disp.isImm() && !Base.getReg())
138     O << '&';
139   printOperand(MI, OpNum+1, "nohash");
140
141   // Print register base field
142   if (Base.getReg()) {
143     O << '(';
144     printOperand(MI, OpNum);
145     O << ')';
146   }
147 }
148
149 void MSP430AsmPrinter::printCCOperand(const MachineInstr *MI, int OpNum) {
150   switch (MI->getOperand(OpNum).getImm()) {
151   default: assert(0 && "Unknown cond");
152   case MSP430CC::COND_E:  O << "eq"; break;
153   case MSP430CC::COND_NE: O << "ne"; break;
154   case MSP430CC::COND_HS: O << "hs"; break;
155   case MSP430CC::COND_LO: O << "lo"; break;
156   case MSP430CC::COND_GE: O << "ge"; break;
157   case MSP430CC::COND_L:  O << 'l';  break;
158   }
159 }
160
161 /// PrintAsmOperand - Print out an operand for an inline asm expression.
162 ///
163 bool MSP430AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
164                                        unsigned AsmVariant,
165                                        const char *ExtraCode) {
166   // Does this asm operand have a single letter operand modifier?
167   if (ExtraCode && ExtraCode[0])
168     return true; // Unknown modifier.
169
170   printOperand(MI, OpNo);
171   return false;
172 }
173
174 bool MSP430AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
175                                              unsigned OpNo, unsigned AsmVariant,
176                                              const char *ExtraCode) {
177   if (ExtraCode && ExtraCode[0]) {
178     return true; // Unknown modifier.
179   }
180   printSrcMemOperand(MI, OpNo);
181   return false;
182 }
183
184 //===----------------------------------------------------------------------===//
185 void MSP430AsmPrinter::EmitInstruction(const MachineInstr *MI) {
186   MSP430MCInstLower MCInstLowering(OutContext, *Mang, *this);
187
188   MCInst TmpInst;
189   MCInstLowering.Lower(MI, TmpInst);
190   OutStreamer.EmitInstruction(TmpInst);
191 }
192
193 static MCInstPrinter *createMSP430MCInstPrinter(const Target &T,
194                                                 unsigned SyntaxVariant,
195                                                 const MCAsmInfo &MAI,
196                                                 raw_ostream &O) {
197   if (SyntaxVariant == 0)
198     return new MSP430InstPrinter(O, MAI);
199   return 0;
200 }
201
202 // Force static initialization.
203 extern "C" void LLVMInitializeMSP430AsmPrinter() {
204   RegisterAsmPrinter<MSP430AsmPrinter> X(TheMSP430Target);
205   TargetRegistry::RegisterMCInstPrinter(TheMSP430Target,
206                                         createMSP430MCInstPrinter);
207 }