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