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