6f45fb5497f98df0e6486c857456dff551724278
[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   const Function *F = MF.getFunction();
177   SwitchSection(".text", F);
178   EmitAlignment(4, F);
179   switch (F->getLinkage()) {
180   default: assert(0 && "Unknown linkage type!");
181   case Function::InternalLinkage:  // Symbols default to internal.
182     break;
183    case Function::ExternalLinkage:
184      O << "\t.globl " << CurrentFnName << "\n";
185      break;
186   case Function::WeakLinkage:
187   case Function::LinkOnceLinkage:
188     O << "\t.weak " << CurrentFnName << "\n";
189     break;
190   }
191
192   O << "\t.ent " << CurrentFnName << "\n";
193
194   O << CurrentFnName << ":\n";
195
196   // Print out code for the function.
197   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
198        I != E; ++I) {
199     // Print a label for the basic block.
200     O << PrivateGlobalPrefix << "LBB" << CurrentFnName << "_" << I->getNumber()
201       << ":\t" << CommentString << " " << I->getBasicBlock()->getName() << "\n";
202     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
203          II != E; ++II) {
204       // Print the assembly for the instruction.
205       O << "\t";
206       printMachineInstruction(II);
207     }
208   }
209   ++LabelNumber;
210
211   O << "\t.end " << CurrentFnName << "\n";
212
213   // We didn't modify anything.
214   return false;
215 }
216
217 bool AlphaAsmPrinter::doInitialization(Module &M)
218 {
219   AsmPrinter::doInitialization(M);
220   if(TM.getSubtarget<AlphaSubtarget>().hasF2I() 
221      || TM.getSubtarget<AlphaSubtarget>().hasCT())
222     O << "\t.arch ev6\n";
223   else
224     O << "\t.arch ev56\n";
225   O << "\t.set noat\n";
226   return false;
227 }
228
229 bool AlphaAsmPrinter::doFinalization(Module &M) {
230   const TargetData &TD = TM.getTargetData();
231
232   for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
233     if (I->hasInitializer()) {   // External global require no code
234       // Check to see if this is a special global used by LLVM, if so, emit it.
235       if (EmitSpecialLLVMGlobal(I))
236         continue;
237       
238       O << "\n\n";
239       std::string name = Mang->getValueName(I);
240       Constant *C = I->getInitializer();
241       unsigned Size = TD.getTypeSize(C->getType());
242       //      unsigned Align = TD.getTypeAlignmentShift(C->getType());
243       unsigned Align = getPreferredAlignmentLog(I);
244
245       if (C->isNullValue() &&
246           (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
247            I->hasWeakLinkage() /* FIXME: Verify correct */)) {
248         SwitchSection("\t.section .data", I);
249         if (I->hasInternalLinkage())
250           O << "\t.local " << name << "\n";
251
252         O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
253           << "," << (1 << Align)
254           <<  "\n";
255       } else {
256         switch (I->getLinkage()) {
257         case GlobalValue::LinkOnceLinkage:
258         case GlobalValue::WeakLinkage:   // FIXME: Verify correct for weak.
259           // Nonnull linkonce -> weak
260           O << "\t.weak " << name << "\n";
261           O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
262           SwitchSection("", I);
263           break;
264         case GlobalValue::AppendingLinkage:
265           // FIXME: appending linkage variables should go into a section of
266           // their name or something.  For now, just emit them as external.
267         case GlobalValue::ExternalLinkage:
268           // If external or appending, declare as a global symbol
269           O << "\t.globl " << name << "\n";
270           // FALL THROUGH
271         case GlobalValue::InternalLinkage:
272           SwitchSection(C->isNullValue() ? "\t.section .bss" : 
273                         "\t.section .data", I);
274           break;
275         case GlobalValue::GhostLinkage:
276           std::cerr << "GhostLinkage cannot appear in AlphaAsmPrinter!\n";
277           abort();
278         }
279
280         EmitAlignment(Align);
281         O << "\t.type " << name << ",@object\n";
282         O << "\t.size " << name << "," << Size << "\n";
283         O << name << ":\n";
284         EmitGlobalConstant(C);
285       }
286     }
287
288   AsmPrinter::doFinalization(M);
289   return false;
290 }