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