blob: 452dd124da7155b43ae2057572f8b7ff0b738bcc [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"
36#include "llvm/Support/Compiler.h"
Anton Korobeynikov7df84622009-07-16 14:36:52 +000037#include "llvm/Support/FormattedStream.h"
Anton Korobeynikov4403b932009-07-16 13:27:25 +000038#include "llvm/Support/Mangler.h"
Anton Korobeynikov4403b932009-07-16 13:27:25 +000039
40using namespace llvm;
41
42STATISTIC(EmittedInsts, "Number of machine instrs printed");
43
44namespace {
45 class VISIBILITY_HIDDEN SystemZAsmPrinter : public AsmPrinter {
46 public:
Anton Korobeynikov7df84622009-07-16 14:36:52 +000047 SystemZAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattner33adcfb2009-08-22 21:43:10 +000048 const MCAsmInfo *MAI, bool V)
49 : AsmPrinter(O, TM, MAI, V) {}
Anton Korobeynikov4403b932009-07-16 13:27:25 +000050
51 virtual const char *getPassName() const {
52 return "SystemZ Assembly Printer";
53 }
54
55 void printOperand(const MachineInstr *MI, int OpNum,
56 const char* Modifier = 0);
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +000057 void printPCRelImmOperand(const MachineInstr *MI, int OpNum);
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +000058 void printRIAddrOperand(const MachineInstr *MI, int OpNum,
59 const char* Modifier = 0);
Anton Korobeynikov3360da92009-07-16 13:44:00 +000060 void printRRIAddrOperand(const MachineInstr *MI, int OpNum,
61 const char* Modifier = 0);
Anton Korobeynikovd3ba2f22009-07-16 14:02:45 +000062 void printS16ImmOperand(const MachineInstr *MI, int OpNum) {
63 O << (int16_t)MI->getOperand(OpNum).getImm();
64 }
65 void printS32ImmOperand(const MachineInstr *MI, int OpNum) {
66 O << (int32_t)MI->getOperand(OpNum).getImm();
67 }
68
Chris Lattner41aefdc2009-08-08 01:32:19 +000069 void printInstruction(const MachineInstr *MI); // autogenerated.
Chris Lattnerd95148f2009-09-13 20:19:22 +000070 static const char *getRegisterName(unsigned RegNo);
Chris Lattner05af2612009-09-13 20:08:00 +000071
Anton Korobeynikov4403b932009-07-16 13:27:25 +000072 void printMachineInstruction(const MachineInstr * MI);
73
74 void emitFunctionHeader(const MachineFunction &MF);
75 bool runOnMachineFunction(MachineFunction &F);
Chris Lattner40bbebd2009-07-21 18:38:57 +000076 void PrintGlobalVariable(const GlobalVariable* GVar);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000077
78 void getAnalysisUsage(AnalysisUsage &AU) const {
79 AsmPrinter::getAnalysisUsage(AU);
80 AU.setPreservesAll();
81 }
82 };
83} // end of anonymous namespace
84
85#include "SystemZGenAsmWriter.inc"
86
Anton Korobeynikov4403b932009-07-16 13:27:25 +000087void SystemZAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Anton Korobeynikov7df84622009-07-16 14:36:52 +000088 unsigned FnAlign = MF.getAlignment();
Anton Korobeynikov4403b932009-07-16 13:27:25 +000089 const Function *F = MF.getFunction();
90
Chris Lattner6c2f9e12009-08-19 05:49:37 +000091 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Anton Korobeynikov4403b932009-07-16 13:27:25 +000092
Anton Korobeynikov4403b932009-07-16 13:27:25 +000093 EmitAlignment(FnAlign, F);
94
95 switch (F->getLinkage()) {
96 default: assert(0 && "Unknown linkage type!");
97 case Function::InternalLinkage: // Symbols default to internal.
98 case Function::PrivateLinkage:
Bill Wendling3d10a5a2009-07-20 01:03:30 +000099 case Function::LinkerPrivateLinkage:
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000100 break;
101 case Function::ExternalLinkage:
102 O << "\t.globl\t" << CurrentFnName << '\n';
103 break;
104 case Function::LinkOnceAnyLinkage:
105 case Function::LinkOnceODRLinkage:
106 case Function::WeakAnyLinkage:
107 case Function::WeakODRLinkage:
108 O << "\t.weak\t" << CurrentFnName << '\n';
109 break;
110 }
111
112 printVisibility(CurrentFnName, F->getVisibility());
113
114 O << "\t.type\t" << CurrentFnName << ",@function\n"
115 << CurrentFnName << ":\n";
116}
117
118bool SystemZAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
119 SetupMachineFunction(MF);
120 O << "\n\n";
121
Anton Korobeynikovae535672009-07-16 14:19:35 +0000122 // Print out constants referenced by the function
123 EmitConstantPool(MF.getConstantPool());
124
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000125 // Print the 'header' of function
126 emitFunctionHeader(MF);
127
128 // Print out code for the function.
129 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
130 I != E; ++I) {
131 // Print a label for the basic block.
132 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
133 // This is an entry block or a block that's only reachable via a
134 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
135 } else {
Chris Lattner70a54c02009-09-13 18:25:37 +0000136 EmitBasicBlockStart(I);
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000137 O << '\n';
138 }
139
140 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
141 II != E; ++II)
142 // Print the assembly for the instruction.
143 printMachineInstruction(II);
144 }
145
Chris Lattner33adcfb2009-08-22 21:43:10 +0000146 if (MAI->hasDotTypeDotSizeDirective())
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000147 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
148
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +0000149 // Print out jump tables referenced by the function.
150 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
151
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000152 // We didn't modify anything
153 return false;
154}
155
156void SystemZAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
157 ++EmittedInsts;
158
Chris Lattnerc5ea2632009-09-09 23:14:36 +0000159 processDebugLoc(MI->getDebugLoc());
160
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000161 // Call the autogenerated instruction printer routines.
Chris Lattner2698cb62009-08-08 00:05:42 +0000162 printInstruction(MI);
Chris Lattnerc5ea2632009-09-09 23:14:36 +0000163
164 if (VerboseAsm && !MI->getDebugLoc().isUnknown())
165 EmitComments(*MI);
166 O << '\n';
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000167}
168
Chris Lattner325d3dc2009-09-13 17:14:04 +0000169void SystemZAsmPrinter::printPCRelImmOperand(const MachineInstr *MI, int OpNum){
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000170 const MachineOperand &MO = MI->getOperand(OpNum);
171 switch (MO.getType()) {
Anton Korobeynikovcd3dfaf2009-07-16 14:17:07 +0000172 case MachineOperand::MO_Immediate:
173 O << MO.getImm();
174 return;
175 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner325d3dc2009-09-13 17:14:04 +0000176 GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
Anton Korobeynikovcd3dfaf2009-07-16 14:17:07 +0000177 return;
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000178 case MachineOperand::MO_GlobalAddress: {
179 const GlobalValue *GV = MO.getGlobal();
Anton Korobeynikov7df84622009-07-16 14:36:52 +0000180 std::string Name = Mang->getMangledName(GV);
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000181
182 O << Name;
183
184 // Assemble calls via PLT for externally visible symbols if PIC.
185 if (TM.getRelocationModel() == Reloc::PIC_ &&
186 !GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
187 !GV->hasLocalLinkage())
188 O << "@PLT";
189
190 printOffset(MO.getOffset());
191 return;
192 }
193 case MachineOperand::MO_ExternalSymbol: {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000194 std::string Name(MAI->getGlobalPrefix());
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000195 Name += MO.getSymbolName();
196 O << Name;
197
198 if (TM.getRelocationModel() == Reloc::PIC_)
199 O << "@PLT";
200
201 return;
202 }
203 default:
204 assert(0 && "Not implemented yet!");
205 }
206}
207
208
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000209void SystemZAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000210 const char* Modifier) {
Anton Korobeynikov1cc9dc72009-07-16 13:29:38 +0000211 const MachineOperand &MO = MI->getOperand(OpNum);
212 switch (MO.getType()) {
Anton Korobeynikoved002122009-07-16 14:04:01 +0000213 case MachineOperand::MO_Register: {
Anton Korobeynikov1cc9dc72009-07-16 13:29:38 +0000214 assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
215 "Virtual registers should be already mapped!");
Anton Korobeynikoved002122009-07-16 14:04:01 +0000216 unsigned Reg = MO.getReg();
217 if (Modifier && strncmp(Modifier, "subreg", 6) == 0) {
218 if (strncmp(Modifier + 7, "even", 4) == 0)
219 Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_EVEN);
220 else if (strncmp(Modifier + 7, "odd", 3) == 0)
221 Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_ODD);
222 else
223 assert(0 && "Invalid subreg modifier");
224 }
225
226 O << '%' << TRI->getAsmName(Reg);
Anton Korobeynikov1cc9dc72009-07-16 13:29:38 +0000227 return;
Anton Korobeynikoved002122009-07-16 14:04:01 +0000228 }
Anton Korobeynikov1cc9dc72009-07-16 13:29:38 +0000229 case MachineOperand::MO_Immediate:
Anton Korobeynikov4cc7ce02009-07-16 14:01:10 +0000230 O << MO.getImm();
Anton Korobeynikov1cc9dc72009-07-16 13:29:38 +0000231 return;
232 case MachineOperand::MO_MachineBasicBlock:
Chris Lattner325d3dc2009-09-13 17:14:04 +0000233 GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
Anton Korobeynikov1cc9dc72009-07-16 13:29:38 +0000234 return;
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +0000235 case MachineOperand::MO_JumpTableIndex:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000236 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +0000237 << MO.getIndex();
238
239 return;
Anton Korobeynikovae535672009-07-16 14:19:35 +0000240 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattner33adcfb2009-08-22 21:43:10 +0000241 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Anton Korobeynikovae535672009-07-16 14:19:35 +0000242 << MO.getIndex();
243
244 printOffset(MO.getOffset());
245 break;
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000246 case MachineOperand::MO_GlobalAddress: {
Anton Korobeynikov70f717f2009-07-16 14:04:22 +0000247 const GlobalValue *GV = MO.getGlobal();
Anton Korobeynikov7df84622009-07-16 14:36:52 +0000248 std::string Name = Mang->getMangledName(GV);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000249
250 O << Name;
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000251 break;
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000252 }
253 case MachineOperand::MO_ExternalSymbol: {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000254 std::string Name(MAI->getGlobalPrefix());
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000255 Name += MO.getSymbolName();
256 O << Name;
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000257 break;
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000258 }
Anton Korobeynikov1cc9dc72009-07-16 13:29:38 +0000259 default:
260 assert(0 && "Not implemented yet!");
261 }
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000262
263 switch (MO.getTargetFlags()) {
264 default:
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000265 llvm_unreachable("Unknown target flag on GV operand");
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000266 case SystemZII::MO_NO_FLAG:
267 break;
268 case SystemZII::MO_GOTENT: O << "@GOTENT"; break;
269 case SystemZII::MO_PLT: O << "@PLT"; break;
270 }
271
272 printOffset(MO.getOffset());
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000273}
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000274
275void SystemZAsmPrinter::printRIAddrOperand(const MachineInstr *MI, int OpNum,
276 const char* Modifier) {
277 const MachineOperand &Base = MI->getOperand(OpNum);
278
279 // Print displacement operand.
280 printOperand(MI, OpNum+1);
281
282 // Print base operand (if any)
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000283 if (Base.getReg()) {
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000284 O << '(';
285 printOperand(MI, OpNum);
286 O << ')';
287 }
288}
289
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000290void SystemZAsmPrinter::printRRIAddrOperand(const MachineInstr *MI, int OpNum,
291 const char* Modifier) {
292 const MachineOperand &Base = MI->getOperand(OpNum);
Anton Korobeynikovc4368a12009-07-16 13:48:42 +0000293 const MachineOperand &Index = MI->getOperand(OpNum+2);
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000294
295 // Print displacement operand.
Anton Korobeynikovc4368a12009-07-16 13:48:42 +0000296 printOperand(MI, OpNum+1);
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000297
298 // Print base operand (if any)
299 if (Base.getReg()) {
300 O << '(';
301 printOperand(MI, OpNum);
302 if (Index.getReg()) {
303 O << ',';
Anton Korobeynikovc4368a12009-07-16 13:48:42 +0000304 printOperand(MI, OpNum+2);
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000305 }
306 O << ')';
307 } else
308 assert(!Index.getReg() && "Should allocate base register first!");
309}
310
Chris Lattner40bbebd2009-07-21 18:38:57 +0000311void SystemZAsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
Anton Korobeynikov70f717f2009-07-16 14:04:22 +0000312 const TargetData *TD = TM.getTargetData();
313
314 if (!GVar->hasInitializer())
315 return; // External global require no code
316
317 // Check to see if this is a special global used by LLVM, if so, emit it.
318 if (EmitSpecialLLVMGlobal(GVar))
319 return;
320
Anton Korobeynikov7df84622009-07-16 14:36:52 +0000321 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikov70f717f2009-07-16 14:04:22 +0000322 Constant *C = GVar->getInitializer();
Anton Korobeynikov7df84622009-07-16 14:36:52 +0000323 unsigned Size = TD->getTypeAllocSize(C->getType());
Anton Korobeynikov70f717f2009-07-16 14:04:22 +0000324 unsigned Align = std::max(1U, TD->getPreferredAlignmentLog(GVar));
325
326 printVisibility(name, GVar->getVisibility());
327
328 O << "\t.type\t" << name << ",@object\n";
329
Chris Lattner6c2f9e12009-08-19 05:49:37 +0000330 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GVar, Mang,
331 TM));
Anton Korobeynikov70f717f2009-07-16 14:04:22 +0000332
333 if (C->isNullValue() && !GVar->hasSection() &&
334 !GVar->isThreadLocal() &&
335 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
336
337 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
338
339 if (GVar->hasLocalLinkage())
340 O << "\t.local\t" << name << '\n';
341
Chris Lattner33adcfb2009-08-22 21:43:10 +0000342 O << MAI->getCOMMDirective() << name << ',' << Size;
343 if (MAI->getCOMMDirectiveTakesAlignment())
344 O << ',' << (MAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikov70f717f2009-07-16 14:04:22 +0000345
346 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000347 O << "\t\t" << MAI->getCommentString() << ' ';
Dan Gohmancf20ac42009-08-13 01:36:44 +0000348 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Anton Korobeynikov70f717f2009-07-16 14:04:22 +0000349 }
350 O << '\n';
351 return;
352 }
353
354 switch (GVar->getLinkage()) {
355 case GlobalValue::CommonLinkage:
356 case GlobalValue::LinkOnceAnyLinkage:
357 case GlobalValue::LinkOnceODRLinkage:
358 case GlobalValue::WeakAnyLinkage:
359 case GlobalValue::WeakODRLinkage:
360 O << "\t.weak\t" << name << '\n';
361 break;
362 case GlobalValue::DLLExportLinkage:
363 case GlobalValue::AppendingLinkage:
364 // FIXME: appending linkage variables should go into a section of
365 // their name or something. For now, just emit them as external.
366 case GlobalValue::ExternalLinkage:
367 // If external or appending, declare as a global symbol
368 O << "\t.globl " << name << '\n';
369 // FALL THROUGH
370 case GlobalValue::PrivateLinkage:
Bill Wendling3d10a5a2009-07-20 01:03:30 +0000371 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikov70f717f2009-07-16 14:04:22 +0000372 case GlobalValue::InternalLinkage:
373 break;
374 default:
375 assert(0 && "Unknown linkage type!");
376 }
377
378 // Use 16-bit alignment by default to simplify bunch of stuff
379 EmitAlignment(Align, GVar, 1);
380 O << name << ":";
381 if (VerboseAsm) {
Chris Lattner33adcfb2009-08-22 21:43:10 +0000382 O << "\t\t\t\t" << MAI->getCommentString() << ' ';
Dan Gohmancf20ac42009-08-13 01:36:44 +0000383 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Anton Korobeynikov70f717f2009-07-16 14:04:22 +0000384 }
385 O << '\n';
Chris Lattner33adcfb2009-08-22 21:43:10 +0000386 if (MAI->hasDotTypeDotSizeDirective())
Anton Korobeynikov70f717f2009-07-16 14:04:22 +0000387 O << "\t.size\t" << name << ", " << Size << '\n';
388
389 EmitGlobalConstant(C);
390}
Anton Korobeynikov7df84622009-07-16 14:36:52 +0000391
392// Force static initialization.
393extern "C" void LLVMInitializeSystemZAsmPrinter() {
Daniel Dunbar0c795d62009-07-25 06:49:55 +0000394 RegisterAsmPrinter<SystemZAsmPrinter> X(TheSystemZTarget);
Anton Korobeynikov7df84622009-07-16 14:36:52 +0000395}