Switch Alpha to new section handling stuff
[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   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       printBasicBlockLabel(I, true, true);
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       if (!printInstruction(II)) {
183         assert(0 && "Unhandled instruction in asm writer!");
184         abort();
185       }
186     }
187   }
188
189   O << "\t.end " << CurrentFnName << "\n";
190
191   // We didn't modify anything.
192   return false;
193 }
194
195 bool AlphaAsmPrinter::doInitialization(Module &M)
196 {
197   if(TM.getSubtarget<AlphaSubtarget>().hasCT())
198     O << "\t.arch ev6\n"; //This might need to be ev67, so leave this test here
199   else
200     O << "\t.arch ev6\n";
201   O << "\t.set noat\n";
202   return AsmPrinter::doInitialization(M);
203 }
204
205 void AlphaAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
206   const TargetData *TD = TM.getTargetData();
207
208   if (!GVar->hasInitializer()) return;  // External global require no code
209
210   // Check to see if this is a special global used by LLVM, if so, emit it.
211   if (EmitSpecialLLVMGlobal(GVar))
212     return;
213
214   std::string SectionName = TAI->SectionForGlobal(GVar);
215   std::string name = Mang->getValueName(GVar);
216   Constant *C = GVar->getInitializer();
217   unsigned Size = TD->getABITypeSize(C->getType());
218   unsigned Align = TD->getPreferredAlignmentLog(GVar);
219
220   // 0: Switch to section
221   SwitchToDataSection(SectionName.c_str());
222
223   // 1: Check visibility
224   if (GVar->hasHiddenVisibility())
225     O << TAI->getHiddenDirective() << name << "\n";
226
227   // 2: Kind
228   switch (GVar->getLinkage()) {
229    case GlobalValue::LinkOnceLinkage:
230    case GlobalValue::WeakLinkage:
231    case GlobalValue::CommonLinkage:
232     O << TAI->getWeakRefDirective() << name << '\n';
233     break;
234    case GlobalValue::AppendingLinkage:
235    case GlobalValue::ExternalLinkage:
236       O << TAI->getGlobalDirective() << name << "\n";
237       break;
238     case GlobalValue::InternalLinkage:
239       break;
240     default:
241       assert(0 && "Unknown linkage type!");
242       cerr << "Unknown linkage type!\n";
243       abort();
244     }
245
246   // 3: Type, Size, Align
247   if (TAI->hasDotTypeDotSizeDirective()) {
248     O << "\t.type\t" << name << ", @object\n";
249     O << "\t.size\t" << name << ", " << Size << "\n";
250   }
251
252   EmitAlignment(Align, GVar);
253
254   O << name << ":\n";
255
256   // If the initializer is a extern weak symbol, remember to emit the weak
257   // reference!
258   if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
259     if (GV->hasExternalWeakLinkage())
260       ExtWeakSymbols.insert(GV);
261
262   EmitGlobalConstant(C);
263   O << '\n';
264 }
265
266 bool AlphaAsmPrinter::doFinalization(Module &M) {
267   for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
268        I != E; ++I)
269     printModuleLevelGV(I);
270
271   return AsmPrinter::doFinalization(M);
272 }
273
274 /// PrintAsmOperand - Print out an operand for an inline asm expression.
275 ///
276 bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
277                                       unsigned AsmVariant,
278                                       const char *ExtraCode) {
279   printOperand(MI, OpNo);
280   return false;
281 }
282
283 bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
284                                             unsigned OpNo,
285                                             unsigned AsmVariant,
286                                             const char *ExtraCode) {
287   if (ExtraCode && ExtraCode[0])
288     return true; // Unknown modifier.
289   O << "0(";
290   printOperand(MI, OpNo);
291   O << ")";
292   return false;
293 }