Rename SwitchSection -> switchSection to avoid conflicting with a future
[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/MachineConstantPool.h"
22 #include "llvm/CodeGen/ValueTypes.h"
23 #include "llvm/CodeGen/AsmPrinter.h"
24
25 #include "llvm/Target/TargetMachine.h"
26
27 #include "llvm/Support/Mangler.h"
28 #include "llvm/ADT/Statistic.h"
29 #include "llvm/Support/CommandLine.h"
30
31 using namespace llvm;
32
33 namespace {
34   Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
35
36   struct AlphaAsmPrinter : public AsmPrinter {
37
38     /// Unique incrementer for label values for referencing Global values.
39     ///
40     unsigned LabelNumber;
41
42      AlphaAsmPrinter(std::ostream &o, TargetMachine &tm)
43        : AsmPrinter(o, tm), LabelNumber(0)
44     {
45       AlignmentIsInBytes = false;
46       PrivateGlobalPrefix = "$";
47     }
48
49     /// We name each basic block in a Function with a unique number, so
50     /// that we can consistently refer to them later. This is cleared
51     /// at the beginning of each call to runOnMachineFunction().
52     ///
53     typedef std::map<const Value *, unsigned> ValueMapTy;
54     ValueMapTy NumberForBB;
55     std::string CurSection;
56
57     virtual const char *getPassName() const {
58       return "Alpha Assembly Printer";
59     }
60     bool printInstruction(const MachineInstr *MI);
61     void printOp(const MachineOperand &MO, bool IsCallOp = false);
62     void printConstantPool(MachineConstantPool *MCP);
63     void printOperand(const MachineInstr *MI, int opNum, MVT::ValueType VT);
64     void printBaseOffsetPair (const MachineInstr *MI, int i, bool brackets=true);
65     void printMachineInstruction(const MachineInstr *MI);
66     bool runOnMachineFunction(MachineFunction &F);
67     bool doInitialization(Module &M);
68     bool doFinalization(Module &M);
69     void switchSection(std::ostream &OS, const char *NewSection);
70   };
71 } // end of anonymous namespace
72
73 /// createAlphaCodePrinterPass - Returns a pass that prints the Alpha
74 /// assembly code for a MachineFunction to the given output stream,
75 /// using the given target machine description.  This should work
76 /// regardless of whether the function is in SSA form.
77 ///
78 FunctionPass *llvm::createAlphaCodePrinterPass (std::ostream &o,
79                                                   TargetMachine &tm) {
80   return new AlphaAsmPrinter(o, tm);
81 }
82
83 #include "AlphaGenAsmWriter.inc"
84
85 void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum, MVT::ValueType VT)
86 {
87   const MachineOperand &MO = MI->getOperand(opNum);
88   if (MO.getType() == MachineOperand::MO_MachineRegister) {
89     assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
90     O << TM.getRegisterInfo()->get(MO.getReg()).Name;
91   } else if (MO.isImmediate()) {
92     O << MO.getImmedValue();
93   } else {
94     printOp(MO);
95   }
96 }
97
98
99 void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
100   const MRegisterInfo &RI = *TM.getRegisterInfo();
101   int new_symbol;
102
103   switch (MO.getType()) {
104   case MachineOperand::MO_VirtualRegister:
105     if (Value *V = MO.getVRegValueOrNull()) {
106       O << "<" << V->getName() << ">";
107       return;
108     }
109     // FALLTHROUGH
110   case MachineOperand::MO_MachineRegister:
111   case MachineOperand::MO_CCRegister:
112     O << RI.get(MO.getReg()).Name;
113     return;
114
115   case MachineOperand::MO_SignExtendedImmed:
116   case MachineOperand::MO_UnextendedImmed:
117     std::cerr << "printOp() does not handle immediate values\n";
118     abort();
119     return;
120
121   case MachineOperand::MO_PCRelativeDisp:
122     std::cerr << "Shouldn't use addPCDisp() when building Alpha MachineInstrs";
123     abort();
124     return;
125
126   case MachineOperand::MO_MachineBasicBlock: {
127     MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
128     O << "$LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
129       << "_" << MBBOp->getNumber() << "\t" << CommentString << " "
130       << MBBOp->getBasicBlock()->getName();
131     return;
132   }
133
134   case MachineOperand::MO_ConstantPoolIndex:
135     O << PrivateGlobalPrefix << "CPI" << CurrentFnName << "_"
136       << MO.getConstantPoolIndex();
137     return;
138
139   case MachineOperand::MO_ExternalSymbol:
140     O << MO.getSymbolName();
141     return;
142
143   case MachineOperand::MO_GlobalAddress:
144     //Abuse PCrel to specify pcrel calls
145     //calls are the only thing that use this flag
146     if (MO.isPCRelative())
147       O << "$" << Mang->getValueName(MO.getGlobal()) << "..ng";
148     else
149       O << Mang->getValueName(MO.getGlobal());
150     return;
151
152   default:
153     O << "<unknown operand type: " << MO.getType() << ">";
154     return;
155   }
156 }
157
158 /// printMachineInstruction -- Print out a single Alpha MI to
159 /// the current output stream.
160 ///
161 void AlphaAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
162   ++EmittedInsts;
163   if (printInstruction(MI))
164     return; // Printer was automatically generated
165
166   assert(0 && "Unhandled instruction in asm writer!");
167   abort();
168   return;
169 }
170
171
172 /// runOnMachineFunction - This uses the printMachineInstruction()
173 /// method to print assembly for each instruction.
174 ///
175 bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
176   setupMachineFunction(MF);
177   O << "\n\n";
178
179   // Print out constants referenced by the function
180   printConstantPool(MF.getConstantPool());
181
182   // Print out labels for the function.
183   switchSection(O, "text");
184   emitAlignment(4);
185   O << "\t.globl " << CurrentFnName << "\n";
186   O << "\t.ent " << CurrentFnName << "\n";
187
188   O << CurrentFnName << ":\n";
189
190   // Print out code for the function.
191   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
192        I != E; ++I) {
193     // Print a label for the basic block.
194     O << "$LBB" << CurrentFnName << "_" << I->getNumber() << ":\t"
195       << CommentString << " " << I->getBasicBlock()->getName() << "\n";
196     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
197          II != E; ++II) {
198       // Print the assembly for the instruction.
199       O << "\t";
200       printMachineInstruction(II);
201     }
202   }
203   ++LabelNumber;
204
205   O << "\t.end " << CurrentFnName << "\n";
206
207   // We didn't modify anything.
208   return false;
209 }
210
211
212 /// printConstantPool - Print to the current output stream assembly
213 /// representations of the constants in the constant pool MCP. This is
214 /// used to print out constants which have been "spilled to memory" by
215 /// the code generator.
216 ///
217 void AlphaAsmPrinter::printConstantPool(MachineConstantPool *MCP) {
218   const std::vector<Constant*> &CP = MCP->getConstants();
219   const TargetData &TD = TM.getTargetData();
220
221   if (CP.empty()) return;
222
223   switchSection(O, "rodata");
224   for (unsigned i = 0, e = CP.size(); i != e; ++i) {
225     //    switchSection(O, "section .rodata, \"dr\"");
226     emitAlignment(TD.getTypeAlignmentShift(CP[i]->getType()));
227     O << PrivateGlobalPrefix << "CPI" << CurrentFnName << "_" << i 
228       << ":\t\t\t\t\t" << CommentString << *CP[i] << "\n";
229     emitGlobalConstant(CP[i]);
230   }
231 }
232
233 bool AlphaAsmPrinter::doInitialization(Module &M)
234 {
235   AsmPrinter::doInitialization(M);
236   if(TM.getSubtarget<AlphaSubtarget>().hasF2I() 
237      || TM.getSubtarget<AlphaSubtarget>().hasCT())
238     O << "\t.arch ev6\n";
239   else
240     O << "\t.arch ev56\n";
241   O << "\t.set noat\n";
242   return false;
243 }
244
245
246 // switchSection - Switch to the specified section of the executable if we are
247 // not already in it!
248 //
249 void AlphaAsmPrinter::switchSection(std::ostream &OS, const char *NewSection)
250 {
251   if (CurSection != NewSection) {
252     CurSection = NewSection;
253     if (!CurSection.empty())
254       OS << "\t.section ." << NewSection << "\n";
255   }
256 }
257
258 bool AlphaAsmPrinter::doFinalization(Module &M) {
259   const TargetData &TD = TM.getTargetData();
260
261   for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
262     if (I->hasInitializer()) {   // External global require no code
263       O << "\n\n";
264       std::string name = Mang->getValueName(I);
265       Constant *C = I->getInitializer();
266       unsigned Size = TD.getTypeSize(C->getType());
267       unsigned Align = TD.getTypeAlignmentShift(C->getType());
268
269       if (C->isNullValue() &&
270           (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
271            I->hasWeakLinkage() /* FIXME: Verify correct */)) {
272         switchSection(O, "data");
273         if (I->hasInternalLinkage())
274           O << "\t.local " << name << "\n";
275
276         O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
277           << "," << (1 << Align);
278         O << "\t\t# ";
279         WriteAsOperand(O, I, true, true, &M);
280         O << "\n";
281       } else {
282         switch (I->getLinkage()) {
283         case GlobalValue::LinkOnceLinkage:
284         case GlobalValue::WeakLinkage:   // FIXME: Verify correct for weak.
285           // Nonnull linkonce -> weak
286           O << "\t.weak " << name << "\n";
287           switchSection(O, "");
288           O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
289           break;
290         case GlobalValue::AppendingLinkage:
291           // FIXME: appending linkage variables should go into a section of
292           // their name or something.  For now, just emit them as external.
293         case GlobalValue::ExternalLinkage:
294           // If external or appending, declare as a global symbol
295           O << "\t.globl " << name << "\n";
296           // FALL THROUGH
297         case GlobalValue::InternalLinkage:
298           if (C->isNullValue())
299             switchSection(O, "bss");
300           else
301             switchSection(O, "data");
302           break;
303         case GlobalValue::GhostLinkage:
304           std::cerr << "GhostLinkage cannot appear in AlphaAsmPrinter!\n";
305           abort();
306         }
307
308         emitAlignment(Align);
309         O << "\t.type " << name << ",@object\n";
310         O << "\t.size " << name << "," << Size << "\n";
311         O << name << ":\t\t\t\t# ";
312         WriteAsOperand(O, I, true, true, &M);
313         O << " = ";
314         WriteAsOperand(O, C, false, false, &M);
315         O << "\n";
316         emitGlobalConstant(C);
317       }
318     }
319
320   AsmPrinter::doFinalization(M);
321   return false;
322 }