Wire up MSP430 printMCInst() method
[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 "MSP430TargetMachine.h"
21 #include "llvm/Constants.h"
22 #include "llvm/DerivedTypes.h"
23 #include "llvm/Module.h"
24 #include "llvm/CodeGen/AsmPrinter.h"
25 #include "llvm/CodeGen/DwarfWriter.h"
26 #include "llvm/CodeGen/MachineModuleInfo.h"
27 #include "llvm/CodeGen/MachineFunctionPass.h"
28 #include "llvm/CodeGen/MachineConstantPool.h"
29 #include "llvm/CodeGen/MachineInstr.h"
30 #include "llvm/MC/MCStreamer.h"
31 #include "llvm/MC/MCSymbol.h"
32 #include "llvm/Target/TargetData.h"
33 #include "llvm/Target/TargetLoweringObjectFile.h"
34 #include "llvm/Target/TargetRegistry.h"
35 #include "llvm/ADT/Statistic.h"
36 #include "llvm/Support/Compiler.h"
37 #include "llvm/Support/FormattedStream.h"
38 #include "llvm/Support/Mangler.h"
39 #include "llvm/Support/ErrorHandling.h"
40
41 using namespace llvm;
42
43 STATISTIC(EmittedInsts, "Number of machine instrs printed");
44
45 namespace {
46   class VISIBILITY_HIDDEN MSP430AsmPrinter : public AsmPrinter {
47   public:
48     MSP430AsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
49                      const MCAsmInfo *MAI, bool V)
50       : AsmPrinter(O, TM, MAI, V) {}
51
52     virtual const char *getPassName() const {
53       return "MSP430 Assembly Printer";
54     }
55
56     void printMCInst(const MCInst *MI) {
57       MSP430InstPrinter(O, *MAI).printInstruction(MI);
58     }
59     void printOperand(const MachineInstr *MI, int OpNum,
60                       const char* Modifier = 0);
61     void printSrcMemOperand(const MachineInstr *MI, int OpNum,
62                             const char* Modifier = 0);
63     void printCCOperand(const MachineInstr *MI, int OpNum);
64     void printInstruction(const MachineInstr *MI);  // autogenerated.
65     static const char *getRegisterName(unsigned RegNo);
66
67     void printMachineInstruction(const MachineInstr * MI);
68     bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
69                          unsigned AsmVariant,
70                          const char *ExtraCode);
71     bool PrintAsmMemoryOperand(const MachineInstr *MI,
72                                unsigned OpNo, unsigned AsmVariant,
73                                const char *ExtraCode);
74
75     void emitFunctionHeader(const MachineFunction &MF);
76     bool runOnMachineFunction(MachineFunction &F);
77
78     virtual void PrintGlobalVariable(const GlobalVariable *GV) {
79       // FIXME: No support for global variables?
80     }
81
82     void getAnalysisUsage(AnalysisUsage &AU) const {
83       AsmPrinter::getAnalysisUsage(AU);
84       AU.setPreservesAll();
85     }
86   };
87 } // end of anonymous namespace
88
89 #include "MSP430GenAsmWriter.inc"
90
91
92 void MSP430AsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
93   const Function *F = MF.getFunction();
94
95   OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
96
97   unsigned FnAlign = MF.getAlignment();
98   EmitAlignment(FnAlign, F);
99
100   switch (F->getLinkage()) {
101   default: llvm_unreachable("Unknown linkage type!");
102   case Function::InternalLinkage:  // Symbols default to internal.
103   case Function::PrivateLinkage:
104   case Function::LinkerPrivateLinkage:
105     break;
106   case Function::ExternalLinkage:
107     O << "\t.globl\t" << CurrentFnName << '\n';
108     break;
109   case Function::LinkOnceAnyLinkage:
110   case Function::LinkOnceODRLinkage:
111   case Function::WeakAnyLinkage:
112   case Function::WeakODRLinkage:
113     O << "\t.weak\t" << CurrentFnName << '\n';
114     break;
115   }
116
117   printVisibility(CurrentFnName, F->getVisibility());
118
119   O << "\t.type\t" << CurrentFnName << ",@function\n"
120     << CurrentFnName << ":\n";
121 }
122
123 bool MSP430AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
124   SetupMachineFunction(MF);
125   O << "\n\n";
126
127   // Print the 'header' of function
128   emitFunctionHeader(MF);
129
130   // Print out code for the function.
131   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
132        I != E; ++I) {
133     // Print a label for the basic block.
134     EmitBasicBlockStart(I);
135
136     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
137          II != E; ++II)
138       // Print the assembly for the instruction.
139       printMachineInstruction(II);
140   }
141
142   if (MAI->hasDotTypeDotSizeDirective())
143     O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
144
145   // We didn't modify anything
146   return false;
147 }
148
149 void MSP430AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
150   ++EmittedInsts;
151
152   processDebugLoc(MI, true);
153
154   // Call the autogenerated instruction printer routines.
155   printInstruction(MI);
156
157   if (VerboseAsm && !MI->getDebugLoc().isUnknown())
158     EmitComments(*MI);
159   O << '\n';
160
161   processDebugLoc(MI, false);
162 }
163
164 void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
165                                     const char* Modifier) {
166   const MachineOperand &MO = MI->getOperand(OpNum);
167   switch (MO.getType()) {
168   case MachineOperand::MO_Register:
169     O << getRegisterName(MO.getReg());
170     return;
171   case MachineOperand::MO_Immediate:
172     if (!Modifier || strcmp(Modifier, "nohash"))
173       O << '#';
174     O << MO.getImm();
175     return;
176   case MachineOperand::MO_MachineBasicBlock:
177     GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
178     return;
179   case MachineOperand::MO_GlobalAddress: {
180     bool isMemOp  = Modifier && !strcmp(Modifier, "mem");
181     std::string Name = Mang->getMangledName(MO.getGlobal());
182     uint64_t Offset = MO.getOffset();
183
184     O << (isMemOp ? '&' : '#');
185     if (Offset)
186       O << '(' << Offset << '+';
187
188     O << Name;
189     if (Offset)
190       O << ')';
191
192     return;
193   }
194   case MachineOperand::MO_ExternalSymbol: {
195     bool isMemOp  = Modifier && !strcmp(Modifier, "mem");
196     std::string Name(MAI->getGlobalPrefix());
197     Name += MO.getSymbolName();
198
199     O << (isMemOp ? '&' : '#') << Name;
200
201     return;
202   }
203   default:
204     llvm_unreachable("Not implemented yet!");
205   }
206 }
207
208 void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
209                                           const char* Modifier) {
210   const MachineOperand &Base = MI->getOperand(OpNum);
211   const MachineOperand &Disp = MI->getOperand(OpNum+1);
212
213   if (Base.isGlobal())
214     printOperand(MI, OpNum, "mem");
215   else if (Disp.isImm() && !Base.getReg())
216     printOperand(MI, OpNum);
217   else if (Base.getReg()) {
218     if (Disp.getImm()) {
219       printOperand(MI, OpNum + 1, "nohash");
220       O << '(';
221       printOperand(MI, OpNum);
222       O << ')';
223     } else {
224       O << '@';
225       printOperand(MI, OpNum);
226     }
227   } else
228     llvm_unreachable("Unsupported memory operand");
229 }
230
231 void MSP430AsmPrinter::printCCOperand(const MachineInstr *MI, int OpNum) {
232   unsigned CC = MI->getOperand(OpNum).getImm();
233
234   switch (CC) {
235   default:
236    llvm_unreachable("Unsupported CC code");
237    break;
238   case MSP430::COND_E:
239    O << "eq";
240    break;
241   case MSP430::COND_NE:
242    O << "ne";
243    break;
244   case MSP430::COND_HS:
245    O << "hs";
246    break;
247   case MSP430::COND_LO:
248    O << "lo";
249    break;
250   case MSP430::COND_GE:
251    O << "ge";
252    break;
253   case MSP430::COND_L:
254    O << 'l';
255    break;
256   }
257 }
258
259 /// PrintAsmOperand - Print out an operand for an inline asm expression.
260 ///
261 bool MSP430AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
262                                        unsigned AsmVariant,
263                                        const char *ExtraCode) {
264   // Does this asm operand have a single letter operand modifier?
265   if (ExtraCode && ExtraCode[0])
266     return true; // Unknown modifier.
267
268   printOperand(MI, OpNo);
269   return false;
270 }
271
272 bool MSP430AsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
273                                              unsigned OpNo, unsigned AsmVariant,
274                                              const char *ExtraCode) {
275   if (ExtraCode && ExtraCode[0]) {
276     return true; // Unknown modifier.
277   }
278   printSrcMemOperand(MI, OpNo);
279   return false;
280 }
281
282 // Force static initialization.
283 extern "C" void LLVMInitializeMSP430AsmPrinter() {
284   RegisterAsmPrinter<MSP430AsmPrinter> X(TheMSP430Target);
285 }