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