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