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