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