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