make AsmPrinter::doFinalization iterate over the global variables
[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     virtual void PrintGlobalVariable(const GlobalVariable *GV) {
63       // FIXME: No support for global variables?
64     }
65
66     void getAnalysisUsage(AnalysisUsage &AU) const {
67       AsmPrinter::getAnalysisUsage(AU);
68       AU.setPreservesAll();
69     }
70   };
71 } // end of anonymous namespace
72
73 #include "MSP430GenAsmWriter.inc"
74
75 /// createMSP430CodePrinterPass - Returns a pass that prints the MSP430
76 /// assembly code for a MachineFunction to the given output stream,
77 /// using the given target machine description.  This should work
78 /// regardless of whether the function is in SSA form.
79 ///
80 FunctionPass *llvm::createMSP430CodePrinterPass(formatted_raw_ostream &o,
81                                                 TargetMachine &tm,
82                                                 bool verbose) {
83   return new MSP430AsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
84 }
85
86
87 void MSP430AsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
88   const Function *F = MF.getFunction();
89
90   SwitchToSection(TAI->SectionForGlobal(F));
91
92   unsigned FnAlign = MF.getAlignment();
93   EmitAlignment(FnAlign, F);
94
95   switch (F->getLinkage()) {
96   default: llvm_unreachable("Unknown linkage type!");
97   case Function::InternalLinkage:  // Symbols default to internal.
98   case Function::PrivateLinkage:
99   case Function::LinkerPrivateLinkage:
100     break;
101   case Function::ExternalLinkage:
102     O << "\t.globl\t" << CurrentFnName << '\n';
103     break;
104   case Function::LinkOnceAnyLinkage:
105   case Function::LinkOnceODRLinkage:
106   case Function::WeakAnyLinkage:
107   case Function::WeakODRLinkage:
108     O << "\t.weak\t" << CurrentFnName << '\n';
109     break;
110   }
111
112   printVisibility(CurrentFnName, F->getVisibility());
113
114   O << "\t.type\t" << CurrentFnName << ",@function\n"
115     << CurrentFnName << ":\n";
116 }
117
118 bool MSP430AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
119   SetupMachineFunction(MF);
120   O << "\n\n";
121
122   // Print the 'header' of function
123   emitFunctionHeader(MF);
124
125   // Print out code for the function.
126   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
127        I != E; ++I) {
128     // Print a label for the basic block.
129     if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
130       // This is an entry block or a block that's only reachable via a
131       // fallthrough edge. In non-VerboseAsm mode, don't print the label.
132     } else {
133       printBasicBlockLabel(I, true, true, VerboseAsm);
134       O << '\n';
135     }
136
137     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
138          II != E; ++II)
139       // Print the assembly for the instruction.
140       printMachineInstruction(II);
141   }
142
143   if (TAI->hasDotTypeDotSizeDirective())
144     O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
145
146   O.flush();
147
148   // We didn't modify anything
149   return false;
150 }
151
152 void MSP430AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
153   ++EmittedInsts;
154
155   // Call the autogenerated instruction printer routines.
156   if (printInstruction(MI))
157     return;
158
159   llvm_unreachable("Should not happen");
160 }
161
162 void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
163                                     const char* Modifier) {
164   const MachineOperand &MO = MI->getOperand(OpNum);
165   switch (MO.getType()) {
166   case MachineOperand::MO_Register:
167     assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
168             "Virtual registers should be already mapped!");
169     O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
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     printBasicBlockLabel(MO.getMBB());
178     return;
179   case MachineOperand::MO_GlobalAddress: {
180     bool isMemOp  = Modifier && !strcmp(Modifier, "mem");
181     bool isCallOp = Modifier && !strcmp(Modifier, "call");
182     std::string Name = Mang->getMangledName(MO.getGlobal());
183     assert(MO.getOffset() == 0 && "No offsets allowed!");
184
185     if (isCallOp)
186       O << '#';
187     else if (isMemOp)
188       O << '&';
189
190     O << Name;
191
192     return;
193   }
194   case MachineOperand::MO_ExternalSymbol: {
195     bool isCallOp = Modifier && !strcmp(Modifier, "call");
196     std::string Name(TAI->getGlobalPrefix());
197     Name += MO.getSymbolName();
198     if (isCallOp)
199       O << '#';
200     O << Name;
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 }