FTOIT and ITOFT are bit converts, and if we drop 21264s, are always available
[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 #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/Target/TargetAsmInfo.h"
24 #include "llvm/Target/TargetMachine.h"
25 #include "llvm/Support/Compiler.h"
26 #include "llvm/Support/Mangler.h"
27 #include "llvm/ADT/Statistic.h"
28 using namespace llvm;
29
30 STATISTIC(EmittedInsts, "Number of machine instrs printed");
31
32 namespace {
33   struct VISIBILITY_HIDDEN AlphaAsmPrinter : public AsmPrinter {
34
35     /// Unique incrementer for label values for referencing Global values.
36     ///
37
38     AlphaAsmPrinter(std::ostream &o, TargetMachine &tm, const TargetAsmInfo *T)
39       : AsmPrinter(o, tm, T) {
40     }
41
42     virtual const char *getPassName() const {
43       return "Alpha Assembly Printer";
44     }
45     bool printInstruction(const MachineInstr *MI);
46     void printOp(const MachineOperand &MO, bool IsCallOp = false);
47     void printOperand(const MachineInstr *MI, int opNum);
48     void printBaseOffsetPair (const MachineInstr *MI, int i, bool brackets=true);
49     bool runOnMachineFunction(MachineFunction &F);
50     bool doInitialization(Module &M);
51     bool doFinalization(Module &M);
52     
53     bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
54                          unsigned AsmVariant, const char *ExtraCode);
55     bool PrintAsmMemoryOperand(const MachineInstr *MI, 
56                                unsigned OpNo,
57                                unsigned AsmVariant, 
58                                const char *ExtraCode);
59   };
60 } // end of anonymous namespace
61
62 /// createAlphaCodePrinterPass - Returns a pass that prints the Alpha
63 /// assembly code for a MachineFunction to the given output stream,
64 /// using the given target machine description.  This should work
65 /// regardless of whether the function is in SSA form.
66 ///
67 FunctionPass *llvm::createAlphaCodePrinterPass(std::ostream &o,
68                                                TargetMachine &tm) {
69   return new AlphaAsmPrinter(o, tm, tm.getTargetAsmInfo());
70 }
71
72 #include "AlphaGenAsmWriter.inc"
73
74 void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum)
75 {
76   const MachineOperand &MO = MI->getOperand(opNum);
77   if (MO.getType() == MachineOperand::MO_Register) {
78     assert(MRegisterInfo::isPhysicalRegister(MO.getReg())&&"Not physreg??");
79     O << TM.getRegisterInfo()->get(MO.getReg()).Name;
80   } else if (MO.isImmediate()) {
81     O << MO.getImmedValue();
82     assert(MO.getImmedValue() < (1 << 30));
83   } else {
84     printOp(MO);
85   }
86 }
87
88
89 void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
90   const MRegisterInfo &RI = *TM.getRegisterInfo();
91
92   switch (MO.getType()) {
93   case MachineOperand::MO_Register:
94     O << RI.get(MO.getReg()).Name;
95     return;
96
97   case MachineOperand::MO_Immediate:
98     cerr << "printOp() does not handle immediate values\n";
99     abort();
100     return;
101
102   case MachineOperand::MO_MachineBasicBlock:
103     printBasicBlockLabel(MO.getMachineBasicBlock());
104     return;
105
106   case MachineOperand::MO_ConstantPoolIndex:
107     O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
108       << MO.getConstantPoolIndex();
109     return;
110
111   case MachineOperand::MO_ExternalSymbol:
112     O << MO.getSymbolName();
113     return;
114
115   case MachineOperand::MO_GlobalAddress:
116     O << Mang->getValueName(MO.getGlobal());
117     return;
118
119   case MachineOperand::MO_JumpTableIndex:
120     O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
121       << '_' << MO.getJumpTableIndex();
122     return;
123
124   default:
125     O << "<unknown operand type: " << MO.getType() << ">";
126     return;
127   }
128 }
129
130 /// runOnMachineFunction - This uses the printMachineInstruction()
131 /// method to print assembly for each instruction.
132 ///
133 bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
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   SwitchToTextSection(getSectionForFunction(*F).c_str(), F);
146   
147   EmitAlignment(4, F);
148   switch (F->getLinkage()) {
149   default: assert(0 && "Unknown linkage type!");
150   case Function::InternalLinkage:  // Symbols default to internal.
151     break;
152    case Function::ExternalLinkage:
153      O << "\t.globl " << CurrentFnName << "\n";
154      break;
155   case Function::WeakLinkage:
156   case Function::LinkOnceLinkage:
157     O << TAI->getWeakRefDirective() << CurrentFnName << "\n";
158     break;
159   }
160
161   O << "\t.ent " << CurrentFnName << "\n";
162
163   O << CurrentFnName << ":\n";
164
165   // Print out code for the function.
166   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
167        I != E; ++I) {
168     if (I != MF.begin()) {
169       printBasicBlockLabel(I, true);
170       O << '\n';
171     }
172     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
173          II != E; ++II) {
174       // Print the assembly for the instruction.
175       ++EmittedInsts;
176       O << "\t";
177       if (!printInstruction(II)) {
178         assert(0 && "Unhandled instruction in asm writer!");
179         abort();
180       }
181     }
182   }
183
184   O << "\t.end " << CurrentFnName << "\n";
185
186   // We didn't modify anything.
187   return false;
188 }
189
190 bool AlphaAsmPrinter::doInitialization(Module &M)
191 {
192   AsmPrinter::doInitialization(M);
193   if(TM.getSubtarget<AlphaSubtarget>().hasCT())
194     O << "\t.arch ev6\n"; //This might need to be ev67, so leave this test here
195   else
196     O << "\t.arch ev6\n";
197   O << "\t.set noat\n";
198   return false;
199 }
200
201 bool AlphaAsmPrinter::doFinalization(Module &M) {
202   const TargetData *TD = TM.getTargetData();
203
204   for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
205     if (I->hasInitializer()) {   // External global require no code
206       // Check to see if this is a special global used by LLVM, if so, emit it.
207       if (EmitSpecialLLVMGlobal(I))
208         continue;
209       
210       O << "\n\n";
211       std::string name = Mang->getValueName(I);
212       Constant *C = I->getInitializer();
213       unsigned Size = TD->getTypeSize(C->getType());
214       //      unsigned Align = TD->getPreferredTypeAlignmentShift(C->getType());
215       unsigned Align = TD->getPreferredAlignmentLog(I);
216
217       if (C->isNullValue() &&
218           (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
219            I->hasWeakLinkage() /* FIXME: Verify correct */)) {
220         SwitchToDataSection("\t.section .data", I);
221         if (I->hasInternalLinkage())
222           O << "\t.local " << name << "\n";
223
224         O << "\t.comm " << name << "," << TD->getTypeSize(C->getType())
225           << "," << (1 << Align)
226           <<  "\n";
227       } else {
228         switch (I->getLinkage()) {
229         case GlobalValue::LinkOnceLinkage:
230         case GlobalValue::WeakLinkage:   // FIXME: Verify correct for weak.
231           // Nonnull linkonce -> weak
232           O << "\t.weak " << name << "\n";
233           O << "\t.section\t.llvm.linkonce.d." << name << ",\"aw\",@progbits\n";
234           SwitchToDataSection("", I);
235           break;
236         case GlobalValue::AppendingLinkage:
237           // FIXME: appending linkage variables should go into a section of
238           // their name or something.  For now, just emit them as external.
239         case GlobalValue::ExternalLinkage:
240           // If external or appending, declare as a global symbol
241           O << "\t.globl " << name << "\n";
242           // FALL THROUGH
243         case GlobalValue::InternalLinkage:
244           SwitchToDataSection(C->isNullValue() ? "\t.section .bss" : 
245                               "\t.section .data", I);
246           break;
247         case GlobalValue::GhostLinkage:
248           cerr << "GhostLinkage cannot appear in AlphaAsmPrinter!\n";
249           abort();
250         case GlobalValue::DLLImportLinkage:
251           cerr << "DLLImport linkage is not supported by this target!\n";
252           abort();
253         case GlobalValue::DLLExportLinkage:
254           cerr << "DLLExport linkage is not supported by this target!\n";
255           abort();
256         default:
257           assert(0 && "Unknown linkage type!");
258         }
259
260         EmitAlignment(Align);
261         O << "\t.type " << name << ",@object\n";
262         O << "\t.size " << name << "," << Size << "\n";
263         O << name << ":\n";
264         EmitGlobalConstant(C);
265       }
266     }
267
268   for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
269     if (I->hasExternalWeakLinkage()) {
270       O << "\n\n";
271       std::string name = Mang->getValueName(I);
272       O << "\t.weak " << name << "\n";
273     }
274
275   AsmPrinter::doFinalization(M);
276   return false;
277 }
278
279 /// PrintAsmOperand - Print out an operand for an inline asm expression.
280 ///
281 bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
282                                     unsigned AsmVariant, 
283                                     const char *ExtraCode) {
284   printOperand(MI, OpNo);
285   return false;
286 }
287
288 bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI, 
289                                             unsigned OpNo,
290                                             unsigned AsmVariant, 
291                                             const char *ExtraCode) {
292   if (ExtraCode && ExtraCode[0])
293     return true; // Unknown modifier.
294   O << "0(";
295   printOperand(MI, OpNo);
296   O << ")";
297   return false;
298 }