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