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