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