make tblgen produce a function that returns the name for a physreg.
[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     const char *getRegisterName(unsigned RegNo) const;
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     if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
128       // This is an entry block or a block that's only reachable via a
129       // fallthrough edge. In non-VerboseAsm mode, don't print the label.
130     } else {
131       EmitBasicBlockStart(I);
132       O << '\n';
133     }
134
135     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
136          II != E; ++II)
137       // Print the assembly for the instruction.
138       printMachineInstruction(II);
139   }
140
141   if (MAI->hasDotTypeDotSizeDirective())
142     O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
143
144   // We didn't modify anything
145   return false;
146 }
147
148 void MSP430AsmPrinter::printMachineInstruction(const MachineInstr *MI) {
149   ++EmittedInsts;
150
151   processDebugLoc(MI->getDebugLoc());
152
153   // Call the autogenerated instruction printer routines.
154   printInstruction(MI);
155   
156   if (VerboseAsm && !MI->getDebugLoc().isUnknown())
157     EmitComments(*MI);
158   O << '\n';
159 }
160
161 void MSP430AsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
162                                     const char* Modifier) {
163   const MachineOperand &MO = MI->getOperand(OpNum);
164   switch (MO.getType()) {
165   case MachineOperand::MO_Register:
166     assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
167             "Virtual registers should be already mapped!");
168     O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
169     return;
170   case MachineOperand::MO_Immediate:
171     if (!Modifier || strcmp(Modifier, "nohash"))
172       O << '#';
173     O << MO.getImm();
174     return;
175   case MachineOperand::MO_MachineBasicBlock:
176     GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
177     return;
178   case MachineOperand::MO_GlobalAddress: {
179     bool isMemOp  = Modifier && !strcmp(Modifier, "mem");
180     bool isCallOp = Modifier && !strcmp(Modifier, "call");
181     std::string Name = Mang->getMangledName(MO.getGlobal());
182     assert(MO.getOffset() == 0 && "No offsets allowed!");
183
184     if (isCallOp)
185       O << '#';
186     else if (isMemOp)
187       O << '&';
188
189     O << Name;
190
191     return;
192   }
193   case MachineOperand::MO_ExternalSymbol: {
194     bool isCallOp = Modifier && !strcmp(Modifier, "call");
195     std::string Name(MAI->getGlobalPrefix());
196     Name += MO.getSymbolName();
197     if (isCallOp)
198       O << '#';
199     O << Name;
200     return;
201   }
202   default:
203     llvm_unreachable("Not implemented yet!");
204   }
205 }
206
207 void MSP430AsmPrinter::printSrcMemOperand(const MachineInstr *MI, int OpNum,
208                                           const char* Modifier) {
209   const MachineOperand &Base = MI->getOperand(OpNum);
210   const MachineOperand &Disp = MI->getOperand(OpNum+1);
211
212   if (Base.isGlobal())
213     printOperand(MI, OpNum, "mem");
214   else if (Disp.isImm() && !Base.getReg())
215     printOperand(MI, OpNum);
216   else if (Base.getReg()) {
217     if (Disp.getImm()) {
218       printOperand(MI, OpNum + 1, "nohash");
219       O << '(';
220       printOperand(MI, OpNum);
221       O << ')';
222     } else {
223       O << '@';
224       printOperand(MI, OpNum);
225     }
226   } else
227     llvm_unreachable("Unsupported memory operand");
228 }
229
230 void MSP430AsmPrinter::printCCOperand(const MachineInstr *MI, int OpNum) {
231   unsigned CC = MI->getOperand(OpNum).getImm();
232
233   switch (CC) {
234   default:
235    llvm_unreachable("Unsupported CC code");
236    break;
237   case MSP430::COND_E:
238    O << "eq";
239    break;
240   case MSP430::COND_NE:
241    O << "ne";
242    break;
243   case MSP430::COND_HS:
244    O << "hs";
245    break;
246   case MSP430::COND_LO:
247    O << "lo";
248    break;
249   case MSP430::COND_GE:
250    O << "ge";
251    break;
252   case MSP430::COND_L:
253    O << 'l';
254    break;
255   }
256 }
257
258 /// PrintAsmOperand - Print out an operand for an inline asm expression.
259 ///
260 bool MSP430AsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
261                                        unsigned AsmVariant,
262                                        const char *ExtraCode) {
263   // Does this asm operand have a single letter operand modifier?
264   if (ExtraCode && ExtraCode[0])
265     return true; // Unknown modifier.
266
267   printOperand(MI, OpNo);
268   return false;
269 }
270
271 // Force static initialization.
272 extern "C" void LLVMInitializeMSP430AsmPrinter() {
273   RegisterAsmPrinter<MSP430AsmPrinter> X(TheMSP430Target);
274 }