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