blob: b93bdfe2ac9ebd4087a45471d71cccc94dfb0816 [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"
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +000031#include "llvm/Target/TargetData.h"
Chris Lattnerc4c40a92009-07-28 03:13:23 +000032#include "llvm/Target/TargetLoweringObjectFile.h"
Anton Korobeynikov147a9a72009-07-16 14:36:52 +000033#include "llvm/Target/TargetRegistry.h"
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +000034#include "llvm/ADT/Statistic.h"
35#include "llvm/Support/Compiler.h"
Anton Korobeynikov147a9a72009-07-16 14:36:52 +000036#include "llvm/Support/FormattedStream.h"
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +000037#include "llvm/Support/Mangler.h"
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +000038
39using namespace llvm;
40
41STATISTIC(EmittedInsts, "Number of machine instrs printed");
42
43namespace {
44 class VISIBILITY_HIDDEN SystemZAsmPrinter : public AsmPrinter {
45 public:
Anton Korobeynikov147a9a72009-07-16 14:36:52 +000046 SystemZAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
Chris Lattnera5ef4d32009-08-22 21:43:10 +000047 const MCAsmInfo *MAI, bool V)
48 : AsmPrinter(O, TM, MAI, V) {}
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +000049
50 virtual const char *getPassName() const {
51 return "SystemZ Assembly Printer";
52 }
53
54 void printOperand(const MachineInstr *MI, int OpNum,
55 const char* Modifier = 0);
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +000056 void printPCRelImmOperand(const MachineInstr *MI, int OpNum);
Anton Korobeynikova58fac92009-07-16 13:43:18 +000057 void printRIAddrOperand(const MachineInstr *MI, int OpNum,
58 const char* Modifier = 0);
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +000059 void printRRIAddrOperand(const MachineInstr *MI, int OpNum,
60 const char* Modifier = 0);
Anton Korobeynikovbf21bac2009-07-16 14:02:45 +000061 void printS16ImmOperand(const MachineInstr *MI, int OpNum) {
62 O << (int16_t)MI->getOperand(OpNum).getImm();
63 }
64 void printS32ImmOperand(const MachineInstr *MI, int OpNum) {
65 O << (int32_t)MI->getOperand(OpNum).getImm();
66 }
67
Chris Lattnerddb259a2009-08-08 01:32:19 +000068 void printInstruction(const MachineInstr *MI); // autogenerated.
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +000069 void printMachineInstruction(const MachineInstr * MI);
70
71 void emitFunctionHeader(const MachineFunction &MF);
72 bool runOnMachineFunction(MachineFunction &F);
Chris Lattnerae982212009-07-21 18:38:57 +000073 void PrintGlobalVariable(const GlobalVariable* GVar);
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +000074
75 void getAnalysisUsage(AnalysisUsage &AU) const {
76 AsmPrinter::getAnalysisUsage(AU);
77 AU.setPreservesAll();
78 }
79 };
80} // end of anonymous namespace
81
82#include "SystemZGenAsmWriter.inc"
83
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +000084void SystemZAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Anton Korobeynikov147a9a72009-07-16 14:36:52 +000085 unsigned FnAlign = MF.getAlignment();
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +000086 const Function *F = MF.getFunction();
87
Chris Lattner73266f92009-08-19 05:49:37 +000088 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +000089
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +000090 EmitAlignment(FnAlign, F);
91
92 switch (F->getLinkage()) {
93 default: assert(0 && "Unknown linkage type!");
94 case Function::InternalLinkage: // Symbols default to internal.
95 case Function::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +000096 case Function::LinkerPrivateLinkage:
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +000097 break;
98 case Function::ExternalLinkage:
99 O << "\t.globl\t" << CurrentFnName << '\n';
100 break;
101 case Function::LinkOnceAnyLinkage:
102 case Function::LinkOnceODRLinkage:
103 case Function::WeakAnyLinkage:
104 case Function::WeakODRLinkage:
105 O << "\t.weak\t" << CurrentFnName << '\n';
106 break;
107 }
108
109 printVisibility(CurrentFnName, F->getVisibility());
110
111 O << "\t.type\t" << CurrentFnName << ",@function\n"
112 << CurrentFnName << ":\n";
113}
114
115bool SystemZAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
116 SetupMachineFunction(MF);
117 O << "\n\n";
118
Anton Korobeynikov67565d62009-07-16 14:19:35 +0000119 // Print out constants referenced by the function
120 EmitConstantPool(MF.getConstantPool());
121
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000122 // Print the 'header' of function
123 emitFunctionHeader(MF);
124
125 // Print out code for the function.
126 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
127 I != E; ++I) {
128 // Print a label for the basic block.
129 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
130 // This is an entry block or a block that's only reachable via a
131 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
132 } else {
133 printBasicBlockLabel(I, true, true, VerboseAsm);
134 O << '\n';
135 }
136
137 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
138 II != E; ++II)
139 // Print the assembly for the instruction.
140 printMachineInstruction(II);
141 }
142
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000143 if (MAI->hasDotTypeDotSizeDirective())
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000144 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
145
Anton Korobeynikov23bf4412009-07-16 14:07:50 +0000146 // Print out jump tables referenced by the function.
147 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
148
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000149 // We didn't modify anything
150 return false;
151}
152
153void SystemZAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
154 ++EmittedInsts;
155
Chris Lattner32d4cc72009-09-09 23:14:36 +0000156 processDebugLoc(MI->getDebugLoc());
157
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000158 // Call the autogenerated instruction printer routines.
Chris Lattner7d337492009-08-08 00:05:42 +0000159 printInstruction(MI);
Chris Lattner32d4cc72009-09-09 23:14:36 +0000160
161 if (VerboseAsm && !MI->getDebugLoc().isUnknown())
162 EmitComments(*MI);
163 O << '\n';
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000164}
165
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +0000166void SystemZAsmPrinter::printPCRelImmOperand(const MachineInstr *MI, int OpNum) {
167 const MachineOperand &MO = MI->getOperand(OpNum);
168 switch (MO.getType()) {
Anton Korobeynikovb4de0b82009-07-16 14:17:07 +0000169 case MachineOperand::MO_Immediate:
170 O << MO.getImm();
171 return;
172 case MachineOperand::MO_MachineBasicBlock:
173 printBasicBlockLabel(MO.getMBB(), false, false, VerboseAsm);
174 return;
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +0000175 case MachineOperand::MO_GlobalAddress: {
176 const GlobalValue *GV = MO.getGlobal();
Anton Korobeynikov147a9a72009-07-16 14:36:52 +0000177 std::string Name = Mang->getMangledName(GV);
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +0000178
179 O << Name;
180
181 // Assemble calls via PLT for externally visible symbols if PIC.
182 if (TM.getRelocationModel() == Reloc::PIC_ &&
183 !GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
184 !GV->hasLocalLinkage())
185 O << "@PLT";
186
187 printOffset(MO.getOffset());
188 return;
189 }
190 case MachineOperand::MO_ExternalSymbol: {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000191 std::string Name(MAI->getGlobalPrefix());
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +0000192 Name += MO.getSymbolName();
193 O << Name;
194
195 if (TM.getRelocationModel() == Reloc::PIC_)
196 O << "@PLT";
197
198 return;
199 }
200 default:
201 assert(0 && "Not implemented yet!");
202 }
203}
204
205
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000206void SystemZAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
Anton Korobeynikova58fac92009-07-16 13:43:18 +0000207 const char* Modifier) {
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000208 const MachineOperand &MO = MI->getOperand(OpNum);
209 switch (MO.getType()) {
Anton Korobeynikov8a563652009-07-16 14:04:01 +0000210 case MachineOperand::MO_Register: {
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000211 assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
212 "Virtual registers should be already mapped!");
Anton Korobeynikov8a563652009-07-16 14:04:01 +0000213 unsigned Reg = MO.getReg();
214 if (Modifier && strncmp(Modifier, "subreg", 6) == 0) {
215 if (strncmp(Modifier + 7, "even", 4) == 0)
216 Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_EVEN);
217 else if (strncmp(Modifier + 7, "odd", 3) == 0)
218 Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_ODD);
219 else
220 assert(0 && "Invalid subreg modifier");
221 }
222
223 O << '%' << TRI->getAsmName(Reg);
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000224 return;
Anton Korobeynikov8a563652009-07-16 14:04:01 +0000225 }
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000226 case MachineOperand::MO_Immediate:
Anton Korobeynikov8eef4042009-07-16 14:01:10 +0000227 O << MO.getImm();
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000228 return;
229 case MachineOperand::MO_MachineBasicBlock:
230 printBasicBlockLabel(MO.getMBB());
231 return;
Anton Korobeynikov23bf4412009-07-16 14:07:50 +0000232 case MachineOperand::MO_JumpTableIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000233 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Anton Korobeynikov23bf4412009-07-16 14:07:50 +0000234 << MO.getIndex();
235
236 return;
Anton Korobeynikov67565d62009-07-16 14:19:35 +0000237 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000238 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Anton Korobeynikov67565d62009-07-16 14:19:35 +0000239 << MO.getIndex();
240
241 printOffset(MO.getOffset());
242 break;
Anton Korobeynikov961cd4a2009-07-16 13:50:21 +0000243 case MachineOperand::MO_GlobalAddress: {
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000244 const GlobalValue *GV = MO.getGlobal();
Anton Korobeynikov147a9a72009-07-16 14:36:52 +0000245 std::string Name = Mang->getMangledName(GV);
Anton Korobeynikov961cd4a2009-07-16 13:50:21 +0000246
247 O << Name;
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +0000248 break;
Anton Korobeynikov961cd4a2009-07-16 13:50:21 +0000249 }
250 case MachineOperand::MO_ExternalSymbol: {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000251 std::string Name(MAI->getGlobalPrefix());
Anton Korobeynikov961cd4a2009-07-16 13:50:21 +0000252 Name += MO.getSymbolName();
253 O << Name;
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +0000254 break;
Anton Korobeynikov961cd4a2009-07-16 13:50:21 +0000255 }
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000256 default:
257 assert(0 && "Not implemented yet!");
258 }
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +0000259
260 switch (MO.getTargetFlags()) {
261 default:
Anton Korobeynikov917cbe12009-07-18 13:33:17 +0000262 llvm_unreachable("Unknown target flag on GV operand");
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +0000263 case SystemZII::MO_NO_FLAG:
264 break;
265 case SystemZII::MO_GOTENT: O << "@GOTENT"; break;
266 case SystemZII::MO_PLT: O << "@PLT"; break;
267 }
268
269 printOffset(MO.getOffset());
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000270}
Anton Korobeynikova58fac92009-07-16 13:43:18 +0000271
272void SystemZAsmPrinter::printRIAddrOperand(const MachineInstr *MI, int OpNum,
273 const char* Modifier) {
274 const MachineOperand &Base = MI->getOperand(OpNum);
275
276 // Print displacement operand.
277 printOperand(MI, OpNum+1);
278
279 // Print base operand (if any)
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000280 if (Base.getReg()) {
Anton Korobeynikova58fac92009-07-16 13:43:18 +0000281 O << '(';
282 printOperand(MI, OpNum);
283 O << ')';
284 }
285}
286
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000287void SystemZAsmPrinter::printRRIAddrOperand(const MachineInstr *MI, int OpNum,
288 const char* Modifier) {
289 const MachineOperand &Base = MI->getOperand(OpNum);
Anton Korobeynikovf7cefd92009-07-16 13:48:42 +0000290 const MachineOperand &Index = MI->getOperand(OpNum+2);
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000291
292 // Print displacement operand.
Anton Korobeynikovf7cefd92009-07-16 13:48:42 +0000293 printOperand(MI, OpNum+1);
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000294
295 // Print base operand (if any)
296 if (Base.getReg()) {
297 O << '(';
298 printOperand(MI, OpNum);
299 if (Index.getReg()) {
300 O << ',';
Anton Korobeynikovf7cefd92009-07-16 13:48:42 +0000301 printOperand(MI, OpNum+2);
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000302 }
303 O << ')';
304 } else
305 assert(!Index.getReg() && "Should allocate base register first!");
306}
307
Chris Lattnerae982212009-07-21 18:38:57 +0000308void SystemZAsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000309 const TargetData *TD = TM.getTargetData();
310
311 if (!GVar->hasInitializer())
312 return; // External global require no code
313
314 // Check to see if this is a special global used by LLVM, if so, emit it.
315 if (EmitSpecialLLVMGlobal(GVar))
316 return;
317
Anton Korobeynikov147a9a72009-07-16 14:36:52 +0000318 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000319 Constant *C = GVar->getInitializer();
Anton Korobeynikov147a9a72009-07-16 14:36:52 +0000320 unsigned Size = TD->getTypeAllocSize(C->getType());
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000321 unsigned Align = std::max(1U, TD->getPreferredAlignmentLog(GVar));
322
323 printVisibility(name, GVar->getVisibility());
324
325 O << "\t.type\t" << name << ",@object\n";
326
Chris Lattner73266f92009-08-19 05:49:37 +0000327 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GVar, Mang,
328 TM));
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000329
330 if (C->isNullValue() && !GVar->hasSection() &&
331 !GVar->isThreadLocal() &&
332 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
333
334 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
335
336 if (GVar->hasLocalLinkage())
337 O << "\t.local\t" << name << '\n';
338
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000339 O << MAI->getCOMMDirective() << name << ',' << Size;
340 if (MAI->getCOMMDirectiveTakesAlignment())
341 O << ',' << (MAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000342
343 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000344 O << "\t\t" << MAI->getCommentString() << ' ';
Dan Gohman2aa282f2009-08-13 01:36:44 +0000345 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000346 }
347 O << '\n';
348 return;
349 }
350
351 switch (GVar->getLinkage()) {
352 case GlobalValue::CommonLinkage:
353 case GlobalValue::LinkOnceAnyLinkage:
354 case GlobalValue::LinkOnceODRLinkage:
355 case GlobalValue::WeakAnyLinkage:
356 case GlobalValue::WeakODRLinkage:
357 O << "\t.weak\t" << name << '\n';
358 break;
359 case GlobalValue::DLLExportLinkage:
360 case GlobalValue::AppendingLinkage:
361 // FIXME: appending linkage variables should go into a section of
362 // their name or something. For now, just emit them as external.
363 case GlobalValue::ExternalLinkage:
364 // If external or appending, declare as a global symbol
365 O << "\t.globl " << name << '\n';
366 // FALL THROUGH
367 case GlobalValue::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000368 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000369 case GlobalValue::InternalLinkage:
370 break;
371 default:
372 assert(0 && "Unknown linkage type!");
373 }
374
375 // Use 16-bit alignment by default to simplify bunch of stuff
376 EmitAlignment(Align, GVar, 1);
377 O << name << ":";
378 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000379 O << "\t\t\t\t" << MAI->getCommentString() << ' ';
Dan Gohman2aa282f2009-08-13 01:36:44 +0000380 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000381 }
382 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000383 if (MAI->hasDotTypeDotSizeDirective())
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000384 O << "\t.size\t" << name << ", " << Size << '\n';
385
386 EmitGlobalConstant(C);
387}
Anton Korobeynikov147a9a72009-07-16 14:36:52 +0000388
389// Force static initialization.
390extern "C" void LLVMInitializeSystemZAsmPrinter() {
Daniel Dunbarc680b012009-07-25 06:49:55 +0000391 RegisterAsmPrinter<SystemZAsmPrinter> X(TheSystemZTarget);
Anton Korobeynikov147a9a72009-07-16 14:36:52 +0000392}