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