Use the AsmPrinter for global variable init printing. This eliminates a
[oota-llvm.git] / lib / Target / SparcV8 / SparcV8AsmPrinter.cpp
1 //===-- SparcV8AsmPrinter.cpp - SparcV8 LLVM assembly writer --------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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 Sparc V8 assembly language.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "SparcV8.h"
16 #include "SparcV8InstrInfo.h"
17 #include "llvm/Constants.h"
18 #include "llvm/DerivedTypes.h"
19 #include "llvm/Module.h"
20 #include "llvm/Assembly/Writer.h"
21 #include "llvm/CodeGen/AsmPrinter.h"
22 #include "llvm/CodeGen/MachineFunctionPass.h"
23 #include "llvm/CodeGen/MachineConstantPool.h"
24 #include "llvm/CodeGen/MachineInstr.h"
25 #include "llvm/Target/TargetMachine.h"
26 #include "llvm/Support/Mangler.h"
27 #include "llvm/ADT/Statistic.h"
28 #include "llvm/ADT/StringExtras.h"
29 #include "llvm/Support/CommandLine.h"
30 #include "llvm/Support/MathExtras.h"
31 #include <cctype>
32 using namespace llvm;
33
34 namespace {
35   Statistic<> EmittedInsts("asm-printer", "Number of machine instrs printed");
36
37   struct SparcV8AsmPrinter : public AsmPrinter {
38     SparcV8AsmPrinter(std::ostream &O, TargetMachine &TM) : AsmPrinter(O, TM) {
39       Data16bitsDirective = "\t.half\t";
40       Data32bitsDirective = "\t.word\t";
41       Data64bitsDirective = "\t.xword\t";
42       ZeroDirective = 0;  // no .zero or .space!
43     }
44
45     /// We name each basic block in a Function with a unique number, so
46     /// that we can consistently refer to them later. This is cleared
47     /// at the beginning of each call to runOnMachineFunction().
48     ///
49     typedef std::map<const Value *, unsigned> ValueMapTy;
50     ValueMapTy NumberForBB;
51
52     virtual const char *getPassName() const {
53       return "SparcV8 Assembly Printer";
54     }
55
56     void printOperand(const MachineInstr *MI, int opNum);
57     bool printInstruction(const MachineInstr *MI);  // autogenerated.
58     bool runOnMachineFunction(MachineFunction &F);
59     bool doInitialization(Module &M);
60     bool doFinalization(Module &M);
61   };
62 } // end of anonymous namespace
63
64 #include "SparcV8GenAsmWriter.inc"
65
66 /// createSparcV8CodePrinterPass - Returns a pass that prints the SparcV8
67 /// assembly code for a MachineFunction to the given output stream,
68 /// using the given target machine description.  This should work
69 /// regardless of whether the function is in SSA form.
70 ///
71 FunctionPass *llvm::createSparcV8CodePrinterPass (std::ostream &o,
72                                                   TargetMachine &tm) {
73   return new SparcV8AsmPrinter(o, tm);
74 }
75
76 /// runOnMachineFunction - This uses the printMachineInstruction()
77 /// method to print assembly for each instruction.
78 ///
79 bool SparcV8AsmPrinter::runOnMachineFunction(MachineFunction &MF) {
80   SetupMachineFunction(MF);
81
82   // Print out constants referenced by the function
83   EmitConstantPool(MF.getConstantPool());
84
85   // BBNumber is used here so that a given Printer will never give two
86   // BBs the same name. (If you have a better way, please let me know!)
87   static unsigned BBNumber = 0;
88
89   O << "\n\n";
90   // What's my mangled name?
91   CurrentFnName = Mang->getValueName(MF.getFunction());
92
93   // Print out labels for the function.
94   O << "\t.text\n";
95   O << "\t.align 16\n";
96   O << "\t.globl\t" << CurrentFnName << "\n";
97   O << "\t.type\t" << CurrentFnName << ", #function\n";
98   O << CurrentFnName << ":\n";
99
100   // Number each basic block so that we can consistently refer to them
101   // in PC-relative references.
102   NumberForBB.clear();
103   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
104        I != E; ++I) {
105     NumberForBB[I->getBasicBlock()] = BBNumber++;
106   }
107
108   // Print out code for the function.
109   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
110        I != E; ++I) {
111     // Print a label for the basic block.
112     O << ".LBB" << Mang->getValueName(MF.getFunction ())
113       << "_" << I->getNumber () << ":\t! "
114       << I->getBasicBlock ()->getName () << "\n";
115     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
116          II != E; ++II) {
117       // Print the assembly for the instruction.
118       O << "\t";
119       printInstruction(II);
120       ++EmittedInsts;
121     }
122   }
123
124   // We didn't modify anything.
125   return false;
126 }
127
128 void SparcV8AsmPrinter::printOperand(const MachineInstr *MI, int opNum) {
129   const MachineOperand &MO = MI->getOperand (opNum);
130   const MRegisterInfo &RI = *TM.getRegisterInfo();
131   bool CloseParen = false;
132   if (MI->getOpcode() == V8::SETHIi && !MO.isRegister() && !MO.isImmediate()) {
133     O << "%hi(";
134     CloseParen = true;
135   } else if (MI->getOpcode() ==V8::ORri &&!MO.isRegister() &&!MO.isImmediate())
136   {
137     O << "%lo(";
138     CloseParen = true;
139   }
140   switch (MO.getType()) {
141   case MachineOperand::MO_VirtualRegister:
142     if (Value *V = MO.getVRegValueOrNull()) {
143       O << "<" << V->getName() << ">";
144       break;
145     }
146     // FALLTHROUGH
147   case MachineOperand::MO_MachineRegister:
148     if (MRegisterInfo::isPhysicalRegister(MO.getReg()))
149       O << "%" << LowercaseString (RI.get(MO.getReg()).Name);
150     else
151       O << "%reg" << MO.getReg();
152     break;
153
154   case MachineOperand::MO_SignExtendedImmed:
155   case MachineOperand::MO_UnextendedImmed:
156     O << (int)MO.getImmedValue();
157     break;
158   case MachineOperand::MO_MachineBasicBlock: {
159     MachineBasicBlock *MBBOp = MO.getMachineBasicBlock();
160     O << ".LBB" << Mang->getValueName(MBBOp->getParent()->getFunction())
161       << "_" << MBBOp->getNumber () << "\t! "
162       << MBBOp->getBasicBlock ()->getName ();
163     return;
164   }
165   case MachineOperand::MO_PCRelativeDisp:
166     std::cerr << "Shouldn't use addPCDisp() when building SparcV8 MachineInstrs";
167     abort ();
168     return;
169   case MachineOperand::MO_GlobalAddress:
170     O << Mang->getValueName(MO.getGlobal());
171     break;
172   case MachineOperand::MO_ExternalSymbol:
173     O << MO.getSymbolName();
174     break;
175   case MachineOperand::MO_ConstantPoolIndex:
176     O << PrivateGlobalPrefix << "CPI" << getFunctionNumber() << "_"
177       << MO.getConstantPoolIndex();
178     break;
179   default:
180     O << "<unknown operand type>"; abort (); break;
181   }
182   if (CloseParen) O << ")";
183 }
184
185 bool SparcV8AsmPrinter::doInitialization(Module &M) {
186   Mang = new Mangler(M);
187   return false; // success
188 }
189
190 bool SparcV8AsmPrinter::doFinalization(Module &M) {
191   const TargetData &TD = TM.getTargetData();
192
193   // Print out module-level global variables here.
194   for (Module::const_global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I)
195     if (I->hasInitializer()) {   // External global require no code
196       O << "\n\n";
197       std::string name = Mang->getValueName(I);
198       Constant *C = I->getInitializer();
199       unsigned Size = TD.getTypeSize(C->getType());
200       unsigned Align = TD.getTypeAlignment(C->getType());
201
202       if (C->isNullValue() &&
203           (I->hasLinkOnceLinkage() || I->hasInternalLinkage() ||
204            I->hasWeakLinkage() /* FIXME: Verify correct */)) {
205         SwitchSection(".data", I);
206         if (I->hasInternalLinkage())
207           O << "\t.local " << name << "\n";
208
209         O << "\t.comm " << name << "," << TD.getTypeSize(C->getType())
210           << "," << (unsigned)TD.getTypeAlignment(C->getType());
211         O << "\t\t! ";
212         WriteAsOperand(O, I, true, true, &M);
213         O << "\n";
214       } else {
215         switch (I->getLinkage()) {
216         case GlobalValue::LinkOnceLinkage:
217         case GlobalValue::WeakLinkage:   // FIXME: Verify correct for weak.
218           // Nonnull linkonce -> weak
219           O << "\t.weak " << name << "\n";
220           SwitchSection("", I);
221           O << "\t.section\t\".llvm.linkonce.d." << name
222             << "\",\"aw\",@progbits\n";
223           break;
224
225         case GlobalValue::AppendingLinkage:
226           // FIXME: appending linkage variables should go into a section of
227           // their name or something.  For now, just emit them as external.
228         case GlobalValue::ExternalLinkage:
229           // If external or appending, declare as a global symbol
230           O << "\t.globl " << name << "\n";
231           // FALL THROUGH
232         case GlobalValue::InternalLinkage:
233           if (C->isNullValue())
234             SwitchSection(".bss", I);
235           else
236             SwitchSection(".data", I);
237           break;
238         case GlobalValue::GhostLinkage:
239           std::cerr << "Should not have any unmaterialized functions!\n";
240           abort();
241         }
242
243         O << "\t.align " << Align << "\n";
244         O << "\t.type " << name << ",#object\n";
245         O << "\t.size " << name << "," << Size << "\n";
246         O << name << ":\t\t\t\t! ";
247         WriteAsOperand(O, I, true, true, &M);
248         O << " = ";
249         WriteAsOperand(O, C, false, false, &M);
250         O << "\n";
251         EmitGlobalConstant(C);
252       }
253     }
254
255   AsmPrinter::doFinalization(M);
256   return false; // success
257 }