Print signed imms properly
[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     O << '%' << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
188     return;
189   case MachineOperand::MO_Immediate:
190     O << MO.getImm();
191     return;
192   case MachineOperand::MO_MachineBasicBlock:
193     printBasicBlockLabel(MO.getMBB());
194     return;
195   case MachineOperand::MO_GlobalAddress: {
196     std::string Name = Mang->getValueName(MO.getGlobal());
197     assert(MO.getOffset() == 0 && "No offsets allowed!");
198
199     O << Name;
200
201     return;
202   }
203   case MachineOperand::MO_ExternalSymbol: {
204     std::string Name(TAI->getGlobalPrefix());
205     Name += MO.getSymbolName();
206     O << Name;
207     return;
208   }
209   default:
210     assert(0 && "Not implemented yet!");
211   }
212 }
213
214 void SystemZAsmPrinter::printRIAddrOperand(const MachineInstr *MI, int OpNum,
215                                            const char* Modifier) {
216   const MachineOperand &Base = MI->getOperand(OpNum);
217
218   // Print displacement operand.
219   printOperand(MI, OpNum+1);
220
221   // Print base operand (if any)
222   if (Base.getReg()) {
223     O << '(';
224     printOperand(MI, OpNum);
225     O << ')';
226   }
227 }
228
229 void SystemZAsmPrinter::printRRIAddrOperand(const MachineInstr *MI, int OpNum,
230                                             const char* Modifier) {
231   const MachineOperand &Base = MI->getOperand(OpNum);
232   const MachineOperand &Index = MI->getOperand(OpNum+2);
233
234   // Print displacement operand.
235   printOperand(MI, OpNum+1);
236
237   // Print base operand (if any)
238   if (Base.getReg()) {
239     O << '(';
240     printOperand(MI, OpNum);
241     if (Index.getReg()) {
242       O << ',';
243       printOperand(MI, OpNum+2);
244     }
245     O << ')';
246   } else
247     assert(!Index.getReg() && "Should allocate base register first!");
248 }
249