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