Implement asmprinting for odd-even regpairs
[oota-llvm.git] / lib / Target / SystemZ / AsmPrinter / SystemZAsmPrinter.cpp
1 //===-- SystemZAsmPrinter.cpp - SystemZ 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 the SystemZ assembly language.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #define DEBUG_TYPE "asm-printer"
16 #include "SystemZ.h"
17 #include "SystemZInstrInfo.h"
18 #include "SystemZTargetMachine.h"
19 #include "llvm/Constants.h"
20 #include "llvm/DerivedTypes.h"
21 #include "llvm/Module.h"
22 #include "llvm/CodeGen/AsmPrinter.h"
23 #include "llvm/CodeGen/DwarfWriter.h"
24 #include "llvm/CodeGen/MachineModuleInfo.h"
25 #include "llvm/CodeGen/MachineFunctionPass.h"
26 #include "llvm/CodeGen/MachineConstantPool.h"
27 #include "llvm/CodeGen/MachineInstr.h"
28 #include "llvm/Target/TargetAsmInfo.h"
29 #include "llvm/Target/TargetData.h"
30 #include "llvm/ADT/Statistic.h"
31 #include "llvm/Support/Compiler.h"
32 #include "llvm/Support/Mangler.h"
33 #include "llvm/Support/raw_ostream.h"
34
35 using namespace llvm;
36
37 STATISTIC(EmittedInsts, "Number of machine instrs printed");
38
39 namespace {
40   class VISIBILITY_HIDDEN SystemZAsmPrinter : public AsmPrinter {
41   public:
42     SystemZAsmPrinter(raw_ostream &O, SystemZTargetMachine &TM,
43                      const TargetAsmInfo *TAI,
44                      CodeGenOpt::Level OL, bool V)
45       : AsmPrinter(O, TM, TAI, OL, V) {}
46
47     virtual const char *getPassName() const {
48       return "SystemZ Assembly Printer";
49     }
50
51     void printOperand(const MachineInstr *MI, int OpNum,
52                       const char* Modifier = 0);
53     void printRIAddrOperand(const MachineInstr *MI, int OpNum,
54                             const char* Modifier = 0);
55     void printRRIAddrOperand(const MachineInstr *MI, int OpNum,
56                              const char* Modifier = 0);
57     void printS16ImmOperand(const MachineInstr *MI, int OpNum) {
58       O << (int16_t)MI->getOperand(OpNum).getImm();
59     }
60     void printS32ImmOperand(const MachineInstr *MI, int OpNum) {
61       O << (int32_t)MI->getOperand(OpNum).getImm();
62     }
63
64     bool printInstruction(const MachineInstr *MI);  // autogenerated.
65     void printMachineInstruction(const MachineInstr * MI);
66
67     void emitFunctionHeader(const MachineFunction &MF);
68     bool runOnMachineFunction(MachineFunction &F);
69     bool doInitialization(Module &M);
70     bool doFinalization(Module &M);
71
72     void getAnalysisUsage(AnalysisUsage &AU) const {
73       AsmPrinter::getAnalysisUsage(AU);
74       AU.setPreservesAll();
75     }
76   };
77 } // end of anonymous namespace
78
79 #include "SystemZGenAsmWriter.inc"
80
81 /// createSystemZCodePrinterPass - Returns a pass that prints the SystemZ
82 /// assembly code for a MachineFunction to the given output stream,
83 /// using the given target machine description.  This should work
84 /// regardless of whether the function is in SSA form.
85 ///
86 FunctionPass *llvm::createSystemZCodePrinterPass(raw_ostream &o,
87                                                 SystemZTargetMachine &tm,
88                                                 CodeGenOpt::Level OptLevel,
89                                                 bool verbose) {
90   return new SystemZAsmPrinter(o, tm, tm.getTargetAsmInfo(), OptLevel, verbose);
91 }
92
93 bool SystemZAsmPrinter::doInitialization(Module &M) {
94   Mang = new Mangler(M, "", TAI->getPrivateGlobalPrefix());
95   return false; // success
96 }
97
98
99 bool SystemZAsmPrinter::doFinalization(Module &M) {
100   return AsmPrinter::doFinalization(M);
101 }
102
103 void SystemZAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
104   const Function *F = MF.getFunction();
105
106   SwitchToSection(TAI->SectionForGlobal(F));
107
108   unsigned FnAlign = 4;
109   if (F->hasFnAttr(Attribute::OptimizeForSize))
110     FnAlign = 1;
111
112   EmitAlignment(FnAlign, F);
113
114   switch (F->getLinkage()) {
115   default: assert(0 && "Unknown linkage type!");
116   case Function::InternalLinkage:  // Symbols default to internal.
117   case Function::PrivateLinkage:
118     break;
119   case Function::ExternalLinkage:
120     O << "\t.globl\t" << CurrentFnName << '\n';
121     break;
122   case Function::LinkOnceAnyLinkage:
123   case Function::LinkOnceODRLinkage:
124   case Function::WeakAnyLinkage:
125   case Function::WeakODRLinkage:
126     O << "\t.weak\t" << CurrentFnName << '\n';
127     break;
128   }
129
130   printVisibility(CurrentFnName, F->getVisibility());
131
132   O << "\t.type\t" << CurrentFnName << ",@function\n"
133     << CurrentFnName << ":\n";
134 }
135
136 bool SystemZAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
137   SetupMachineFunction(MF);
138   O << "\n\n";
139
140   // Print the 'header' of function
141   emitFunctionHeader(MF);
142
143   // Print out code for the function.
144   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
145        I != E; ++I) {
146     // Print a label for the basic block.
147     if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
148       // This is an entry block or a block that's only reachable via a
149       // fallthrough edge. In non-VerboseAsm mode, don't print the label.
150     } else {
151       printBasicBlockLabel(I, true, true, VerboseAsm);
152       O << '\n';
153     }
154
155     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
156          II != E; ++II)
157       // Print the assembly for the instruction.
158       printMachineInstruction(II);
159   }
160
161   if (TAI->hasDotTypeDotSizeDirective())
162     O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
163
164   O.flush();
165
166   // We didn't modify anything
167   return false;
168 }
169
170 void SystemZAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
171   ++EmittedInsts;
172
173   // Call the autogenerated instruction printer routines.
174   if (printInstruction(MI))
175     return;
176
177   assert(0 && "Should not happen");
178 }
179
180 void SystemZAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
181                                      const char* Modifier) {
182   const MachineOperand &MO = MI->getOperand(OpNum);
183   switch (MO.getType()) {
184   case MachineOperand::MO_Register: {
185     assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
186             "Virtual registers should be already mapped!");
187     unsigned Reg = MO.getReg();
188     if (Modifier && strncmp(Modifier, "subreg", 6) == 0) {
189       if (strncmp(Modifier + 7, "even", 4) == 0)
190         Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_EVEN);
191       else if (strncmp(Modifier + 7, "odd", 3) == 0)
192         Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_ODD);
193       else
194         assert(0 && "Invalid subreg modifier");
195     }
196
197     O << '%' << TRI->getAsmName(Reg);
198     return;
199   }
200   case MachineOperand::MO_Immediate:
201     O << MO.getImm();
202     return;
203   case MachineOperand::MO_MachineBasicBlock:
204     printBasicBlockLabel(MO.getMBB());
205     return;
206   case MachineOperand::MO_GlobalAddress: {
207     std::string Name = Mang->getValueName(MO.getGlobal());
208     assert(MO.getOffset() == 0 && "No offsets allowed!");
209
210     O << Name;
211
212     return;
213   }
214   case MachineOperand::MO_ExternalSymbol: {
215     std::string Name(TAI->getGlobalPrefix());
216     Name += MO.getSymbolName();
217     O << Name;
218     return;
219   }
220   default:
221     assert(0 && "Not implemented yet!");
222   }
223 }
224
225 void SystemZAsmPrinter::printRIAddrOperand(const MachineInstr *MI, int OpNum,
226                                            const char* Modifier) {
227   const MachineOperand &Base = MI->getOperand(OpNum);
228
229   // Print displacement operand.
230   printOperand(MI, OpNum+1);
231
232   // Print base operand (if any)
233   if (Base.getReg()) {
234     O << '(';
235     printOperand(MI, OpNum);
236     O << ')';
237   }
238 }
239
240 void SystemZAsmPrinter::printRRIAddrOperand(const MachineInstr *MI, int OpNum,
241                                             const char* Modifier) {
242   const MachineOperand &Base = MI->getOperand(OpNum);
243   const MachineOperand &Index = MI->getOperand(OpNum+2);
244
245   // Print displacement operand.
246   printOperand(MI, OpNum+1);
247
248   // Print base operand (if any)
249   if (Base.getReg()) {
250     O << '(';
251     printOperand(MI, OpNum);
252     if (Index.getReg()) {
253       O << ',';
254       printOperand(MI, OpNum+2);
255     }
256     O << ')';
257   } else
258     assert(!Index.getReg() && "Should allocate base register first!");
259 }
260