49023688f7d549d3786496dae4b4874aeee99318
[oota-llvm.git] / lib / Target / Alpha / AsmPrinter / AlphaAsmPrinter.cpp
1 //===-- AlphaAsmPrinter.cpp - Alpha LLVM assembly writer ------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // 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 #define DEBUG_TYPE "asm-printer"
16 #include "Alpha.h"
17 #include "AlphaInstrInfo.h"
18 #include "AlphaTargetMachine.h"
19 #include "llvm/Module.h"
20 #include "llvm/Type.h"
21 #include "llvm/Assembly/Writer.h"
22 #include "llvm/CodeGen/AsmPrinter.h"
23 #include "llvm/CodeGen/DwarfWriter.h"
24 #include "llvm/MC/MCStreamer.h"
25 #include "llvm/MC/MCAsmInfo.h"
26 #include "llvm/MC/MCSymbol.h"
27 #include "llvm/Target/TargetLoweringObjectFile.h"
28 #include "llvm/Target/TargetMachine.h"
29 #include "llvm/Target/TargetRegistry.h"
30 #include "llvm/Support/Compiler.h"
31 #include "llvm/Support/ErrorHandling.h"
32 #include "llvm/Support/Mangler.h"
33 #include "llvm/Support/FormattedStream.h"
34 #include "llvm/ADT/Statistic.h"
35 using namespace llvm;
36
37 STATISTIC(EmittedInsts, "Number of machine instrs printed");
38
39 namespace {
40   struct VISIBILITY_HIDDEN AlphaAsmPrinter : public AsmPrinter {
41     /// Unique incrementer for label values for referencing Global values.
42     ///
43
44     explicit AlphaAsmPrinter(formatted_raw_ostream &o, TargetMachine &tm,
45                              const MCAsmInfo *T, bool V)
46       : AsmPrinter(o, tm, T, V) {}
47
48     virtual const char *getPassName() const {
49       return "Alpha Assembly Printer";
50     }
51     void printInstruction(const MachineInstr *MI);
52     static const char *getRegisterName(unsigned RegNo);
53
54     void printOp(const MachineOperand &MO, bool IsCallOp = false);
55     void printOperand(const MachineInstr *MI, int opNum);
56     void printBaseOffsetPair(const MachineInstr *MI, int i, bool brackets=true);
57     void PrintGlobalVariable(const GlobalVariable *GVar);
58     bool runOnMachineFunction(MachineFunction &F);
59     bool doInitialization(Module &M);
60
61     bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
62                          unsigned AsmVariant, const char *ExtraCode);
63     bool PrintAsmMemoryOperand(const MachineInstr *MI,
64                                unsigned OpNo,
65                                unsigned AsmVariant,
66                                const char *ExtraCode);
67   };
68 } // end of anonymous namespace
69
70 #include "AlphaGenAsmWriter.inc"
71
72 void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum)
73 {
74   const MachineOperand &MO = MI->getOperand(opNum);
75   if (MO.getType() == MachineOperand::MO_Register) {
76     assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
77            "Not physreg??");
78     O << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
79   } else if (MO.isImm()) {
80     O << MO.getImm();
81     assert(MO.getImm() < (1 << 30));
82   } else {
83     printOp(MO);
84   }
85 }
86
87
88 void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
89   const TargetRegisterInfo &RI = *TM.getRegisterInfo();
90
91   switch (MO.getType()) {
92   case MachineOperand::MO_Register:
93     O << RI.get(MO.getReg()).AsmName;
94     return;
95
96   case MachineOperand::MO_Immediate:
97     llvm_unreachable("printOp() does not handle immediate values");
98     return;
99
100   case MachineOperand::MO_MachineBasicBlock:
101     GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
102     return;
103
104   case MachineOperand::MO_ConstantPoolIndex:
105     O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
106       << MO.getIndex();
107     return;
108
109   case MachineOperand::MO_ExternalSymbol:
110     O << MO.getSymbolName();
111     return;
112
113   case MachineOperand::MO_GlobalAddress:
114     O << Mang->getMangledName(MO.getGlobal());
115     return;
116
117   case MachineOperand::MO_JumpTableIndex:
118     O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
119       << '_' << MO.getIndex();
120     return;
121
122   default:
123     O << "<unknown operand type: " << MO.getType() << ">";
124     return;
125   }
126 }
127
128 /// runOnMachineFunction - This uses the printMachineInstruction()
129 /// method to print assembly for each instruction.
130 ///
131 bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
132   this->MF = &MF;
133
134   SetupMachineFunction(MF);
135   O << "\n\n";
136
137   // Print out constants referenced by the function
138   EmitConstantPool(MF.getConstantPool());
139
140   // Print out jump tables referenced by the function
141   EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
142
143   // Print out labels for the function.
144   const Function *F = MF.getFunction();
145   OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
146
147   EmitAlignment(MF.getAlignment(), F);
148   switch (F->getLinkage()) {
149   default: llvm_unreachable("Unknown linkage type!");
150   case Function::InternalLinkage:  // Symbols default to internal.
151   case Function::PrivateLinkage:
152   case Function::LinkerPrivateLinkage:
153     break;
154    case Function::ExternalLinkage:
155      O << "\t.globl " << CurrentFnName << "\n";
156      break;
157   case Function::WeakAnyLinkage:
158   case Function::WeakODRLinkage:
159   case Function::LinkOnceAnyLinkage:
160   case Function::LinkOnceODRLinkage:
161     O << MAI->getWeakRefDirective() << CurrentFnName << "\n";
162     break;
163   }
164
165   printVisibility(CurrentFnName, F->getVisibility());
166
167   O << "\t.ent " << CurrentFnName << "\n";
168
169   O << CurrentFnName << ":\n";
170
171   // Print out code for the function.
172   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
173        I != E; ++I) {
174     if (I != MF.begin()) {
175       EmitBasicBlockStart(I);
176       O << '\n';
177     }
178     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
179          II != E; ++II) {
180       // Print the assembly for the instruction.
181       ++EmittedInsts;
182       processDebugLoc(II->getDebugLoc());
183       
184       printInstruction(II);
185       
186       if (VerboseAsm && !II->getDebugLoc().isUnknown())
187         EmitComments(*II);
188       O << '\n';
189     }
190   }
191
192   O << "\t.end " << CurrentFnName << "\n";
193
194   // We didn't modify anything.
195   return false;
196 }
197
198 bool AlphaAsmPrinter::doInitialization(Module &M)
199 {
200   if(TM.getSubtarget<AlphaSubtarget>().hasCT())
201     O << "\t.arch ev6\n"; //This might need to be ev67, so leave this test here
202   else
203     O << "\t.arch ev6\n";
204   O << "\t.set noat\n";
205   return AsmPrinter::doInitialization(M);
206 }
207
208 void AlphaAsmPrinter::PrintGlobalVariable(const GlobalVariable *GVar) {
209   const TargetData *TD = TM.getTargetData();
210
211   if (!GVar->hasInitializer()) return;  // External global require no code
212
213   // Check to see if this is a special global used by LLVM, if so, emit it.
214   if (EmitSpecialLLVMGlobal(GVar))
215     return;
216
217   std::string name = Mang->getMangledName(GVar);
218   Constant *C = GVar->getInitializer();
219   unsigned Size = TD->getTypeAllocSize(C->getType());
220   unsigned Align = TD->getPreferredAlignmentLog(GVar);
221
222   // 0: Switch to section
223   OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GVar, Mang,
224                                                                   TM));
225
226   // 1: Check visibility
227   printVisibility(name, GVar->getVisibility());
228
229   // 2: Kind
230   switch (GVar->getLinkage()) {
231    case GlobalValue::LinkOnceAnyLinkage:
232    case GlobalValue::LinkOnceODRLinkage:
233    case GlobalValue::WeakAnyLinkage:
234    case GlobalValue::WeakODRLinkage:
235    case GlobalValue::CommonLinkage:
236     O << MAI->getWeakRefDirective() << name << '\n';
237     break;
238    case GlobalValue::AppendingLinkage:
239    case GlobalValue::ExternalLinkage:
240       O << MAI->getGlobalDirective() << name << "\n";
241       break;
242     case GlobalValue::InternalLinkage:
243     case GlobalValue::PrivateLinkage:
244     case GlobalValue::LinkerPrivateLinkage:
245       break;
246     default:
247       llvm_unreachable("Unknown linkage type!");
248     }
249
250   // 3: Type, Size, Align
251   if (MAI->hasDotTypeDotSizeDirective()) {
252     O << "\t.type\t" << name << ", @object\n";
253     O << "\t.size\t" << name << ", " << Size << "\n";
254   }
255
256   EmitAlignment(Align, GVar);
257
258   O << name << ":\n";
259
260   EmitGlobalConstant(C);
261   O << '\n';
262 }
263
264 /// PrintAsmOperand - Print out an operand for an inline asm expression.
265 ///
266 bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
267                                       unsigned AsmVariant,
268                                       const char *ExtraCode) {
269   printOperand(MI, OpNo);
270   return false;
271 }
272
273 bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
274                                             unsigned OpNo,
275                                             unsigned AsmVariant,
276                                             const char *ExtraCode) {
277   if (ExtraCode && ExtraCode[0])
278     return true; // Unknown modifier.
279   O << "0(";
280   printOperand(MI, OpNo);
281   O << ")";
282   return false;
283 }
284
285 // Force static initialization.
286 extern "C" void LLVMInitializeAlphaAsmPrinter() { 
287   RegisterAsmPrinter<AlphaAsmPrinter> X(TheAlphaTarget);
288 }