blob: e97e7ca9632efe99a662e1c148675e542cc21780 [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 +000038#include "llvm/Support/Mangler.h"
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +000039
40using namespace llvm;
41
42STATISTIC(EmittedInsts, "Number of machine instrs printed");
43
44namespace {
Nick Lewycky492d06e2009-10-25 06:33:48 +000045 class SystemZAsmPrinter : public AsmPrinter {
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +000046 public:
Anton Korobeynikov147a9a72009-07-16 14:36:52 +000047 SystemZAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattnera5ef4d32009-08-22 21:43:10 +000048 const MCAsmInfo *MAI, bool V)
49 : AsmPrinter(O, TM, MAI, V) {}
Anton Korobeynikov32b7d5b2009-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 Korobeynikov1c2eedb2009-07-16 14:16:05 +000057 void printPCRelImmOperand(const MachineInstr *MI, int OpNum);
Anton Korobeynikova58fac92009-07-16 13:43:18 +000058 void printRIAddrOperand(const MachineInstr *MI, int OpNum,
59 const char* Modifier = 0);
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +000060 void printRRIAddrOperand(const MachineInstr *MI, int OpNum,
61 const char* Modifier = 0);
Anton Korobeynikovbf21bac2009-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 Lattnerddb259a2009-08-08 01:32:19 +000069 void printInstruction(const MachineInstr *MI); // autogenerated.
Chris Lattner213703c2009-09-13 20:19:22 +000070 static const char *getRegisterName(unsigned RegNo);
Chris Lattner92221692009-09-13 20:08:00 +000071
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +000072 void printMachineInstruction(const MachineInstr * MI);
73
74 void emitFunctionHeader(const MachineFunction &MF);
75 bool runOnMachineFunction(MachineFunction &F);
Chris Lattnerae982212009-07-21 18:38:57 +000076 void PrintGlobalVariable(const GlobalVariable* GVar);
Anton Korobeynikov32b7d5b2009-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 Korobeynikov32b7d5b2009-07-16 13:27:25 +000087void SystemZAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Anton Korobeynikov147a9a72009-07-16 14:36:52 +000088 unsigned FnAlign = MF.getAlignment();
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +000089 const Function *F = MF.getFunction();
90
Chris Lattner73266f92009-08-19 05:49:37 +000091 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +000092
Anton Korobeynikov32b7d5b2009-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 Wendling41a07852009-07-20 01:03:30 +000099 case Function::LinkerPrivateLinkage:
Anton Korobeynikov32b7d5b2009-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 Korobeynikov67565d62009-07-16 14:19:35 +0000122 // Print out constants referenced by the function
123 EmitConstantPool(MF.getConstantPool());
124
Anton Korobeynikov32b7d5b2009-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.
Dan Gohman5fda4342009-10-06 17:38:38 +0000132 EmitBasicBlockStart(I);
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000133
134 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
135 II != E; ++II)
136 // Print the assembly for the instruction.
137 printMachineInstruction(II);
138 }
139
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000140 if (MAI->hasDotTypeDotSizeDirective())
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000141 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
142
Anton Korobeynikov23bf4412009-07-16 14:07:50 +0000143 // Print out jump tables referenced by the function.
144 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
145
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000146 // We didn't modify anything
147 return false;
148}
149
150void SystemZAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
151 ++EmittedInsts;
152
Devang Patel5450fc12009-10-06 02:19:11 +0000153 processDebugLoc(MI, true);
Chris Lattner32d4cc72009-09-09 23:14:36 +0000154
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000155 // Call the autogenerated instruction printer routines.
Chris Lattner7d337492009-08-08 00:05:42 +0000156 printInstruction(MI);
Chris Lattner32d4cc72009-09-09 23:14:36 +0000157
David Greeneca9b04b2009-11-13 21:34:57 +0000158 if (VerboseAsm)
Chris Lattner32d4cc72009-09-09 23:14:36 +0000159 EmitComments(*MI);
160 O << '\n';
Devang Patel5450fc12009-10-06 02:19:11 +0000161
162 processDebugLoc(MI, false);
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000163}
164
Chris Lattnerc6f802d2009-09-13 17:14:04 +0000165void SystemZAsmPrinter::printPCRelImmOperand(const MachineInstr *MI, int OpNum){
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +0000166 const MachineOperand &MO = MI->getOperand(OpNum);
167 switch (MO.getType()) {
Anton Korobeynikovb4de0b82009-07-16 14:17:07 +0000168 case MachineOperand::MO_Immediate:
169 O << MO.getImm();
170 return;
171 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerc6f802d2009-09-13 17:14:04 +0000172 GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
Anton Korobeynikovb4de0b82009-07-16 14:17:07 +0000173 return;
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +0000174 case MachineOperand::MO_GlobalAddress: {
175 const GlobalValue *GV = MO.getGlobal();
Anton Korobeynikov147a9a72009-07-16 14:36:52 +0000176 std::string Name = Mang->getMangledName(GV);
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +0000177
178 O << Name;
179
180 // Assemble calls via PLT for externally visible symbols if PIC.
181 if (TM.getRelocationModel() == Reloc::PIC_ &&
182 !GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
183 !GV->hasLocalLinkage())
184 O << "@PLT";
185
186 printOffset(MO.getOffset());
187 return;
188 }
189 case MachineOperand::MO_ExternalSymbol: {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000190 std::string Name(MAI->getGlobalPrefix());
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +0000191 Name += MO.getSymbolName();
192 O << Name;
193
194 if (TM.getRelocationModel() == Reloc::PIC_)
195 O << "@PLT";
196
197 return;
198 }
199 default:
200 assert(0 && "Not implemented yet!");
201 }
202}
203
204
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000205void SystemZAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
Anton Korobeynikova58fac92009-07-16 13:43:18 +0000206 const char* Modifier) {
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000207 const MachineOperand &MO = MI->getOperand(OpNum);
208 switch (MO.getType()) {
Anton Korobeynikov8a563652009-07-16 14:04:01 +0000209 case MachineOperand::MO_Register: {
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000210 assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
211 "Virtual registers should be already mapped!");
Anton Korobeynikov8a563652009-07-16 14:04:01 +0000212 unsigned Reg = MO.getReg();
213 if (Modifier && strncmp(Modifier, "subreg", 6) == 0) {
214 if (strncmp(Modifier + 7, "even", 4) == 0)
215 Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_EVEN);
216 else if (strncmp(Modifier + 7, "odd", 3) == 0)
217 Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_ODD);
218 else
219 assert(0 && "Invalid subreg modifier");
220 }
221
Chris Lattnerf0a25de2009-09-13 20:31:40 +0000222 O << '%' << getRegisterName(Reg);
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000223 return;
Anton Korobeynikov8a563652009-07-16 14:04:01 +0000224 }
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000225 case MachineOperand::MO_Immediate:
Anton Korobeynikov8eef4042009-07-16 14:01:10 +0000226 O << MO.getImm();
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000227 return;
228 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerc6f802d2009-09-13 17:14:04 +0000229 GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000230 return;
Anton Korobeynikov23bf4412009-07-16 14:07:50 +0000231 case MachineOperand::MO_JumpTableIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000232 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Anton Korobeynikov23bf4412009-07-16 14:07:50 +0000233 << MO.getIndex();
234
235 return;
Anton Korobeynikov67565d62009-07-16 14:19:35 +0000236 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000237 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Anton Korobeynikov67565d62009-07-16 14:19:35 +0000238 << MO.getIndex();
239
240 printOffset(MO.getOffset());
241 break;
Anton Korobeynikov961cd4a2009-07-16 13:50:21 +0000242 case MachineOperand::MO_GlobalAddress: {
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000243 const GlobalValue *GV = MO.getGlobal();
Anton Korobeynikov147a9a72009-07-16 14:36:52 +0000244 std::string Name = Mang->getMangledName(GV);
Anton Korobeynikov961cd4a2009-07-16 13:50:21 +0000245
246 O << Name;
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +0000247 break;
Anton Korobeynikov961cd4a2009-07-16 13:50:21 +0000248 }
249 case MachineOperand::MO_ExternalSymbol: {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000250 std::string Name(MAI->getGlobalPrefix());
Anton Korobeynikov961cd4a2009-07-16 13:50:21 +0000251 Name += MO.getSymbolName();
252 O << Name;
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +0000253 break;
Anton Korobeynikov961cd4a2009-07-16 13:50:21 +0000254 }
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000255 default:
256 assert(0 && "Not implemented yet!");
257 }
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +0000258
259 switch (MO.getTargetFlags()) {
260 default:
Anton Korobeynikov917cbe12009-07-18 13:33:17 +0000261 llvm_unreachable("Unknown target flag on GV operand");
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +0000262 case SystemZII::MO_NO_FLAG:
263 break;
264 case SystemZII::MO_GOTENT: O << "@GOTENT"; break;
265 case SystemZII::MO_PLT: O << "@PLT"; break;
266 }
267
268 printOffset(MO.getOffset());
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000269}
Anton Korobeynikova58fac92009-07-16 13:43:18 +0000270
271void SystemZAsmPrinter::printRIAddrOperand(const MachineInstr *MI, int OpNum,
272 const char* Modifier) {
273 const MachineOperand &Base = MI->getOperand(OpNum);
274
275 // Print displacement operand.
276 printOperand(MI, OpNum+1);
277
278 // Print base operand (if any)
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000279 if (Base.getReg()) {
Anton Korobeynikova58fac92009-07-16 13:43:18 +0000280 O << '(';
281 printOperand(MI, OpNum);
282 O << ')';
283 }
284}
285
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000286void SystemZAsmPrinter::printRRIAddrOperand(const MachineInstr *MI, int OpNum,
287 const char* Modifier) {
288 const MachineOperand &Base = MI->getOperand(OpNum);
Anton Korobeynikovf7cefd92009-07-16 13:48:42 +0000289 const MachineOperand &Index = MI->getOperand(OpNum+2);
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000290
291 // Print displacement operand.
Anton Korobeynikovf7cefd92009-07-16 13:48:42 +0000292 printOperand(MI, OpNum+1);
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000293
294 // Print base operand (if any)
295 if (Base.getReg()) {
296 O << '(';
297 printOperand(MI, OpNum);
298 if (Index.getReg()) {
299 O << ',';
Anton Korobeynikovf7cefd92009-07-16 13:48:42 +0000300 printOperand(MI, OpNum+2);
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000301 }
302 O << ')';
303 } else
304 assert(!Index.getReg() && "Should allocate base register first!");
305}
306
Chris Lattnerae982212009-07-21 18:38:57 +0000307void SystemZAsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000308 const TargetData *TD = TM.getTargetData();
309
310 if (!GVar->hasInitializer())
311 return; // External global require no code
312
313 // Check to see if this is a special global used by LLVM, if so, emit it.
314 if (EmitSpecialLLVMGlobal(GVar))
315 return;
316
Anton Korobeynikov147a9a72009-07-16 14:36:52 +0000317 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000318 Constant *C = GVar->getInitializer();
Anton Korobeynikov147a9a72009-07-16 14:36:52 +0000319 unsigned Size = TD->getTypeAllocSize(C->getType());
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000320 unsigned Align = std::max(1U, TD->getPreferredAlignmentLog(GVar));
321
322 printVisibility(name, GVar->getVisibility());
323
324 O << "\t.type\t" << name << ",@object\n";
325
Chris Lattner73266f92009-08-19 05:49:37 +0000326 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GVar, Mang,
327 TM));
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000328
329 if (C->isNullValue() && !GVar->hasSection() &&
330 !GVar->isThreadLocal() &&
331 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
332
333 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
334
335 if (GVar->hasLocalLinkage())
336 O << "\t.local\t" << name << '\n';
337
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000338 O << MAI->getCOMMDirective() << name << ',' << Size;
339 if (MAI->getCOMMDirectiveTakesAlignment())
340 O << ',' << (MAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000341
342 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000343 O << "\t\t" << MAI->getCommentString() << ' ';
Dan Gohman2aa282f2009-08-13 01:36:44 +0000344 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000345 }
346 O << '\n';
347 return;
348 }
349
350 switch (GVar->getLinkage()) {
351 case GlobalValue::CommonLinkage:
352 case GlobalValue::LinkOnceAnyLinkage:
353 case GlobalValue::LinkOnceODRLinkage:
354 case GlobalValue::WeakAnyLinkage:
355 case GlobalValue::WeakODRLinkage:
356 O << "\t.weak\t" << name << '\n';
357 break;
358 case GlobalValue::DLLExportLinkage:
359 case GlobalValue::AppendingLinkage:
360 // FIXME: appending linkage variables should go into a section of
361 // their name or something. For now, just emit them as external.
362 case GlobalValue::ExternalLinkage:
363 // If external or appending, declare as a global symbol
364 O << "\t.globl " << name << '\n';
365 // FALL THROUGH
366 case GlobalValue::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000367 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000368 case GlobalValue::InternalLinkage:
369 break;
370 default:
371 assert(0 && "Unknown linkage type!");
372 }
373
374 // Use 16-bit alignment by default to simplify bunch of stuff
375 EmitAlignment(Align, GVar, 1);
376 O << name << ":";
377 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000378 O << "\t\t\t\t" << MAI->getCommentString() << ' ';
Dan Gohman2aa282f2009-08-13 01:36:44 +0000379 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000380 }
381 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000382 if (MAI->hasDotTypeDotSizeDirective())
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000383 O << "\t.size\t" << name << ", " << Size << '\n';
384
385 EmitGlobalConstant(C);
386}
Anton Korobeynikov147a9a72009-07-16 14:36:52 +0000387
388// Force static initialization.
389extern "C" void LLVMInitializeSystemZAsmPrinter() {
Daniel Dunbarc680b012009-07-25 06:49:55 +0000390 RegisterAsmPrinter<SystemZAsmPrinter> X(TheSystemZTarget);
Anton Korobeynikov147a9a72009-07-16 14:36:52 +0000391}