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