Add plumbing for the `linker_private' linkage type. This type is meant for
[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   case Function::LinkerPrivateLinkage:
161     break;
162    case Function::ExternalLinkage:
163      O << "\t.globl " << CurrentFnName << "\n";
164      break;
165   case Function::WeakAnyLinkage:
166   case Function::WeakODRLinkage:
167   case Function::LinkOnceAnyLinkage:
168   case Function::LinkOnceODRLinkage:
169     O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
170     break;
171   }
172
173   printVisibility(CurrentFnName, F->getVisibility());
174
175   O << "\t.ent " << CurrentFnName << "\n";
176
177   O << CurrentFnName << ":\n";
178
179   // Print out code for the function.
180   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
181        I != E; ++I) {
182     if (I != MF.begin()) {
183       printBasicBlockLabel(I, true, true);
184       O << '\n';
185     }
186     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
187          II != E; ++II) {
188       // Print the assembly for the instruction.
189       ++EmittedInsts;
190       if (!printInstruction(II)) {
191         llvm_unreachable("Unhandled instruction in asm writer!");
192       }
193     }
194   }
195
196   O << "\t.end " << CurrentFnName << "\n";
197
198   // We didn't modify anything.
199   return false;
200 }
201
202 bool AlphaAsmPrinter::doInitialization(Module &M)
203 {
204   if(TM.getSubtarget<AlphaSubtarget>().hasCT())
205     O << "\t.arch ev6\n"; //This might need to be ev67, so leave this test here
206   else
207     O << "\t.arch ev6\n";
208   O << "\t.set noat\n";
209   return AsmPrinter::doInitialization(M);
210 }
211
212 void AlphaAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
213   const TargetData *TD = TM.getTargetData();
214
215   if (!GVar->hasInitializer()) return;  // External global require no code
216
217   // Check to see if this is a special global used by LLVM, if so, emit it.
218   if (EmitSpecialLLVMGlobal(GVar))
219     return;
220
221   std::string name = Mang->getMangledName(GVar);
222   Constant *C = GVar->getInitializer();
223   if (isa<MDNode>(C) || isa<MDString>(C))
224     return;
225   unsigned Size = TD->getTypeAllocSize(C->getType());
226   unsigned Align = TD->getPreferredAlignmentLog(GVar);
227
228   // 0: Switch to section
229   SwitchToSection(TAI->SectionForGlobal(GVar));
230
231   // 1: Check visibility
232   printVisibility(name, GVar->getVisibility());
233
234   // 2: Kind
235   switch (GVar->getLinkage()) {
236    case GlobalValue::LinkOnceAnyLinkage:
237    case GlobalValue::LinkOnceODRLinkage:
238    case GlobalValue::WeakAnyLinkage:
239    case GlobalValue::WeakODRLinkage:
240    case GlobalValue::CommonLinkage:
241     O << TAI->getWeakRefDirective() << name << '\n';
242     break;
243    case GlobalValue::AppendingLinkage:
244    case GlobalValue::ExternalLinkage:
245       O << TAI->getGlobalDirective() << name << "\n";
246       break;
247     case GlobalValue::InternalLinkage:
248     case GlobalValue::PrivateLinkage:
249     case GlobalValue::LinkerPrivateLinkage:
250       break;
251     default:
252       llvm_unreachable("Unknown linkage type!");
253     }
254
255   // 3: Type, Size, Align
256   if (TAI->hasDotTypeDotSizeDirective()) {
257     O << "\t.type\t" << name << ", @object\n";
258     O << "\t.size\t" << name << ", " << Size << "\n";
259   }
260
261   EmitAlignment(Align, GVar);
262
263   O << name << ":\n";
264
265   EmitGlobalConstant(C);
266   O << '\n';
267 }
268
269 bool AlphaAsmPrinter::doFinalization(Module &M) {
270   for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
271        I != E; ++I)
272     printModuleLevelGV(I);
273
274   return AsmPrinter::doFinalization(M);
275 }
276
277 /// PrintAsmOperand - Print out an operand for an inline asm expression.
278 ///
279 bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
280                                       unsigned AsmVariant,
281                                       const char *ExtraCode) {
282   printOperand(MI, OpNo);
283   return false;
284 }
285
286 bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
287                                             unsigned OpNo,
288                                             unsigned AsmVariant,
289                                             const char *ExtraCode) {
290   if (ExtraCode && ExtraCode[0])
291     return true; // Unknown modifier.
292   O << "0(";
293   printOperand(MI, OpNo);
294   O << ")";
295   return false;
296 }
297
298 // Force static initialization.
299 extern "C" void LLVMInitializeAlphaAsmPrinter() { 
300   TargetRegistry::RegisterAsmPrinter(TheAlphaTarget,
301                                      createAlphaCodePrinterPass);
302 }