add a new MachineBasicBlock::getSymbol method, replacing
[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 << *MO.getMBB()->getSymbol(OutContext);
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   SetupMachineFunction(MF);
128   O << "\n\n";
129
130   // Print out constants referenced by the function
131   EmitConstantPool(MF.getConstantPool());
132
133   // Print out jump tables referenced by the function
134   EmitJumpTableInfo(MF);
135
136   // Print out labels for the function.
137   const Function *F = MF.getFunction();
138   OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
139
140   EmitAlignment(MF.getAlignment(), F);
141   switch (F->getLinkage()) {
142   default: llvm_unreachable("Unknown linkage type!");
143   case Function::InternalLinkage:  // Symbols default to internal.
144   case Function::PrivateLinkage:
145   case Function::LinkerPrivateLinkage:
146     break;
147   case Function::ExternalLinkage:
148     O << "\t.globl " << *CurrentFnSym << '\n';
149     break;
150   case Function::WeakAnyLinkage:
151   case Function::WeakODRLinkage:
152   case Function::LinkOnceAnyLinkage:
153   case Function::LinkOnceODRLinkage:
154     O << MAI->getWeakRefDirective() << *CurrentFnSym << '\n';
155     break;
156   }
157
158   printVisibility(CurrentFnSym, F->getVisibility());
159
160   O << "\t.ent " << *CurrentFnSym << "\n";
161
162   O << *CurrentFnSym << ":\n";
163
164   // Print out code for the function.
165   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
166        I != E; ++I) {
167     if (I != MF.begin())
168       EmitBasicBlockStart(I);
169
170     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
171          II != E; ++II) {
172       // Print the assembly for the instruction.
173       ++EmittedInsts;
174       processDebugLoc(II, true);
175       printInstruction(II);
176       
177       if (VerboseAsm)
178         EmitComments(*II);
179       O << '\n';
180       processDebugLoc(II, false);
181     }
182   }
183
184   O << "\t.end " << *CurrentFnSym << "\n";
185
186   // We didn't modify anything.
187   return false;
188 }
189
190 void AlphaAsmPrinter::EmitStartOfAsmFile(Module &M) {
191   if (TM.getSubtarget<AlphaSubtarget>().hasCT())
192     O << "\t.arch ev6\n"; //This might need to be ev67, so leave this test here
193   else
194     O << "\t.arch ev6\n";
195   O << "\t.set noat\n";
196 }
197
198 /// PrintAsmOperand - Print out an operand for an inline asm expression.
199 ///
200 bool AlphaAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
201                                       unsigned AsmVariant,
202                                       const char *ExtraCode) {
203   printOperand(MI, OpNo);
204   return false;
205 }
206
207 bool AlphaAsmPrinter::PrintAsmMemoryOperand(const MachineInstr *MI,
208                                             unsigned OpNo,
209                                             unsigned AsmVariant,
210                                             const char *ExtraCode) {
211   if (ExtraCode && ExtraCode[0])
212     return true; // Unknown modifier.
213   O << "0(";
214   printOperand(MI, OpNo);
215   O << ")";
216   return false;
217 }
218
219 // Force static initialization.
220 extern "C" void LLVMInitializeAlphaAsmPrinter() { 
221   RegisterAsmPrinter<AlphaAsmPrinter> X(TheAlphaTarget);
222 }