5fb6918987c9c877bb584fdceb436065ebea1d1e
[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/Type.h"
21 #include "llvm/Assembly/Writer.h"
22 #include "llvm/CodeGen/AsmPrinter.h"
23 #include "llvm/Target/TargetAsmInfo.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include "llvm/Support/Compiler.h"
26 #include "llvm/Support/Mangler.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include "llvm/ADT/Statistic.h"
29 using namespace llvm;
30
31 STATISTIC(EmittedInsts, "Number of machine instrs printed");
32
33 namespace {
34   struct VISIBILITY_HIDDEN AlphaAsmPrinter : public AsmPrinter {
35
36     /// Unique incrementer for label values for referencing Global values.
37     ///
38
39     AlphaAsmPrinter(raw_ostream &o, TargetMachine &tm, const TargetAsmInfo *T)
40       : AsmPrinter(o, tm, T) {
41     }
42
43     virtual const char *getPassName() const {
44       return "Alpha Assembly Printer";
45     }
46     bool printInstruction(const MachineInstr *MI);
47     void printOp(const MachineOperand &MO, bool IsCallOp = false);
48     void printOperand(const MachineInstr *MI, int opNum);
49     void printBaseOffsetPair (const MachineInstr *MI, int i, bool brackets=true);
50     void printModuleLevelGV(const GlobalVariable* GVar);
51     bool runOnMachineFunction(MachineFunction &F);
52     bool doInitialization(Module &M);
53     bool doFinalization(Module &M);
54
55     bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
56                          unsigned AsmVariant, const char *ExtraCode);
57     bool PrintAsmMemoryOperand(const MachineInstr *MI,
58                                unsigned OpNo,
59                                unsigned AsmVariant,
60                                const char *ExtraCode);
61   };
62 } // end of anonymous namespace
63
64 /// createAlphaCodePrinterPass - Returns a pass that prints the Alpha
65 /// assembly code for a MachineFunction to the given output stream,
66 /// using the given target machine description.  This should work
67 /// regardless of whether the function is in SSA form.
68 ///
69 FunctionPass *llvm::createAlphaCodePrinterPass(raw_ostream &o,
70                                                TargetMachine &tm) {
71   return new AlphaAsmPrinter(o, tm, tm.getTargetAsmInfo());
72 }
73
74 #include "AlphaGenAsmWriter.inc"
75
76 void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum)
77 {
78   const MachineOperand &MO = MI->getOperand(opNum);
79   if (MO.getType() == MachineOperand::MO_Register) {
80     assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
81            "Not physreg??");
82     O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
83   } else if (MO.isImm()) {
84     O << MO.getImm();
85     assert(MO.getImm() < (1 << 30));
86   } else {
87     printOp(MO);
88   }
89 }
90
91
92 void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
93   const TargetRegisterInfo &RI = *TM.getRegisterInfo();
94
95   switch (MO.getType()) {
96   case MachineOperand::MO_Register:
97     O << RI.get(MO.getReg()).AsmName;
98     return;
99
100   case MachineOperand::MO_Immediate:
101     cerr << "printOp() does not handle immediate values\n";
102     abort();
103     return;
104
105   case MachineOperand::MO_MachineBasicBlock:
106     printBasicBlockLabel(MO.getMBB());
107     return;
108
109   case MachineOperand::MO_ConstantPoolIndex:
110     O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
111       << MO.getIndex();
112     return;
113
114   case MachineOperand::MO_ExternalSymbol:
115     O << MO.getSymbolName();
116     return;
117
118   case MachineOperand::MO_GlobalAddress: {
119     GlobalValue *GV = MO.getGlobal();
120     O << Mang->getValueName(GV);
121     if (GV->isDeclaration() && GV->hasExternalWeakLinkage())
122       ExtWeakSymbols.insert(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   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(4, F);
155   switch (F->getLinkage()) {
156   default: assert(0 && "Unknown linkage type!");
157   case Function::InternalLinkage:  // Symbols default to internal.
158     break;
159    case Function::ExternalLinkage:
160      O << "\t.globl " << CurrentFnName << "\n";
161      break;
162   case Function::WeakLinkage:
163   case Function::LinkOnceLinkage:
164     O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
165     break;
166   }
167
168   printVisibility(CurrentFnName, F->getVisibility());
169
170   O << "\t.ent " << CurrentFnName << "\n";
171
172   O << CurrentFnName << ":\n";
173
174   // Print out code for the function.
175   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
176        I != E; ++I) {
177     if (I != MF.begin()) {
178       printBasicBlockLabel(I, true, true);
179       O << '\n';
180     }
181     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
182          II != E; ++II) {
183       // Print the assembly for the instruction.
184       ++EmittedInsts;
185       if (!printInstruction(II)) {
186         assert(0 && "Unhandled instruction in asm writer!");
187         abort();
188       }
189     }
190   }
191
192   O << "\t.end " << CurrentFnName << "\n";
193
194   // We didn't modify anything.
195   return false;
196 }
197
198 bool AlphaAsmPrinter::doInitialization(Module &M)
199 {
200   if(TM.getSubtarget<AlphaSubtarget>().hasCT())
201     O << "\t.arch ev6\n"; //This might need to be ev67, so leave this test here
202   else
203     O << "\t.arch ev6\n";
204   O << "\t.set noat\n";
205   return AsmPrinter::doInitialization(M);
206 }
207
208 void AlphaAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
209   const TargetData *TD = TM.getTargetData();
210
211   if (!GVar->hasInitializer()) return;  // External global require no code
212
213   // Check to see if this is a special global used by LLVM, if so, emit it.
214   if (EmitSpecialLLVMGlobal(GVar))
215     return;
216
217   std::string name = Mang->getValueName(GVar);
218   Constant *C = GVar->getInitializer();
219   unsigned Size = TD->getABITypeSize(C->getType());
220   unsigned Align = TD->getPreferredAlignmentLog(GVar);
221
222   // 0: Switch to section
223   SwitchToSection(TAI->SectionForGlobal(GVar));
224
225   // 1: Check visibility
226   printVisibility(name, GVar->getVisibility());
227
228   // 2: Kind
229   switch (GVar->getLinkage()) {
230    case GlobalValue::LinkOnceLinkage:
231    case GlobalValue::WeakLinkage:
232    case GlobalValue::CommonLinkage:
233     O << TAI->getWeakRefDirective() << name << '\n';
234     break;
235    case GlobalValue::AppendingLinkage:
236    case GlobalValue::ExternalLinkage:
237       O << TAI->getGlobalDirective() << name << "\n";
238       break;
239     case GlobalValue::InternalLinkage:
240       break;
241     default:
242       assert(0 && "Unknown linkage type!");
243       cerr << "Unknown linkage type!\n";
244       abort();
245     }
246
247   // 3: Type, Size, Align
248   if (TAI->hasDotTypeDotSizeDirective()) {
249     O << "\t.type\t" << name << ", @object\n";
250     O << "\t.size\t" << name << ", " << Size << "\n";
251   }
252
253   EmitAlignment(Align, GVar);
254
255   O << name << ":\n";
256
257   // If the initializer is a extern weak symbol, remember to emit the weak
258   // reference!
259   if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
260     if (GV->hasExternalWeakLinkage())
261       ExtWeakSymbols.insert(GV);
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 }