Dwarf requires variable entries to be in the source order. Right now, since we are...
[oota-llvm.git] / lib / Target / Alpha / 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/ADT/Statistic.h"
28 using namespace llvm;
29
30 STATISTIC(EmittedInsts, "Number of machine instrs printed");
31
32 namespace {
33   struct VISIBILITY_HIDDEN AlphaAsmPrinter : public AsmPrinter {
34
35     /// Unique incrementer for label values for referencing Global values.
36     ///
37
38     AlphaAsmPrinter(std::ostream &o, TargetMachine &tm, const TargetAsmInfo *T)
39       : AsmPrinter(o, tm, T) {
40     }
41
42     virtual const char *getPassName() const {
43       return "Alpha Assembly Printer";
44     }
45     bool printInstruction(const MachineInstr *MI);
46     void printOp(const MachineOperand &MO, bool IsCallOp = false);
47     void printOperand(const MachineInstr *MI, int opNum);
48     void printBaseOffsetPair (const MachineInstr *MI, int i, bool brackets=true);
49     bool runOnMachineFunction(MachineFunction &F);
50     bool doInitialization(Module &M);
51     bool doFinalization(Module &M);
52     
53     bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
54                          unsigned AsmVariant, const char *ExtraCode);
55     bool PrintAsmMemoryOperand(const MachineInstr *MI, 
56                                unsigned OpNo,
57                                unsigned AsmVariant, 
58                                const char *ExtraCode);
59   };
60 } // end of anonymous namespace
61
62 /// createAlphaCodePrinterPass - Returns a pass that prints the Alpha
63 /// assembly code for a MachineFunction to the given output stream,
64 /// using the given target machine description.  This should work
65 /// regardless of whether the function is in SSA form.
66 ///
67 FunctionPass *llvm::createAlphaCodePrinterPass(std::ostream &o,
68                                                TargetMachine &tm) {
69   return new AlphaAsmPrinter(o, tm, tm.getTargetAsmInfo());
70 }
71
72 #include "AlphaGenAsmWriter.inc"
73
74 void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum)
75 {
76   const MachineOperand &MO = MI->getOperand(opNum);
77   if (MO.getType() == MachineOperand::MO_Register) {
78     assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
79     O << TM.getRegisterInfo()->get(MO.getReg()).Name;
80   } else if (MO.isImmediate()) {
81     O << MO.getImm();
82     assert(MO.getImm() < (1 << 30));
83   } else {
84     printOp(MO);
85   }
86 }
87
88
89 void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
90   const MRegisterInfo &RI = *TM.getRegisterInfo();
91
92   switch (MO.getType()) {
93   case MachineOperand::MO_Register:
94     O << RI.get(MO.getReg()).Name;
95     return;
96
97   case MachineOperand::MO_Immediate:
98     cerr << "printOp() does not handle immediate values\n";
99     abort();
100     return;
101
102   case MachineOperand::MO_MachineBasicBlock:
103     printBasicBlockLabel(MO.getMBB());
104     return;
105
106   case MachineOperand::MO_ConstantPoolIndex:
107     O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
108       << MO.getIndex();
109     return;
110
111   case MachineOperand::MO_ExternalSymbol:
112     O << MO.getSymbolName();
113     return;
114
115   case MachineOperand::MO_GlobalAddress: {
116     GlobalValue *GV = MO.getGlobal();
117     O << Mang->getValueName(GV);
118     if (GV->isDeclaration() && GV->hasExternalWeakLinkage())
119       ExtWeakSymbols.insert(GV);
120     return;
121   }
122
123   case MachineOperand::MO_JumpTableIndex:
124     O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
125       << '_' << MO.getIndex();
126     return;
127
128   default:
129     O << "<unknown operand type: " << MO.getType() << ">";
130     return;
131   }
132 }
133
134 /// runOnMachineFunction - This uses the printMachineInstruction()
135 /// method to print assembly for each instruction.
136 ///
137 bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
138   SetupMachineFunction(MF);
139   O << "\n\n";
140
141   // Print out constants referenced by the function
142   EmitConstantPool(MF.getConstantPool());
143
144   // Print out jump tables referenced by the function
145   EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
146
147   // Print out labels for the function.
148   const Function *F = MF.getFunction();
149   SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
150   
151   EmitAlignment(4, F);
152   switch (F->getLinkage()) {
153   default: assert(0 && "Unknown linkage type!");
154   case Function::InternalLinkage:  // Symbols default to internal.
155     break;
156    case Function::ExternalLinkage:
157      O << "\t.globl " << CurrentFnName << "\n";
158      break;
159   case Function::WeakLinkage:
160   case Function::LinkOnceLinkage:
161     O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
162     break;
163   }
164
165   O << "\t.ent " << CurrentFnName << "\n";
166
167   O << CurrentFnName << ":\n";
168
169   // Print out code for the function.
170   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
171        I != E; ++I) {
172     if (I != MF.begin()) {
173       printBasicBlockLabel(I, true);
174       O << '\n';
175     }
176     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
177          II != E; ++II) {
178       // Print the assembly for the instruction.
179       ++EmittedInsts;
180       if (!printInstruction(II)) {
181         assert(0 && "Unhandled instruction in asm writer!");
182         abort();
183       }
184     }
185   }
186
187   O << "\t.end " << CurrentFnName << "\n";
188
189   // We didn't modify anything.
190   return false;
191 }
192
193 bool AlphaAsmPrinter::doInitialization(Module &M)
194 {
195   if(TM.getSubtarget<AlphaSubtarget>().hasCT())
196     O << "\t.arch ev6\n"; //This might need to be ev67, so leave this test here
197   else
198     O << "\t.arch ev6\n";
199   O << "\t.set noat\n";
200   return AsmPrinter::doInitialization(M);
201 }
202
203 bool AlphaAsmPrinter::doFinalization(Module &M) {
204   const TargetData *TD = TM.getTargetData();
205
206   for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I) {
207
208     if (!I->hasInitializer()) continue;  // External global require no code
209     
210     // Check to see if this is a special global used by LLVM, if so, emit it.
211     if (EmitSpecialLLVMGlobal(I))
212       continue;
213     
214     std::string name = Mang->getValueName(I);
215     Constant *C = I->getInitializer();
216     unsigned Size = TD->getABITypeSize(C->getType());
217     unsigned Align = TD->getPreferredAlignmentLog(I);
218     
219     //1: hidden?
220     if (I->hasHiddenVisibility())
221       O << TAI->getHiddenDirective() << name << "\n";
222     
223     //2: kind
224     switch (I->getLinkage()) {
225     case GlobalValue::LinkOnceLinkage:
226     case GlobalValue::WeakLinkage:
227       O << TAI->getWeakRefDirective() << name << '\n';
228       break;
229     case GlobalValue::AppendingLinkage:
230     case GlobalValue::ExternalLinkage:
231       O << "\t.globl " << name << "\n";
232       break;
233     case GlobalValue::InternalLinkage:
234       break;
235     default:
236       assert(0 && "Unknown linkage type!");
237       cerr << "Unknown linkage type!\n";
238       abort();
239     }
240     
241     //3: Section (if changed)
242     if (I->hasSection() &&
243         (I->getSection() == ".ctors" ||
244          I->getSection() == ".dtors")) {
245       std::string SectionName = ".section\t" + I->getSection()
246         + ",\"aw\",@progbits";
247       SwitchToDataSection(SectionName.c_str());
248     } else {
249       if (C->isNullValue())
250         SwitchToDataSection("\t.section\t.bss", I);
251       else
252         SwitchToDataSection("\t.section\t.data", I);
253     }
254     
255     //4: Type, Size, Align
256     O << "\t.type\t" << name << ", @object\n";
257     O << "\t.size\t" << name << ", " << Size << "\n";
258     EmitAlignment(Align, I);
259     
260     O << name << ":\n";
261     
262     // If the initializer is a extern weak symbol, remember to emit the weak
263     // reference!
264     if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
265       if (GV->hasExternalWeakLinkage())
266         ExtWeakSymbols.insert(GV);
267     
268     EmitGlobalConstant(C);
269     O << '\n';
270   }
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 }