blob: 0edf5203c64392c7e1410b1038821f2ec147b984 [file] [log] [blame]
Anton Korobeynikov4403b932009-07-16 13:27:25 +00001//===-- 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"
Dan Gohmancf20ac42009-08-13 01:36:44 +000022#include "llvm/Assembly/Writer.h"
Anton Korobeynikov4403b932009-07-16 13:27:25 +000023#include "llvm/CodeGen/AsmPrinter.h"
24#include "llvm/CodeGen/DwarfWriter.h"
25#include "llvm/CodeGen/MachineModuleInfo.h"
26#include "llvm/CodeGen/MachineFunctionPass.h"
27#include "llvm/CodeGen/MachineConstantPool.h"
28#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner6c2f9e12009-08-19 05:49:37 +000029#include "llvm/MC/MCStreamer.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000030#include "llvm/MC/MCAsmInfo.h"
Chris Lattner325d3dc2009-09-13 17:14:04 +000031#include "llvm/MC/MCSymbol.h"
Anton Korobeynikov4403b932009-07-16 13:27:25 +000032#include "llvm/Target/TargetData.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000033#include "llvm/Target/TargetLoweringObjectFile.h"
Anton Korobeynikov7df84622009-07-16 14:36:52 +000034#include "llvm/Target/TargetRegistry.h"
Anton Korobeynikov4403b932009-07-16 13:27:25 +000035#include "llvm/ADT/Statistic.h"
Chris Lattner8f9b0f62009-11-07 09:20:54 +000036#include "llvm/Support/ErrorHandling.h"
Anton Korobeynikov7df84622009-07-16 14:36:52 +000037#include "llvm/Support/FormattedStream.h"
Anton Korobeynikov4403b932009-07-16 13:27:25 +000038using namespace llvm;
39
40STATISTIC(EmittedInsts, "Number of machine instrs printed");
41
42namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000043 class SystemZAsmPrinter : public AsmPrinter {
Anton Korobeynikov4403b932009-07-16 13:27:25 +000044 public:
Anton Korobeynikov7df84622009-07-16 14:36:52 +000045 SystemZAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattner33adcfb2009-08-22 21:43:10 +000046 const MCAsmInfo *MAI, bool V)
47 : AsmPrinter(O, TM, MAI, V) {}
Anton Korobeynikov4403b932009-07-16 13:27:25 +000048
49 virtual const char *getPassName() const {
50 return "SystemZ Assembly Printer";
51 }
52
53 void printOperand(const MachineInstr *MI, int OpNum,
54 const char* Modifier = 0);
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +000055 void printPCRelImmOperand(const MachineInstr *MI, int OpNum);
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +000056 void printRIAddrOperand(const MachineInstr *MI, int OpNum,
57 const char* Modifier = 0);
Anton Korobeynikov3360da92009-07-16 13:44:00 +000058 void printRRIAddrOperand(const MachineInstr *MI, int OpNum,
59 const char* Modifier = 0);
Anton Korobeynikovd3ba2f22009-07-16 14:02:45 +000060 void printS16ImmOperand(const MachineInstr *MI, int OpNum) {
61 O << (int16_t)MI->getOperand(OpNum).getImm();
62 }
63 void printS32ImmOperand(const MachineInstr *MI, int OpNum) {
64 O << (int32_t)MI->getOperand(OpNum).getImm();
65 }
66
Chris Lattner41aefdc2009-08-08 01:32:19 +000067 void printInstruction(const MachineInstr *MI); // autogenerated.
Chris Lattnerd95148f2009-09-13 20:19:22 +000068 static const char *getRegisterName(unsigned RegNo);
Chris Lattner05af2612009-09-13 20:08:00 +000069
Anton Korobeynikov4403b932009-07-16 13:27:25 +000070 void printMachineInstruction(const MachineInstr * MI);
71
Anton Korobeynikov4403b932009-07-16 13:27:25 +000072 bool runOnMachineFunction(MachineFunction &F);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000073
74 void getAnalysisUsage(AnalysisUsage &AU) const {
75 AsmPrinter::getAnalysisUsage(AU);
76 AU.setPreservesAll();
77 }
78 };
79} // end of anonymous namespace
80
81#include "SystemZGenAsmWriter.inc"
82
Anton Korobeynikov4403b932009-07-16 13:27:25 +000083
Anton Korobeynikov4403b932009-07-16 13:27:25 +000084
85bool SystemZAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
86 SetupMachineFunction(MF);
87 O << "\n\n";
88
89 // Print the 'header' of function
Chris Lattner4129ccd2010-01-27 00:17:20 +000090 EmitFunctionHeader();
Anton Korobeynikov4403b932009-07-16 13:27:25 +000091
92 // Print out code for the function.
93 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
94 I != E; ++I) {
95 // Print a label for the basic block.
Dan Gohmane3cc3f32009-10-06 17:38:38 +000096 EmitBasicBlockStart(I);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000097
98 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
99 II != E; ++II)
100 // Print the assembly for the instruction.
101 printMachineInstruction(II);
102 }
103
Chris Lattner10b318b2010-01-17 21:43:43 +0000104 if (MAI->hasDotTypeDotSizeDirective())
105 O << "\t.size\t" << *CurrentFnSym << ", .-" << *CurrentFnSym << '\n';
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000106
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +0000107 // Print out jump tables referenced by the function.
Chris Lattner44e87252010-01-25 22:41:33 +0000108 EmitJumpTableInfo(MF);
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +0000109
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000110 // We didn't modify anything
111 return false;
112}
113
114void SystemZAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
115 ++EmittedInsts;
116
Devang Patelaf0e2722009-10-06 02:19:11 +0000117 processDebugLoc(MI, true);
Chris Lattnerc5ea2632009-09-09 23:14:36 +0000118
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000119 // Call the autogenerated instruction printer routines.
Chris Lattner2698cb62009-08-08 00:05:42 +0000120 printInstruction(MI);
Chris Lattnerc5ea2632009-09-09 23:14:36 +0000121
David Greene1924aab2009-11-13 21:34:57 +0000122 if (VerboseAsm)
Chris Lattnerc5ea2632009-09-09 23:14:36 +0000123 EmitComments(*MI);
124 O << '\n';
Devang Patelaf0e2722009-10-06 02:19:11 +0000125
126 processDebugLoc(MI, false);
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000127}
128
Chris Lattner325d3dc2009-09-13 17:14:04 +0000129void SystemZAsmPrinter::printPCRelImmOperand(const MachineInstr *MI, int OpNum){
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000130 const MachineOperand &MO = MI->getOperand(OpNum);
131 switch (MO.getType()) {
Anton Korobeynikovcd3dfaf2009-07-16 14:17:07 +0000132 case MachineOperand::MO_Immediate:
133 O << MO.getImm();
134 return;
135 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerf71cb012010-01-26 04:55:51 +0000136 O << *MO.getMBB()->getSymbol(OutContext);
Anton Korobeynikovcd3dfaf2009-07-16 14:17:07 +0000137 return;
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000138 case MachineOperand::MO_GlobalAddress: {
139 const GlobalValue *GV = MO.getGlobal();
Chris Lattner10b318b2010-01-17 21:43:43 +0000140 O << *GetGlobalValueSymbol(GV);
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000141
142 // Assemble calls via PLT for externally visible symbols if PIC.
143 if (TM.getRelocationModel() == Reloc::PIC_ &&
144 !GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
145 !GV->hasLocalLinkage())
146 O << "@PLT";
147
148 printOffset(MO.getOffset());
149 return;
150 }
151 case MachineOperand::MO_ExternalSymbol: {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000152 std::string Name(MAI->getGlobalPrefix());
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000153 Name += MO.getSymbolName();
154 O << Name;
155
156 if (TM.getRelocationModel() == Reloc::PIC_)
157 O << "@PLT";
158
159 return;
160 }
161 default:
162 assert(0 && "Not implemented yet!");
163 }
164}
165
166
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000167void SystemZAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000168 const char* Modifier) {
Anton Korobeynikov1cc9dc72009-07-16 13:29:38 +0000169 const MachineOperand &MO = MI->getOperand(OpNum);
170 switch (MO.getType()) {
Anton Korobeynikoved002122009-07-16 14:04:01 +0000171 case MachineOperand::MO_Register: {
Anton Korobeynikov1cc9dc72009-07-16 13:29:38 +0000172 assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
173 "Virtual registers should be already mapped!");
Anton Korobeynikoved002122009-07-16 14:04:01 +0000174 unsigned Reg = MO.getReg();
175 if (Modifier && strncmp(Modifier, "subreg", 6) == 0) {
176 if (strncmp(Modifier + 7, "even", 4) == 0)
177 Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_EVEN);
178 else if (strncmp(Modifier + 7, "odd", 3) == 0)
179 Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_ODD);
180 else
181 assert(0 && "Invalid subreg modifier");
182 }
183
Chris Lattner762ccea2009-09-13 20:31:40 +0000184 O << '%' << getRegisterName(Reg);
Anton Korobeynikov1cc9dc72009-07-16 13:29:38 +0000185 return;
Anton Korobeynikoved002122009-07-16 14:04:01 +0000186 }
Anton Korobeynikov1cc9dc72009-07-16 13:29:38 +0000187 case MachineOperand::MO_Immediate:
Anton Korobeynikov4cc7ce02009-07-16 14:01:10 +0000188 O << MO.getImm();
Anton Korobeynikov1cc9dc72009-07-16 13:29:38 +0000189 return;
190 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerf71cb012010-01-26 04:55:51 +0000191 O << *MO.getMBB()->getSymbol(OutContext);
Anton Korobeynikov1cc9dc72009-07-16 13:29:38 +0000192 return;
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +0000193 case MachineOperand::MO_JumpTableIndex:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000194 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +0000195 << MO.getIndex();
196
197 return;
Anton Korobeynikovae535672009-07-16 14:19:35 +0000198 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000199 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Anton Korobeynikovae535672009-07-16 14:19:35 +0000200 << MO.getIndex();
201
202 printOffset(MO.getOffset());
203 break;
Chris Lattner5c40e692010-01-16 01:17:26 +0000204 case MachineOperand::MO_GlobalAddress:
Chris Lattner10b318b2010-01-17 21:43:43 +0000205 O << *GetGlobalValueSymbol(MO.getGlobal());
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000206 break;
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000207 case MachineOperand::MO_ExternalSymbol: {
Chris Lattner10b318b2010-01-17 21:43:43 +0000208 O << *GetExternalSymbolSymbol(MO.getSymbolName());
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000209 break;
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000210 }
Anton Korobeynikov1cc9dc72009-07-16 13:29:38 +0000211 default:
212 assert(0 && "Not implemented yet!");
213 }
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000214
215 switch (MO.getTargetFlags()) {
216 default:
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000217 llvm_unreachable("Unknown target flag on GV operand");
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000218 case SystemZII::MO_NO_FLAG:
219 break;
220 case SystemZII::MO_GOTENT: O << "@GOTENT"; break;
221 case SystemZII::MO_PLT: O << "@PLT"; break;
222 }
223
224 printOffset(MO.getOffset());
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000225}
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000226
227void SystemZAsmPrinter::printRIAddrOperand(const MachineInstr *MI, int OpNum,
228 const char* Modifier) {
229 const MachineOperand &Base = MI->getOperand(OpNum);
230
231 // Print displacement operand.
232 printOperand(MI, OpNum+1);
233
234 // Print base operand (if any)
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000235 if (Base.getReg()) {
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000236 O << '(';
237 printOperand(MI, OpNum);
238 O << ')';
239 }
240}
241
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000242void SystemZAsmPrinter::printRRIAddrOperand(const MachineInstr *MI, int OpNum,
243 const char* Modifier) {
244 const MachineOperand &Base = MI->getOperand(OpNum);
Anton Korobeynikovc4368a12009-07-16 13:48:42 +0000245 const MachineOperand &Index = MI->getOperand(OpNum+2);
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000246
247 // Print displacement operand.
Anton Korobeynikovc4368a12009-07-16 13:48:42 +0000248 printOperand(MI, OpNum+1);
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000249
250 // Print base operand (if any)
251 if (Base.getReg()) {
252 O << '(';
253 printOperand(MI, OpNum);
254 if (Index.getReg()) {
255 O << ',';
Anton Korobeynikovc4368a12009-07-16 13:48:42 +0000256 printOperand(MI, OpNum+2);
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000257 }
258 O << ')';
259 } else
260 assert(!Index.getReg() && "Should allocate base register first!");
261}
262
Anton Korobeynikov7df84622009-07-16 14:36:52 +0000263// Force static initialization.
264extern "C" void LLVMInitializeSystemZAsmPrinter() {
Daniel Dunbar0c795d62009-07-25 06:49:55 +0000265 RegisterAsmPrinter<SystemZAsmPrinter> X(TheSystemZTarget);
Anton Korobeynikov7df84622009-07-16 14:36:52 +0000266}