Add shifts and reg-imm address matching
[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     bool printInstruction(const MachineInstr *MI);  // autogenerated.
56     void printMachineInstruction(const MachineInstr * MI);
57
58     void emitFunctionHeader(const MachineFunction &MF);
59     bool runOnMachineFunction(MachineFunction &F);
60     bool doInitialization(Module &M);
61     bool doFinalization(Module &M);
62
63     void getAnalysisUsage(AnalysisUsage &AU) const {
64       AsmPrinter::getAnalysisUsage(AU);
65       AU.setPreservesAll();
66     }
67   };
68 } // end of anonymous namespace
69
70 #include "SystemZGenAsmWriter.inc"
71
72 /// createSystemZCodePrinterPass - Returns a pass that prints the SystemZ
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::createSystemZCodePrinterPass(raw_ostream &o,
78                                                 SystemZTargetMachine &tm,
79                                                 CodeGenOpt::Level OptLevel,
80                                                 bool verbose) {
81   return new SystemZAsmPrinter(o, tm, tm.getTargetAsmInfo(), OptLevel, verbose);
82 }
83
84 bool SystemZAsmPrinter::doInitialization(Module &M) {
85   Mang = new Mangler(M, "", TAI->getPrivateGlobalPrefix());
86   return false; // success
87 }
88
89
90 bool SystemZAsmPrinter::doFinalization(Module &M) {
91   return AsmPrinter::doFinalization(M);
92 }
93
94 void SystemZAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
95   const Function *F = MF.getFunction();
96
97   SwitchToSection(TAI->SectionForGlobal(F));
98
99   unsigned FnAlign = 4;
100   if (F->hasFnAttr(Attribute::OptimizeForSize))
101     FnAlign = 1;
102
103   EmitAlignment(FnAlign, F);
104
105   switch (F->getLinkage()) {
106   default: assert(0 && "Unknown linkage type!");
107   case Function::InternalLinkage:  // Symbols default to internal.
108   case Function::PrivateLinkage:
109     break;
110   case Function::ExternalLinkage:
111     O << "\t.globl\t" << CurrentFnName << '\n';
112     break;
113   case Function::LinkOnceAnyLinkage:
114   case Function::LinkOnceODRLinkage:
115   case Function::WeakAnyLinkage:
116   case Function::WeakODRLinkage:
117     O << "\t.weak\t" << CurrentFnName << '\n';
118     break;
119   }
120
121   printVisibility(CurrentFnName, F->getVisibility());
122
123   O << "\t.type\t" << CurrentFnName << ",@function\n"
124     << CurrentFnName << ":\n";
125 }
126
127 bool SystemZAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
128   SetupMachineFunction(MF);
129   O << "\n\n";
130
131   // Print the 'header' of function
132   emitFunctionHeader(MF);
133
134   // Print out code for the function.
135   for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
136        I != E; ++I) {
137     // Print a label for the basic block.
138     if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
139       // This is an entry block or a block that's only reachable via a
140       // fallthrough edge. In non-VerboseAsm mode, don't print the label.
141     } else {
142       printBasicBlockLabel(I, true, true, VerboseAsm);
143       O << '\n';
144     }
145
146     for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
147          II != E; ++II)
148       // Print the assembly for the instruction.
149       printMachineInstruction(II);
150   }
151
152   if (TAI->hasDotTypeDotSizeDirective())
153     O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
154
155   O.flush();
156
157   // We didn't modify anything
158   return false;
159 }
160
161 void SystemZAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
162   ++EmittedInsts;
163
164   // Call the autogenerated instruction printer routines.
165   if (printInstruction(MI))
166     return;
167
168   assert(0 && "Should not happen");
169 }
170
171 void SystemZAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
172                                      const char* Modifier) {
173   const MachineOperand &MO = MI->getOperand(OpNum);
174   switch (MO.getType()) {
175   case MachineOperand::MO_Register:
176     assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
177             "Virtual registers should be already mapped!");
178     O << '%' << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
179     return;
180   case MachineOperand::MO_Immediate:
181     O << MO.getImm();
182     return;
183   case MachineOperand::MO_MachineBasicBlock:
184     printBasicBlockLabel(MO.getMBB());
185     return;
186   default:
187     assert(0 && "Not implemented yet!");
188   }
189 }
190
191 void SystemZAsmPrinter::printRIAddrOperand(const MachineInstr *MI, int OpNum,
192                                            const char* Modifier) {
193   const MachineOperand &Base = MI->getOperand(OpNum);
194
195   // Print displacement operand.
196   printOperand(MI, OpNum+1);
197
198   // Print base operand (if any)
199   if (!(Base.isReg() && Base.getReg() == SystemZ::R0D)) {
200     O << '(';
201     printOperand(MI, OpNum);
202     O << ')';
203   }
204 }
205