Now that we have everything nicely factored (e.g. asmprinter is not
[oota-llvm.git] / lib / Target / Alpha / AsmPrinter / 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/CodeGen/DwarfWriter.h"
24 #include "llvm/MC/MCStreamer.h"
25 #include "llvm/MC/MCAsmInfo.h"
26 #include "llvm/MC/MCSymbol.h"
27 #include "llvm/Target/TargetLoweringObjectFile.h"
28 #include "llvm/Target/TargetMachine.h"
29 #include "llvm/Target/TargetRegistry.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/Support/FormattedStream.h"
32 #include "llvm/ADT/Statistic.h"
33 using namespace llvm;
34
35 STATISTIC(EmittedInsts, "Number of machine instrs printed");
36
37 namespace {
38   struct AlphaAsmPrinter : public AsmPrinter {
39     /// Unique incrementer for label values for referencing Global values.
40     ///
41
42     explicit AlphaAsmPrinter(formatted_raw_ostream &o, TargetMachine &tm,
43                              const MCAsmInfo *T, bool V)
44       : AsmPrinter(o, tm, T, V) {}
45
46     virtual const char *getPassName() const {
47       return "Alpha Assembly Printer";
48     }
49     void printInstruction(const MachineInstr *MI);
50     static const char *getRegisterName(unsigned RegNo);
51
52     void printOp(const MachineOperand &MO, bool IsCallOp = false);
53     void printOperand(const MachineInstr *MI, int opNum);
54     void printBaseOffsetPair(const MachineInstr *MI, int i, bool brackets=true);
55     bool runOnMachineFunction(MachineFunction &F);
56     void EmitStartOfAsmFile(Module &M);
57
58     bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
59                          unsigned AsmVariant, const char *ExtraCode);
60     bool PrintAsmMemoryOperand(const MachineInstr *MI,
61                                unsigned OpNo,
62                                unsigned AsmVariant,
63                                const char *ExtraCode);
64   };
65 } // end of anonymous namespace
66
67 #include "AlphaGenAsmWriter.inc"
68
69 void AlphaAsmPrinter::printOperand(const MachineInstr *MI, int opNum)
70 {
71   const MachineOperand &MO = MI->getOperand(opNum);
72   if (MO.getType() == MachineOperand::MO_Register) {
73     assert(TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
74            "Not physreg??");
75     O << getRegisterName(MO.getReg());
76   } else if (MO.isImm()) {
77     O << MO.getImm();
78     assert(MO.getImm() < (1 << 30));
79   } else {
80     printOp(MO);
81   }
82 }
83
84
85 void AlphaAsmPrinter::printOp(const MachineOperand &MO, bool IsCallOp) {
86   switch (MO.getType()) {
87   case MachineOperand::MO_Register:
88     O << getRegisterName(MO.getReg());
89     return;
90
91   case MachineOperand::MO_Immediate:
92     llvm_unreachable("printOp() does not handle immediate values");
93     return;
94
95   case MachineOperand::MO_MachineBasicBlock:
96     O << *GetMBBSymbol(MO.getMBB()->getNumber());
97     return;
98
99   case MachineOperand::MO_ConstantPoolIndex:
100     O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << "_"
101       << MO.getIndex();
102     return;
103
104   case MachineOperand::MO_ExternalSymbol:
105     O << MO.getSymbolName();
106     return;
107
108   case MachineOperand::MO_GlobalAddress:
109     O << *GetGlobalValueSymbol(MO.getGlobal());
110     return;
111
112   case MachineOperand::MO_JumpTableIndex:
113     O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber()
114       << '_' << MO.getIndex();
115     return;
116
117   default:
118     O << "<unknown operand type: " << MO.getType() << ">";
119     return;
120   }
121 }
122
123 /// runOnMachineFunction - This uses the printMachineInstruction()
124 /// method to print assembly for each instruction.
125 ///
126 bool AlphaAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
127   this->MF = &MF;
128
129   SetupMachineFunction(MF);
130   O << "\n\n";
131
132   // Print out constants referenced by the function
133   EmitConstantPool(MF.getConstantPool());
134
135   // Print out jump tables referenced by the function
136   EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
137
138   // Print out labels for the function.
139   const Function *F = MF.getFunction();
140   OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
141
142   EmitAlignment(MF.getAlignment(), F);
143   switch (F->getLinkage()) {
144   default: llvm_unreachable("Unknown linkage type!");
145   case Function::InternalLinkage:  // Symbols default to internal.
146   case Function::PrivateLinkage:
147   case Function::LinkerPrivateLinkage:
148     break;
149   case Function::ExternalLinkage:
150     O << "\t.globl " << *CurrentFnSym << '\n';
151     break;
152   case Function::WeakAnyLinkage:
153   case Function::WeakODRLinkage:
154   case Function::LinkOnceAnyLinkage:
155   case Function::LinkOnceODRLinkage:
156     O << MAI->getWeakRefDirective() << *CurrentFnSym << '\n';
157     break;
158   }
159
160   printVisibility(CurrentFnSym, F->getVisibility());
161
162   O << "\t.ent " << *CurrentFnSym << "\n";
163
164   O << *CurrentFnSym << ":\n";
165
166   // Print out code for the function.
167   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
168        I != E; ++I) {
169     if (I != MF.begin())
170       EmitBasicBlockStart(I);
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       processDebugLoc(II, true);
177       printInstruction(II);
178       
179       if (VerboseAsm)
180         EmitComments(*II);
181       O << '\n';
182       processDebugLoc(II, false);
183     }
184   }
185
186   O << "\t.end " << *CurrentFnSym << "\n";
187
188   // We didn't modify anything.
189   return false;
190 }
191
192 void AlphaAsmPrinter::EmitStartOfAsmFile(Module &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 }
199
200 /// PrintAsmOperand - Print out an operand for an inline asm expression.
201 ///
202 bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
203                                       unsigned AsmVariant,
204                                       const char *ExtraCode) {
205   printOperand(MI, OpNo);
206   return false;
207 }
208
209 bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
210                                             unsigned OpNo,
211                                             unsigned AsmVariant,
212                                             const char *ExtraCode) {
213   if (ExtraCode && ExtraCode[0])
214     return true; // Unknown modifier.
215   O << "0(";
216   printOperand(MI, OpNo);
217   O << ")";
218   return false;
219 }
220
221 // Force static initialization.
222 extern "C" void LLVMInitializeAlphaAsmPrinter() { 
223   RegisterAsmPrinter<AlphaAsmPrinter> X(TheAlphaTarget);
224 }