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