fix Sparc, SystemZ, and MSP430 to not override AsmPrinter::doInitialization.
[oota-llvm.git] / lib / Target / MSP430 / 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 "MSP430TargetMachine.h"
19 #include "llvm/Constants.h"
20 #include "llvm/DerivedTypes.h"
21 #include "llvm/Module.h"
22 #include "llvm/CodeGen/AsmPrinter.h"
23 #include "llvm/CodeGen/DwarfWriter.h"
24 #include "llvm/CodeGen/MachineModuleInfo.h"
25 #include "llvm/CodeGen/MachineFunctionPass.h"
26 #include "llvm/CodeGen/MachineConstantPool.h"
27 #include "llvm/CodeGen/MachineInstr.h"
28 #include "llvm/Target/TargetAsmInfo.h"
29 #include "llvm/Target/TargetData.h"
30 #include "llvm/ADT/Statistic.h"
31 #include "llvm/Support/Compiler.h"
32 #include "llvm/Support/FormattedStream.h"
33 #include "llvm/Support/Mangler.h"
34 #include "llvm/Support/ErrorHandling.h"
35
36 using namespace llvm;
37
38 STATISTIC(EmittedInsts, "Number of machine instrs printed");
39
40 namespace {
41   class VISIBILITY_HIDDEN MSP430AsmPrinter : public AsmPrinter {
42   public:
43     MSP430AsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
44                      const TargetAsmInfo *TAI, bool V)
45       : AsmPrinter(O, TM, TAI, V) {}
46
47     virtual const char *getPassName() const {
48       return "MSP430 Assembly Printer";
49     }
50
51     void printOperand(const MachineInstr *MI, int OpNum,
52                       const char* Modifier = 0);
53     void printSrcMemOperand(const MachineInstr *MI, int OpNum,
54                             const char* Modifier = 0);
55     void printCCOperand(const MachineInstr *MI, int OpNum);
56     bool printInstruction(const MachineInstr *MI);  // autogenerated.
57     void printMachineInstruction(const MachineInstr * MI);
58
59     void emitFunctionHeader(const MachineFunction &MF);
60     bool runOnMachineFunction(MachineFunction &F);
61
62     void getAnalysisUsage(AnalysisUsage &AU) const {
63       AsmPrinter::getAnalysisUsage(AU);
64       AU.setPreservesAll();
65     }
66   };
67 } // end of anonymous namespace
68
69 #include "MSP430GenAsmWriter.inc"
70
71 /// createMSP430CodePrinterPass - Returns a pass that prints the MSP430
72 /// assembly code for a MachineFunction to the given output stream,
73 /// using the given target machine description.  This should work
74 /// regardless of whether the function is in SSA form.
75 ///
76 FunctionPass *llvm::createMSP430CodePrinterPass(formatted_raw_ostream &o,
77                                                 TargetMachine &tm,
78                                                 bool verbose) {
79   return new MSP430AsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
80 }
81
82
83 void MSP430AsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
84   const Function *F = MF.getFunction();
85
86   SwitchToSection(TAI->SectionForGlobal(F));
87
88   unsigned FnAlign = MF.getAlignment();
89   EmitAlignment(FnAlign, F);
90
91   switch (F->getLinkage()) {
92   default: llvm_unreachable("Unknown linkage type!");
93   case Function::InternalLinkage:  // Symbols default to internal.
94   case Function::PrivateLinkage:
95   case Function::LinkerPrivateLinkage:
96     break;
97   case Function::ExternalLinkage:
98     O << "\t.globl\t" << CurrentFnName << '\n';
99     break;
100   case Function::LinkOnceAnyLinkage:
101   case Function::LinkOnceODRLinkage:
102   case Function::WeakAnyLinkage:
103   case Function::WeakODRLinkage:
104     O << "\t.weak\t" << CurrentFnName << '\n';
105     break;
106   }
107
108   printVisibility(CurrentFnName, F->getVisibility());
109
110   O << "\t.type\t" << CurrentFnName << ",@function\n"
111     << CurrentFnName << ":\n";
112 }
113
114 bool MSP430AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
115   SetupMachineFunction(MF);
116   O << "\n\n";
117
118   // Print the 'header' of function
119   emitFunctionHeader(MF);
120
121   // Print out code for the function.
122   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
123        I != E; ++I) {
124     // Print a label for the basic block.
125     if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
126       // This is an entry block or a block that's only reachable via a
127       // fallthrough edge. In non-VerboseAsm mode, don't print the label.
128     } else {
129       printBasicBlockLabel(I, true, true, VerboseAsm);
130       O << '\n';
131     }
132
133     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
134          II != E; ++II)
135       // Print the assembly for the instruction.
136       printMachineInstruction(II);
137   }
138
139   if (TAI->hasDotTypeDotSizeDirective())
140     O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
141
142   O.flush();
143
144   // We didn't modify anything
145   return false;
146 }
147
148 void MSP430AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
149   ++EmittedInsts;
150
151   // Call the autogenerated instruction printer routines.
152   if (printInstruction(MI))
153     return;
154
155   llvm_unreachable("Should not happen");
156 }
157
158 void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
159                                     const char* Modifier) {
160   const MachineOperand &MO = MI->getOperand(OpNum);
161   switch (MO.getType()) {
162   case MachineOperand::MO_Register:
163     assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
164             "Virtual registers should be already mapped!");
165     O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
166     return;
167   case MachineOperand::MO_Immediate:
168     if (!Modifier || strcmp(Modifier, "nohash"))
169       O << '#';
170     O << MO.getImm();
171     return;
172   case MachineOperand::MO_MachineBasicBlock:
173     printBasicBlockLabel(MO.getMBB());
174     return;
175   case MachineOperand::MO_GlobalAddress: {
176     bool isMemOp  = Modifier && !strcmp(Modifier, "mem");
177     bool isCallOp = Modifier && !strcmp(Modifier, "call");
178     std::string Name = Mang->getMangledName(MO.getGlobal());
179     assert(MO.getOffset() == 0 && "No offsets allowed!");
180
181     if (isCallOp)
182       O << '#';
183     else if (isMemOp)
184       O << '&';
185
186     O << Name;
187
188     return;
189   }
190   case MachineOperand::MO_ExternalSymbol: {
191     bool isCallOp = Modifier && !strcmp(Modifier, "call");
192     std::string Name(TAI->getGlobalPrefix());
193     Name += MO.getSymbolName();
194     if (isCallOp)
195       O << '#';
196     O << Name;
197     return;
198   }
199   default:
200     llvm_unreachable("Not implemented yet!");
201   }
202 }
203
204 void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
205                                           const char* Modifier) {
206   const MachineOperand &Base = MI->getOperand(OpNum);
207   const MachineOperand &Disp = MI->getOperand(OpNum+1);
208
209   if (Base.isGlobal())
210     printOperand(MI, OpNum, "mem");
211   else if (Disp.isImm() && !Base.getReg())
212     printOperand(MI, OpNum);
213   else if (Base.getReg()) {
214     if (Disp.getImm()) {
215       printOperand(MI, OpNum + 1, "nohash");
216       O << '(';
217       printOperand(MI, OpNum);
218       O << ')';
219     } else {
220       O << '@';
221       printOperand(MI, OpNum);
222     }
223   } else
224     llvm_unreachable("Unsupported memory operand");
225 }
226
227 void MSP430AsmPrinter::printCCOperand(const MachineInstr *MI, int OpNum) {
228   unsigned CC = MI->getOperand(OpNum).getImm();
229
230   switch (CC) {
231   default:
232    llvm_unreachable("Unsupported CC code");
233    break;
234   case MSP430::COND_E:
235    O << "eq";
236    break;
237   case MSP430::COND_NE:
238    O << "ne";
239    break;
240   case MSP430::COND_HS:
241    O << "hs";
242    break;
243   case MSP430::COND_LO:
244    O << "lo";
245    break;
246   case MSP430::COND_GE:
247    O << "ge";
248    break;
249   case MSP430::COND_L:
250    O << 'l';
251    break;
252   }
253 }