What should be the last unnecessary <iostream>s in the library.
[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 was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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 #include "Alpha.h"
16 #include "AlphaInstrInfo.h"
17 #include "AlphaTargetMachine.h"
18 #include "llvm/Module.h"
19 #include "llvm/Type.h"
20 #include "llvm/Assembly/Writer.h"
21 #include "llvm/CodeGen/AsmPrinter.h"
22 #include "llvm/Target/TargetAsmInfo.h"
23 #include "llvm/Target/TargetMachine.h"
24 #include "llvm/Support/Compiler.h"
25 #include "llvm/Support/Mangler.h"
26 #include "llvm/ADT/Statistic.h"
27 using namespace llvm;
28
29 namespace {
30   Statistic EmittedInsts("asm-printer", "Number of machine instrs printed");
31   
32   struct VISIBILITY_HIDDEN AlphaAsmPrinter : public AsmPrinter {
33
34     /// Unique incrementer for label values for referencing Global values.
35     ///
36     unsigned LabelNumber;
37
38     AlphaAsmPrinter(std::ostream &o, TargetMachine &tm, const TargetAsmInfo *T)
39        : AsmPrinter(o, tm, T), LabelNumber(0) {
40     }
41
42     /// We name each basic block in a Function with a unique number, so
43     /// that we can consistently refer to them later. This is cleared
44     /// at the beginning of each call to runOnMachineFunction().
45     ///
46     typedef std::map<const Value *, unsigned> ValueMapTy;
47     ValueMapTy NumberForBB;
48     std::string CurSection;
49
50     virtual const char *getPassName() const {
51       return "Alpha Assembly Printer";
52     }
53     bool printInstruction(const MachineInstr *MI);
54     void printOp(const MachineOperand &MO, bool IsCallOp = false);
55     void printOperand(const MachineInstr *MI, int opNum);
56     void printBaseOffsetPair (const MachineInstr *MI, int i, bool brackets=true);
57     void printMachineInstruction(const MachineInstr *MI);
58     bool runOnMachineFunction(MachineFunction &F);
59     bool doInitialization(Module &M);
60     bool doFinalization(Module &M);
61     
62     bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
63                          unsigned AsmVariant, const char *ExtraCode);
64     bool PrintAsmMemoryOperand(const MachineInstr *MI, 
65                                unsigned OpNo,
66                                unsigned AsmVariant, 
67                                const char *ExtraCode);
68   };
69 } // end of anonymous namespace
70
71 /// createAlphaCodePrinterPass - Returns a pass that prints the Alpha
72 /// assembly code for a MachineFunction to the given output stream,
73 /// using the given target machine description.  This should work
74 /// regardless of whether the function is in SSA form.
75 ///
76 FunctionPass *llvm::createAlphaCodePrinterPass(std::ostream &o,
77                                                TargetMachine &tm) {
78   return new AlphaAsmPrinter(o, tm, tm.getTargetAsmInfo());
79 }
80
81 #include "AlphaGenAsmWriter.inc"
82
83 void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum)
84 {
85   const MachineOperand &MO = MI->getOperand(opNum);
86   if (MO.getType() == MachineOperand::MO_Register) {
87     assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
88     O << TM.getRegisterInfo()->get(MO.getReg()).Name;
89   } else if (MO.isImmediate()) {
90     O << MO.getImmedValue();
91     assert(MO.getImmedValue() < (1 << 30));
92   } else {
93     printOp(MO);
94   }
95 }
96
97
98 void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
99   const MRegisterInfo &RI = *TM.getRegisterInfo();
100
101   switch (MO.getType()) {
102   case MachineOperand::MO_Register:
103     O << RI.get(MO.getReg()).Name;
104     return;
105
106   case MachineOperand::MO_Immediate:
107     cerr << "printOp() does not handle immediate values\n";
108     abort();
109     return;
110
111   case MachineOperand::MO_MachineBasicBlock:
112     printBasicBlockLabel(MO.getMachineBasicBlock());
113     return;
114
115   case MachineOperand::MO_ConstantPoolIndex:
116     O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
117       << MO.getConstantPoolIndex();
118     return;
119
120   case MachineOperand::MO_ExternalSymbol:
121     O << MO.getSymbolName();
122     return;
123
124   case MachineOperand::MO_GlobalAddress:
125     O << Mang->getValueName(MO.getGlobal());
126     return;
127
128   case MachineOperand::MO_JumpTableIndex:
129     O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
130       << '_' << MO.getJumpTableIndex();
131     return;
132
133   default:
134     O << "<unknown operand type: " << MO.getType() << ">";
135     return;
136   }
137 }
138
139 /// printMachineInstruction -- Print out a single Alpha MI to
140 /// the current output stream.
141 ///
142 void AlphaAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
143   ++EmittedInsts;
144   if (printInstruction(MI))
145     return; // Printer was automatically generated
146
147   assert(0 && "Unhandled instruction in asm writer!");
148   abort();
149   return;
150 }
151
152
153 /// runOnMachineFunction - This uses the printMachineInstruction()
154 /// method to print assembly for each instruction.
155 ///
156 bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
157   SetupMachineFunction(MF);
158   O << "\n\n";
159
160   // Print out constants referenced by the function
161   EmitConstantPool(MF.getConstantPool());
162
163   // Print out jump tables referenced by the function
164   EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
165
166   // Print out labels for the function.
167   const Function *F = MF.getFunction();
168   SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
169   
170   EmitAlignment(4, F);
171   switch (F->getLinkage()) {
172   default: assert(0 && "Unknown linkage type!");
173   case Function::InternalLinkage:  // Symbols default to internal.
174     break;
175    case Function::ExternalLinkage:
176      O << "\t.globl " << CurrentFnName << "\n";
177      break;
178   case Function::WeakLinkage:
179   case Function::LinkOnceLinkage:
180     O << "\t.weak " << CurrentFnName << "\n";
181     break;
182   }
183
184   O << "\t.ent " << CurrentFnName << "\n";
185
186   O << CurrentFnName << ":\n";
187
188   // Print out code for the function.
189   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
190        I != E; ++I) {
191     printBasicBlockLabel(I, true);
192     O << '\n';
193     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
194          II != E; ++II) {
195       // Print the assembly for the instruction.
196       O << "\t";
197       printMachineInstruction(II);
198     }
199   }
200   ++LabelNumber;
201
202   O << "\t.end " << CurrentFnName << "\n";
203
204   // We didn't modify anything.
205   return false;
206 }
207
208 bool AlphaAsmPrinter::doInitialization(Module &M)
209 {
210   AsmPrinter::doInitialization(M);
211   if(TM.getSubtarget<AlphaSubtarget>().hasF2I() 
212      || TM.getSubtarget<AlphaSubtarget>().hasCT())
213     O << "\t.arch ev6\n";
214   else
215     O << "\t.arch ev56\n";
216   O << "\t.set noat\n";
217   return false;
218 }
219
220 bool AlphaAsmPrinter::doFinalization(Module &M) {
221   const TargetData *TD = TM.getTargetData();
222
223   for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
224     if (I->hasInitializer()) {   // External global require no code
225       // Check to see if this is a special global used by LLVM, if so, emit it.
226       if (EmitSpecialLLVMGlobal(I))
227         continue;
228       
229       O << "\n\n";
230       std::string name = Mang->getValueName(I);
231       Constant *C = I->getInitializer();
232       unsigned Size = TD->getTypeSize(C->getType());
233       //      unsigned Align = TD->getTypeAlignmentShift(C->getType());
234       unsigned Align = TD->getPreferredAlignmentLog(I);
235
236       if (C->isNullValue() &&
237           (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
238            I->hasWeakLinkage() /* FIXME: Verify correct */)) {
239         SwitchToDataSection("\t.section .data", I);
240         if (I->hasInternalLinkage())
241           O << "\t.local " << name << "\n";
242
243         O << "\t.comm " << name << "," << TD->getTypeSize(C->getType())
244           << "," << (1 << Align)
245           <<  "\n";
246       } else {
247         switch (I->getLinkage()) {
248         case GlobalValue::LinkOnceLinkage:
249         case GlobalValue::WeakLinkage:   // FIXME: Verify correct for weak.
250           // Nonnull linkonce -> weak
251           O << "\t.weak " << name << "\n";
252           O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
253           SwitchToDataSection("", I);
254           break;
255         case GlobalValue::AppendingLinkage:
256           // FIXME: appending linkage variables should go into a section of
257           // their name or something.  For now, just emit them as external.
258         case GlobalValue::ExternalLinkage:
259           // If external or appending, declare as a global symbol
260           O << "\t.globl " << name << "\n";
261           // FALL THROUGH
262         case GlobalValue::InternalLinkage:
263           SwitchToDataSection(C->isNullValue() ? "\t.section .bss" : 
264                               "\t.section .data", I);
265           break;
266         case GlobalValue::GhostLinkage:
267           cerr << "GhostLinkage cannot appear in AlphaAsmPrinter!\n";
268           abort();
269         case GlobalValue::DLLImportLinkage:
270           cerr << "DLLImport linkage is not supported by this target!\n";
271           abort();
272         case GlobalValue::DLLExportLinkage:
273           cerr << "DLLExport linkage is not supported by this target!\n";
274           abort();
275         default:
276           assert(0 && "Unknown linkage type!");
277         }
278
279         EmitAlignment(Align);
280         O << "\t.type " << name << ",@object\n";
281         O << "\t.size " << name << "," << Size << "\n";
282         O << name << ":\n";
283         EmitGlobalConstant(C);
284       }
285     }
286
287   for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
288     if (I->hasExternalWeakLinkage()) {
289       O << "\n\n";
290       std::string name = Mang->getValueName(I);
291       O << "\t.weak " << name << "\n";
292     }
293
294   AsmPrinter::doFinalization(M);
295   return false;
296 }
297
298 /// PrintAsmOperand - Print out an operand for an inline asm expression.
299 ///
300 bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
301                                     unsigned AsmVariant, 
302                                     const char *ExtraCode) {
303   printOperand(MI, OpNo);
304   return false;
305 }
306
307 bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, 
308                                             unsigned OpNo,
309                                             unsigned AsmVariant, 
310                                             const char *ExtraCode) {
311   if (ExtraCode && ExtraCode[0])
312     return true; // Unknown modifier.
313   O << "0(";
314   printOperand(MI, OpNo);
315   O << ")";
316   return false;
317 }