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