Remove old style hacks to register AsmPrinter into TargetMachine.
[oota-llvm.git] / lib / Target / Alpha / AsmPrinter / AlphaAsmPrinter.cpp
1 //===-- AlphaAsmPrinter.cpp - Alpha 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 GAS-format Alpha assembly language.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #define DEBUG_TYPE "asm-printer"
16 #include "Alpha.h"
17 #include "AlphaInstrInfo.h"
18 #include "AlphaTargetMachine.h"
19 #include "llvm/Module.h"
20 #include "llvm/MDNode.h"
21 #include "llvm/Type.h"
22 #include "llvm/Assembly/Writer.h"
23 #include "llvm/CodeGen/AsmPrinter.h"
24 #include "llvm/CodeGen/DwarfWriter.h"
25 #include "llvm/Target/TargetAsmInfo.h"
26 #include "llvm/Target/TargetMachine.h"
27 #include "llvm/Target/TargetRegistry.h"
28 #include "llvm/Support/Compiler.h"
29 #include "llvm/Support/ErrorHandling.h"
30 #include "llvm/Support/Mangler.h"
31 #include "llvm/Support/FormattedStream.h"
32 #include "llvm/ADT/Statistic.h"
33 using namespace llvm;
34
35 STATISTIC(EmittedInsts, "Number of machine instrs printed");
36
37 namespace {
38   struct VISIBILITY_HIDDEN AlphaAsmPrinter : public AsmPrinter {
39     /// Unique incrementer for label values for referencing Global values.
40     ///
41
42     explicit AlphaAsmPrinter(formatted_raw_ostream &o, TargetMachine &tm,
43                              const TargetAsmInfo *T, bool V)
44       : AsmPrinter(o, tm, T, V) {}
45
46     virtual const char *getPassName() const {
47       return "Alpha Assembly Printer";
48     }
49     bool printInstruction(const MachineInstr *MI);
50     void printOp(const MachineOperand &MO, bool IsCallOp = false);
51     void printOperand(const MachineInstr *MI, int opNum);
52     void printBaseOffsetPair (const MachineInstr *MI, int i, bool brackets=true);
53     void printModuleLevelGV(const GlobalVariable* GVar);
54     bool runOnMachineFunction(MachineFunction &F);
55     bool doInitialization(Module &M);
56     bool doFinalization(Module &M);
57
58     bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
59                          unsigned AsmVariant, const char *ExtraCode);
60     bool PrintAsmMemoryOperand(const MachineInstr *MI,
61                                unsigned OpNo,
62                                unsigned AsmVariant,
63                                const char *ExtraCode);
64   };
65 } // end of anonymous namespace
66
67 /// createAlphaCodePrinterPass - Returns a pass that prints the Alpha
68 /// assembly code for a MachineFunction to the given output stream,
69 /// using the given target machine description.  This should work
70 /// regardless of whether the function is in SSA form.
71 ///
72 FunctionPass *llvm::createAlphaCodePrinterPass(formatted_raw_ostream &o,
73                                                TargetMachine &tm,
74                                                bool verbose) {
75   return new AlphaAsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
76 }
77
78 #include "AlphaGenAsmWriter.inc"
79
80 void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum)
81 {
82   const MachineOperand &MO = MI->getOperand(opNum);
83   if (MO.getType() == MachineOperand::MO_Register) {
84     assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
85            "Not physreg??");
86     O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
87   } else if (MO.isImm()) {
88     O << MO.getImm();
89     assert(MO.getImm() < (1 << 30));
90   } else {
91     printOp(MO);
92   }
93 }
94
95
96 void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
97   const TargetRegisterInfo &RI = *TM.getRegisterInfo();
98
99   switch (MO.getType()) {
100   case MachineOperand::MO_Register:
101     O << RI.get(MO.getReg()).AsmName;
102     return;
103
104   case MachineOperand::MO_Immediate:
105     llvm_unreachable("printOp() does not handle immediate values");
106     return;
107
108   case MachineOperand::MO_MachineBasicBlock:
109     printBasicBlockLabel(MO.getMBB());
110     return;
111
112   case MachineOperand::MO_ConstantPoolIndex:
113     O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
114       << MO.getIndex();
115     return;
116
117   case MachineOperand::MO_ExternalSymbol:
118     O << MO.getSymbolName();
119     return;
120
121   case MachineOperand::MO_GlobalAddress:
122     O << Mang->getMangledName(MO.getGlobal());
123     return;
124
125   case MachineOperand::MO_JumpTableIndex:
126     O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
127       << '_' << MO.getIndex();
128     return;
129
130   default:
131     O << "<unknown operand type: " << MO.getType() << ">";
132     return;
133   }
134 }
135
136 /// runOnMachineFunction - This uses the printMachineInstruction()
137 /// method to print assembly for each instruction.
138 ///
139 bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
140   this->MF = &MF;
141
142   SetupMachineFunction(MF);
143   O << "\n\n";
144
145   // Print out constants referenced by the function
146   EmitConstantPool(MF.getConstantPool());
147
148   // Print out jump tables referenced by the function
149   EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
150
151   // Print out labels for the function.
152   const Function *F = MF.getFunction();
153   SwitchToSection(TAI->SectionForGlobal(F));
154
155   EmitAlignment(MF.getAlignment(), F);
156   switch (F->getLinkage()) {
157   default: llvm_unreachable("Unknown linkage type!");
158   case Function::InternalLinkage:  // Symbols default to internal.
159   case Function::PrivateLinkage:
160     break;
161    case Function::ExternalLinkage:
162      O << "\t.globl " << CurrentFnName << "\n";
163      break;
164   case Function::WeakAnyLinkage:
165   case Function::WeakODRLinkage:
166   case Function::LinkOnceAnyLinkage:
167   case Function::LinkOnceODRLinkage:
168     O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
169     break;
170   }
171
172   printVisibility(CurrentFnName, F->getVisibility());
173
174   O << "\t.ent " << CurrentFnName << "\n";
175
176   O << CurrentFnName << ":\n";
177
178   // Print out code for the function.
179   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
180        I != E; ++I) {
181     if (I != MF.begin()) {
182       printBasicBlockLabel(I, true, true);
183       O << '\n';
184     }
185     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
186          II != E; ++II) {
187       // Print the assembly for the instruction.
188       ++EmittedInsts;
189       if (!printInstruction(II)) {
190         llvm_unreachable("Unhandled instruction in asm writer!");
191       }
192     }
193   }
194
195   O << "\t.end " << CurrentFnName << "\n";
196
197   // We didn't modify anything.
198   return false;
199 }
200
201 bool AlphaAsmPrinter::doInitialization(Module &M)
202 {
203   if(TM.getSubtarget<AlphaSubtarget>().hasCT())
204     O << "\t.arch ev6\n"; //This might need to be ev67, so leave this test here
205   else
206     O << "\t.arch ev6\n";
207   O << "\t.set noat\n";
208   return AsmPrinter::doInitialization(M);
209 }
210
211 void AlphaAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
212   const TargetData *TD = TM.getTargetData();
213
214   if (!GVar->hasInitializer()) return;  // External global require no code
215
216   // Check to see if this is a special global used by LLVM, if so, emit it.
217   if (EmitSpecialLLVMGlobal(GVar))
218     return;
219
220   std::string name = Mang->getMangledName(GVar);
221   Constant *C = GVar->getInitializer();
222   if (isa<MDNode>(C) || isa<MDString>(C))
223     return;
224   unsigned Size = TD->getTypeAllocSize(C->getType());
225   unsigned Align = TD->getPreferredAlignmentLog(GVar);
226
227   // 0: Switch to section
228   SwitchToSection(TAI->SectionForGlobal(GVar));
229
230   // 1: Check visibility
231   printVisibility(name, GVar->getVisibility());
232
233   // 2: Kind
234   switch (GVar->getLinkage()) {
235    case GlobalValue::LinkOnceAnyLinkage:
236    case GlobalValue::LinkOnceODRLinkage:
237    case GlobalValue::WeakAnyLinkage:
238    case GlobalValue::WeakODRLinkage:
239    case GlobalValue::CommonLinkage:
240     O << TAI->getWeakRefDirective() << name << '\n';
241     break;
242    case GlobalValue::AppendingLinkage:
243    case GlobalValue::ExternalLinkage:
244       O << TAI->getGlobalDirective() << name << "\n";
245       break;
246     case GlobalValue::InternalLinkage:
247     case GlobalValue::PrivateLinkage:
248       break;
249     default:
250       llvm_unreachable("Unknown linkage type!");
251     }
252
253   // 3: Type, Size, Align
254   if (TAI->hasDotTypeDotSizeDirective()) {
255     O << "\t.type\t" << name << ", @object\n";
256     O << "\t.size\t" << name << ", " << Size << "\n";
257   }
258
259   EmitAlignment(Align, GVar);
260
261   O << name << ":\n";
262
263   EmitGlobalConstant(C);
264   O << '\n';
265 }
266
267 bool AlphaAsmPrinter::doFinalization(Module &M) {
268   for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
269        I != E; ++I)
270     printModuleLevelGV(I);
271
272   return AsmPrinter::doFinalization(M);
273 }
274
275 /// PrintAsmOperand - Print out an operand for an inline asm expression.
276 ///
277 bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
278                                       unsigned AsmVariant,
279                                       const char *ExtraCode) {
280   printOperand(MI, OpNo);
281   return false;
282 }
283
284 bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
285                                             unsigned OpNo,
286                                             unsigned AsmVariant,
287                                             const char *ExtraCode) {
288   if (ExtraCode && ExtraCode[0])
289     return true; // Unknown modifier.
290   O << "0(";
291   printOperand(MI, OpNo);
292   O << ")";
293   return false;
294 }
295
296 // Force static initialization.
297 extern "C" void LLVMInitializeAlphaAsmPrinter() { 
298   extern Target TheAlphaTarget;
299   TargetRegistry::RegisterAsmPrinter(TheAlphaTarget,
300                                      createAlphaCodePrinterPass);
301 }