blob: 0edf5203c64392c7e1410b1038821f2ec147b984 [file] [log] [blame]
Anton Korobeynikov32b7d5b2009-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 Gohman2aa282f2009-08-13 01:36:44 +000022#include "llvm/Assembly/Writer.h"
Anton Korobeynikov32b7d5b2009-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 Lattner73266f92009-08-19 05:49:37 +000029#include "llvm/MC/MCStreamer.h"
Chris Lattner621c44d2009-08-22 20:48:53 +000030#include "llvm/MC/MCAsmInfo.h"
Chris Lattnerc6f802d2009-09-13 17:14:04 +000031#include "llvm/MC/MCSymbol.h"
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +000032#include "llvm/Target/TargetData.h"
Chris Lattnerc4c40a92009-07-28 03:13:23 +000033#include "llvm/Target/TargetLoweringObjectFile.h"
Anton Korobeynikov147a9a72009-07-16 14:36:52 +000034#include "llvm/Target/TargetRegistry.h"
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +000035#include "llvm/ADT/Statistic.h"
Chris Lattner2aaa75e2009-11-07 09:20:54 +000036#include "llvm/Support/ErrorHandling.h"
Anton Korobeynikov147a9a72009-07-16 14:36:52 +000037#include "llvm/Support/FormattedStream.h"
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +000038using namespace llvm;
39
40STATISTIC(EmittedInsts, "Number of machine instrs printed");
41
42namespace {
Nick Lewycky492d06e2009-10-25 06:33:48 +000043 class SystemZAsmPrinter : public AsmPrinter {
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +000044 public:
Anton Korobeynikov147a9a72009-07-16 14:36:52 +000045 SystemZAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattnera5ef4d32009-08-22 21:43:10 +000046 const MCAsmInfo *MAI, bool V)
47 : AsmPrinter(O, TM, MAI, V) {}
Anton Korobeynikov32b7d5b2009-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 Korobeynikov1c2eedb2009-07-16 14:16:05 +000055 void printPCRelImmOperand(const MachineInstr *MI, int OpNum);
Anton Korobeynikova58fac92009-07-16 13:43:18 +000056 void printRIAddrOperand(const MachineInstr *MI, int OpNum,
57 const char* Modifier = 0);
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +000058 void printRRIAddrOperand(const MachineInstr *MI, int OpNum,
59 const char* Modifier = 0);
Anton Korobeynikovbf21bac2009-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 Lattnerddb259a2009-08-08 01:32:19 +000067 void printInstruction(const MachineInstr *MI); // autogenerated.
Chris Lattner213703c2009-09-13 20:19:22 +000068 static const char *getRegisterName(unsigned RegNo);
Chris Lattner92221692009-09-13 20:08:00 +000069
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +000070 void printMachineInstruction(const MachineInstr * MI);
71
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +000072 bool runOnMachineFunction(MachineFunction &F);
Anton Korobeynikov32b7d5b2009-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 Korobeynikov32b7d5b2009-07-16 13:27:25 +000083
Anton Korobeynikov32b7d5b2009-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 Lattner7eb11562010-01-27 00:17:20 +000090 EmitFunctionHeader();
Anton Korobeynikov32b7d5b2009-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 Gohman5fda4342009-10-06 17:38:38 +000096 EmitBasicBlockStart(I);
Anton Korobeynikov32b7d5b2009-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 Lattnerce409842010-01-17 21:43:43 +0000104 if (MAI->hasDotTypeDotSizeDirective())
105 O << "\t.size\t" << *CurrentFnSym << ", .-" << *CurrentFnSym << '\n';
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000106
Anton Korobeynikov23bf4412009-07-16 14:07:50 +0000107 // Print out jump tables referenced by the function.
Chris Lattner1f8c8922010-01-25 22:41:33 +0000108 EmitJumpTableInfo(MF);
Anton Korobeynikov23bf4412009-07-16 14:07:50 +0000109
Anton Korobeynikov32b7d5b2009-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 Patel5450fc12009-10-06 02:19:11 +0000117 processDebugLoc(MI, true);
Chris Lattner32d4cc72009-09-09 23:14:36 +0000118
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000119 // Call the autogenerated instruction printer routines.
Chris Lattner7d337492009-08-08 00:05:42 +0000120 printInstruction(MI);
Chris Lattner32d4cc72009-09-09 23:14:36 +0000121
David Greeneca9b04b2009-11-13 21:34:57 +0000122 if (VerboseAsm)
Chris Lattner32d4cc72009-09-09 23:14:36 +0000123 EmitComments(*MI);
124 O << '\n';
Devang Patel5450fc12009-10-06 02:19:11 +0000125
126 processDebugLoc(MI, false);
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000127}
128
Chris Lattnerc6f802d2009-09-13 17:14:04 +0000129void SystemZAsmPrinter::printPCRelImmOperand(const MachineInstr *MI, int OpNum){
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +0000130 const MachineOperand &MO = MI->getOperand(OpNum);
131 switch (MO.getType()) {
Anton Korobeynikovb4de0b82009-07-16 14:17:07 +0000132 case MachineOperand::MO_Immediate:
133 O << MO.getImm();
134 return;
135 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner84d5ca92010-01-26 04:55:51 +0000136 O << *MO.getMBB()->getSymbol(OutContext);
Anton Korobeynikovb4de0b82009-07-16 14:17:07 +0000137 return;
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +0000138 case MachineOperand::MO_GlobalAddress: {
139 const GlobalValue *GV = MO.getGlobal();
Chris Lattnerce409842010-01-17 21:43:43 +0000140 O << *GetGlobalValueSymbol(GV);
Anton Korobeynikov1c2eedb2009-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 Lattnera5ef4d32009-08-22 21:43:10 +0000152 std::string Name(MAI->getGlobalPrefix());
Anton Korobeynikov1c2eedb2009-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 Korobeynikov32b7d5b2009-07-16 13:27:25 +0000167void SystemZAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
Anton Korobeynikova58fac92009-07-16 13:43:18 +0000168 const char* Modifier) {
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000169 const MachineOperand &MO = MI->getOperand(OpNum);
170 switch (MO.getType()) {
Anton Korobeynikov8a563652009-07-16 14:04:01 +0000171 case MachineOperand::MO_Register: {
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000172 assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
173 "Virtual registers should be already mapped!");
Anton Korobeynikov8a563652009-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 Lattnerf0a25de2009-09-13 20:31:40 +0000184 O << '%' << getRegisterName(Reg);
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000185 return;
Anton Korobeynikov8a563652009-07-16 14:04:01 +0000186 }
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000187 case MachineOperand::MO_Immediate:
Anton Korobeynikov8eef4042009-07-16 14:01:10 +0000188 O << MO.getImm();
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000189 return;
190 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner84d5ca92010-01-26 04:55:51 +0000191 O << *MO.getMBB()->getSymbol(OutContext);
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000192 return;
Anton Korobeynikov23bf4412009-07-16 14:07:50 +0000193 case MachineOperand::MO_JumpTableIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000194 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Anton Korobeynikov23bf4412009-07-16 14:07:50 +0000195 << MO.getIndex();
196
197 return;
Anton Korobeynikov67565d62009-07-16 14:19:35 +0000198 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000199 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Anton Korobeynikov67565d62009-07-16 14:19:35 +0000200 << MO.getIndex();
201
202 printOffset(MO.getOffset());
203 break;
Chris Lattnerc0c8d7d2010-01-16 01:17:26 +0000204 case MachineOperand::MO_GlobalAddress:
Chris Lattnerce409842010-01-17 21:43:43 +0000205 O << *GetGlobalValueSymbol(MO.getGlobal());
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +0000206 break;
Anton Korobeynikov961cd4a2009-07-16 13:50:21 +0000207 case MachineOperand::MO_ExternalSymbol: {
Chris Lattnerce409842010-01-17 21:43:43 +0000208 O << *GetExternalSymbolSymbol(MO.getSymbolName());
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +0000209 break;
Anton Korobeynikov961cd4a2009-07-16 13:50:21 +0000210 }
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000211 default:
212 assert(0 && "Not implemented yet!");
213 }
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +0000214
215 switch (MO.getTargetFlags()) {
216 default:
Anton Korobeynikov917cbe12009-07-18 13:33:17 +0000217 llvm_unreachable("Unknown target flag on GV operand");
Anton Korobeynikov1c2eedb2009-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 Korobeynikov32b7d5b2009-07-16 13:27:25 +0000225}
Anton Korobeynikova58fac92009-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 Korobeynikov87b83aa2009-07-16 13:44:00 +0000235 if (Base.getReg()) {
Anton Korobeynikova58fac92009-07-16 13:43:18 +0000236 O << '(';
237 printOperand(MI, OpNum);
238 O << ')';
239 }
240}
241
Anton Korobeynikov87b83aa2009-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 Korobeynikovf7cefd92009-07-16 13:48:42 +0000245 const MachineOperand &Index = MI->getOperand(OpNum+2);
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000246
247 // Print displacement operand.
Anton Korobeynikovf7cefd92009-07-16 13:48:42 +0000248 printOperand(MI, OpNum+1);
Anton Korobeynikov87b83aa2009-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 Korobeynikovf7cefd92009-07-16 13:48:42 +0000256 printOperand(MI, OpNum+2);
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000257 }
258 O << ')';
259 } else
260 assert(!Index.getReg() && "Should allocate base register first!");
261}
262
Anton Korobeynikov147a9a72009-07-16 14:36:52 +0000263// Force static initialization.
264extern "C" void LLVMInitializeSystemZAsmPrinter() {
Daniel Dunbarc680b012009-07-25 06:49:55 +0000265 RegisterAsmPrinter<SystemZAsmPrinter> X(TheSystemZTarget);
Anton Korobeynikov147a9a72009-07-16 14:36:52 +0000266}