11f177dd27991c189a607e3a2aed45f042c41d93
[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     GlobalValue *GV = MO.getGlobal();
122     O << Mang->getValueName(GV);
123     return;
124   }
125
126   case MachineOperand::MO_JumpTableIndex:
127     O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
128       << '_' << MO.getIndex();
129     return;
130
131   default:
132     O << "<unknown operand type: " << MO.getType() << ">";
133     return;
134   }
135 }
136
137 /// runOnMachineFunction - This uses the printMachineInstruction()
138 /// method to print assembly for each instruction.
139 ///
140 bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
141   this->MF = &MF;
142
143   SetupMachineFunction(MF);
144   O << "\n\n";
145
146   // Print out constants referenced by the function
147   EmitConstantPool(MF.getConstantPool());
148
149   // Print out jump tables referenced by the function
150   EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
151
152   // Print out labels for the function.
153   const Function *F = MF.getFunction();
154   SwitchToSection(TAI->SectionForGlobal(F));
155
156   EmitAlignment(MF.getAlignment(), F);
157   switch (F->getLinkage()) {
158   default: assert(0 && "Unknown linkage type!");
159   case Function::InternalLinkage:  // Symbols default to internal.
160   case Function::PrivateLinkage:
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->getValueName(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       break;
250     default:
251       LLVM_UNREACHABLE("Unknown linkage type!");
252     }
253
254   // 3: Type, Size, Align
255   if (TAI->hasDotTypeDotSizeDirective()) {
256     O << "\t.type\t" << name << ", @object\n";
257     O << "\t.size\t" << name << ", " << Size << "\n";
258   }
259
260   EmitAlignment(Align, GVar);
261
262   O << name << ":\n";
263
264   EmitGlobalConstant(C);
265   O << '\n';
266 }
267
268 bool AlphaAsmPrinter::doFinalization(Module &M) {
269   for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
270        I != E; ++I)
271     printModuleLevelGV(I);
272
273   return AsmPrinter::doFinalization(M);
274 }
275
276 /// PrintAsmOperand - Print out an operand for an inline asm expression.
277 ///
278 bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
279                                       unsigned AsmVariant,
280                                       const char *ExtraCode) {
281   printOperand(MI, OpNo);
282   return false;
283 }
284
285 bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
286                                             unsigned OpNo,
287                                             unsigned AsmVariant,
288                                             const char *ExtraCode) {
289   if (ExtraCode && ExtraCode[0])
290     return true; // Unknown modifier.
291   O << "0(";
292   printOperand(MI, OpNo);
293   O << ")";
294   return false;
295 }
296
297 // Force static initialization.
298 extern "C" void LLVMInitializeAlphaAsmPrinter() { }
299
300 namespace {
301   static struct Register {
302     Register() {
303       AlphaTargetMachine::registerAsmPrinter(createAlphaCodePrinterPass);
304     }
305   } Registrator;
306 }