blob: 7dd6fcc304d064fc19116d7559c6b1472a33e523 [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"
22#include "llvm/CodeGen/AsmPrinter.h"
23#include "llvm/CodeGen/DwarfWriter.h"
24#include "llvm/CodeGen/MachineModuleInfo.h"
25#include "llvm/CodeGen/MachineFunctionPass.h"
26#include "llvm/CodeGen/MachineConstantPool.h"
27#include "llvm/CodeGen/MachineInstr.h"
28#include "llvm/Target/TargetAsmInfo.h"
29#include "llvm/Target/TargetData.h"
Anton Korobeynikov147a9a72009-07-16 14:36:52 +000030#include "llvm/Target/TargetRegistry.h"
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +000031#include "llvm/ADT/Statistic.h"
32#include "llvm/Support/Compiler.h"
Anton Korobeynikov147a9a72009-07-16 14:36:52 +000033#include "llvm/Support/FormattedStream.h"
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +000034#include "llvm/Support/Mangler.h"
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +000035
36using namespace llvm;
37
38STATISTIC(EmittedInsts, "Number of machine instrs printed");
39
40namespace {
41 class VISIBILITY_HIDDEN SystemZAsmPrinter : public AsmPrinter {
42 public:
Anton Korobeynikov147a9a72009-07-16 14:36:52 +000043 SystemZAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
44 const TargetAsmInfo *TAI, bool V)
45 : AsmPrinter(O, TM, TAI, V) {}
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +000046
47 virtual const char *getPassName() const {
48 return "SystemZ Assembly Printer";
49 }
50
51 void printOperand(const MachineInstr *MI, int OpNum,
52 const char* Modifier = 0);
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +000053 void printPCRelImmOperand(const MachineInstr *MI, int OpNum);
Anton Korobeynikova58fac92009-07-16 13:43:18 +000054 void printRIAddrOperand(const MachineInstr *MI, int OpNum,
55 const char* Modifier = 0);
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +000056 void printRRIAddrOperand(const MachineInstr *MI, int OpNum,
57 const char* Modifier = 0);
Anton Korobeynikovbf21bac2009-07-16 14:02:45 +000058 void printS16ImmOperand(const MachineInstr *MI, int OpNum) {
59 O << (int16_t)MI->getOperand(OpNum).getImm();
60 }
61 void printS32ImmOperand(const MachineInstr *MI, int OpNum) {
62 O << (int32_t)MI->getOperand(OpNum).getImm();
63 }
64
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +000065 bool printInstruction(const MachineInstr *MI); // autogenerated.
66 void printMachineInstruction(const MachineInstr * MI);
67
68 void emitFunctionHeader(const MachineFunction &MF);
69 bool runOnMachineFunction(MachineFunction &F);
70 bool doInitialization(Module &M);
71 bool doFinalization(Module &M);
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +000072 void printModuleLevelGV(const GlobalVariable* GVar);
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
83/// createSystemZCodePrinterPass - Returns a pass that prints the SystemZ
84/// assembly code for a MachineFunction to the given output stream,
85/// using the given target machine description. This should work
86/// regardless of whether the function is in SSA form.
87///
Anton Korobeynikov147a9a72009-07-16 14:36:52 +000088FunctionPass *llvm::createSystemZCodePrinterPass(formatted_raw_ostream &o,
89 TargetMachine &tm,
90 bool verbose) {
91 return new SystemZAsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +000092}
93
94bool SystemZAsmPrinter::doInitialization(Module &M) {
95 Mang = new Mangler(M, "", TAI->getPrivateGlobalPrefix());
96 return false; // success
97}
98
99
100bool SystemZAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000101 // Print out module-level global variables here.
102 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikov0323dee2009-07-16 14:12:00 +0000103 I != E; ++I)
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000104 printModuleLevelGV(I);
105
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000106 return AsmPrinter::doFinalization(M);
107}
108
109void SystemZAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Anton Korobeynikov147a9a72009-07-16 14:36:52 +0000110 unsigned FnAlign = MF.getAlignment();
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000111 const Function *F = MF.getFunction();
112
113 SwitchToSection(TAI->SectionForGlobal(F));
114
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000115 EmitAlignment(FnAlign, F);
116
117 switch (F->getLinkage()) {
118 default: assert(0 && "Unknown linkage type!");
119 case Function::InternalLinkage: // Symbols default to internal.
120 case Function::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000121 case Function::LinkerPrivateLinkage:
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000122 break;
123 case Function::ExternalLinkage:
124 O << "\t.globl\t" << CurrentFnName << '\n';
125 break;
126 case Function::LinkOnceAnyLinkage:
127 case Function::LinkOnceODRLinkage:
128 case Function::WeakAnyLinkage:
129 case Function::WeakODRLinkage:
130 O << "\t.weak\t" << CurrentFnName << '\n';
131 break;
132 }
133
134 printVisibility(CurrentFnName, F->getVisibility());
135
136 O << "\t.type\t" << CurrentFnName << ",@function\n"
137 << CurrentFnName << ":\n";
138}
139
140bool SystemZAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
141 SetupMachineFunction(MF);
142 O << "\n\n";
143
Anton Korobeynikov67565d62009-07-16 14:19:35 +0000144 // Print out constants referenced by the function
145 EmitConstantPool(MF.getConstantPool());
146
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000147 // Print the 'header' of function
148 emitFunctionHeader(MF);
149
150 // Print out code for the function.
151 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
152 I != E; ++I) {
153 // Print a label for the basic block.
154 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
155 // This is an entry block or a block that's only reachable via a
156 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
157 } else {
158 printBasicBlockLabel(I, true, true, VerboseAsm);
159 O << '\n';
160 }
161
162 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
163 II != E; ++II)
164 // Print the assembly for the instruction.
165 printMachineInstruction(II);
166 }
167
168 if (TAI->hasDotTypeDotSizeDirective())
169 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
170
Anton Korobeynikov23bf4412009-07-16 14:07:50 +0000171 // Print out jump tables referenced by the function.
172 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
173
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000174 O.flush();
175
176 // We didn't modify anything
177 return false;
178}
179
180void SystemZAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
181 ++EmittedInsts;
182
183 // Call the autogenerated instruction printer routines.
184 if (printInstruction(MI))
185 return;
186
Anton Korobeynikov917cbe12009-07-18 13:33:17 +0000187 llvm_unreachable("Unreachable!");
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000188}
189
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +0000190void SystemZAsmPrinter::printPCRelImmOperand(const MachineInstr *MI, int OpNum) {
191 const MachineOperand &MO = MI->getOperand(OpNum);
192 switch (MO.getType()) {
Anton Korobeynikovb4de0b82009-07-16 14:17:07 +0000193 case MachineOperand::MO_Immediate:
194 O << MO.getImm();
195 return;
196 case MachineOperand::MO_MachineBasicBlock:
197 printBasicBlockLabel(MO.getMBB(), false, false, VerboseAsm);
198 return;
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +0000199 case MachineOperand::MO_GlobalAddress: {
200 const GlobalValue *GV = MO.getGlobal();
Anton Korobeynikov147a9a72009-07-16 14:36:52 +0000201 std::string Name = Mang->getMangledName(GV);
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +0000202
203 O << Name;
204
205 // Assemble calls via PLT for externally visible symbols if PIC.
206 if (TM.getRelocationModel() == Reloc::PIC_ &&
207 !GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
208 !GV->hasLocalLinkage())
209 O << "@PLT";
210
211 printOffset(MO.getOffset());
212 return;
213 }
214 case MachineOperand::MO_ExternalSymbol: {
215 std::string Name(TAI->getGlobalPrefix());
216 Name += MO.getSymbolName();
217 O << Name;
218
219 if (TM.getRelocationModel() == Reloc::PIC_)
220 O << "@PLT";
221
222 return;
223 }
224 default:
225 assert(0 && "Not implemented yet!");
226 }
227}
228
229
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000230void SystemZAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
Anton Korobeynikova58fac92009-07-16 13:43:18 +0000231 const char* Modifier) {
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000232 const MachineOperand &MO = MI->getOperand(OpNum);
233 switch (MO.getType()) {
Anton Korobeynikov8a563652009-07-16 14:04:01 +0000234 case MachineOperand::MO_Register: {
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000235 assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
236 "Virtual registers should be already mapped!");
Anton Korobeynikov8a563652009-07-16 14:04:01 +0000237 unsigned Reg = MO.getReg();
238 if (Modifier && strncmp(Modifier, "subreg", 6) == 0) {
239 if (strncmp(Modifier + 7, "even", 4) == 0)
240 Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_EVEN);
241 else if (strncmp(Modifier + 7, "odd", 3) == 0)
242 Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_ODD);
243 else
244 assert(0 && "Invalid subreg modifier");
245 }
246
247 O << '%' << TRI->getAsmName(Reg);
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000248 return;
Anton Korobeynikov8a563652009-07-16 14:04:01 +0000249 }
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000250 case MachineOperand::MO_Immediate:
Anton Korobeynikov8eef4042009-07-16 14:01:10 +0000251 O << MO.getImm();
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000252 return;
253 case MachineOperand::MO_MachineBasicBlock:
254 printBasicBlockLabel(MO.getMBB());
255 return;
Anton Korobeynikov23bf4412009-07-16 14:07:50 +0000256 case MachineOperand::MO_JumpTableIndex:
257 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
258 << MO.getIndex();
259
260 return;
Anton Korobeynikov67565d62009-07-16 14:19:35 +0000261 case MachineOperand::MO_ConstantPoolIndex:
262 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
263 << MO.getIndex();
264
265 printOffset(MO.getOffset());
266 break;
Anton Korobeynikov961cd4a2009-07-16 13:50:21 +0000267 case MachineOperand::MO_GlobalAddress: {
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000268 const GlobalValue *GV = MO.getGlobal();
Anton Korobeynikov147a9a72009-07-16 14:36:52 +0000269 std::string Name = Mang->getMangledName(GV);
Anton Korobeynikov961cd4a2009-07-16 13:50:21 +0000270
271 O << Name;
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +0000272 break;
Anton Korobeynikov961cd4a2009-07-16 13:50:21 +0000273 }
274 case MachineOperand::MO_ExternalSymbol: {
275 std::string Name(TAI->getGlobalPrefix());
276 Name += MO.getSymbolName();
277 O << Name;
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +0000278 break;
Anton Korobeynikov961cd4a2009-07-16 13:50:21 +0000279 }
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000280 default:
281 assert(0 && "Not implemented yet!");
282 }
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +0000283
284 switch (MO.getTargetFlags()) {
285 default:
Anton Korobeynikov917cbe12009-07-18 13:33:17 +0000286 llvm_unreachable("Unknown target flag on GV operand");
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +0000287 case SystemZII::MO_NO_FLAG:
288 break;
289 case SystemZII::MO_GOTENT: O << "@GOTENT"; break;
290 case SystemZII::MO_PLT: O << "@PLT"; break;
291 }
292
293 printOffset(MO.getOffset());
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000294}
Anton Korobeynikova58fac92009-07-16 13:43:18 +0000295
296void SystemZAsmPrinter::printRIAddrOperand(const MachineInstr *MI, int OpNum,
297 const char* Modifier) {
298 const MachineOperand &Base = MI->getOperand(OpNum);
299
300 // Print displacement operand.
301 printOperand(MI, OpNum+1);
302
303 // Print base operand (if any)
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000304 if (Base.getReg()) {
Anton Korobeynikova58fac92009-07-16 13:43:18 +0000305 O << '(';
306 printOperand(MI, OpNum);
307 O << ')';
308 }
309}
310
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000311void SystemZAsmPrinter::printRRIAddrOperand(const MachineInstr *MI, int OpNum,
312 const char* Modifier) {
313 const MachineOperand &Base = MI->getOperand(OpNum);
Anton Korobeynikovf7cefd92009-07-16 13:48:42 +0000314 const MachineOperand &Index = MI->getOperand(OpNum+2);
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000315
316 // Print displacement operand.
Anton Korobeynikovf7cefd92009-07-16 13:48:42 +0000317 printOperand(MI, OpNum+1);
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000318
319 // Print base operand (if any)
320 if (Base.getReg()) {
321 O << '(';
322 printOperand(MI, OpNum);
323 if (Index.getReg()) {
324 O << ',';
Anton Korobeynikovf7cefd92009-07-16 13:48:42 +0000325 printOperand(MI, OpNum+2);
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000326 }
327 O << ')';
328 } else
329 assert(!Index.getReg() && "Should allocate base register first!");
330}
331
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000332/// PrintUnmangledNameSafely - Print out the printable characters in the name.
333/// Don't print things like \\n or \\0.
Anton Korobeynikov147a9a72009-07-16 14:36:52 +0000334static void PrintUnmangledNameSafely(const Value *V, formatted_raw_ostream &OS) {
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000335 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
336 Name != E; ++Name)
337 if (isprint(*Name))
338 OS << *Name;
339}
340
341void SystemZAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
342 const TargetData *TD = TM.getTargetData();
343
344 if (!GVar->hasInitializer())
345 return; // External global require no code
346
347 // Check to see if this is a special global used by LLVM, if so, emit it.
348 if (EmitSpecialLLVMGlobal(GVar))
349 return;
350
Anton Korobeynikov147a9a72009-07-16 14:36:52 +0000351 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000352 Constant *C = GVar->getInitializer();
Anton Korobeynikov147a9a72009-07-16 14:36:52 +0000353 unsigned Size = TD->getTypeAllocSize(C->getType());
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000354 unsigned Align = std::max(1U, TD->getPreferredAlignmentLog(GVar));
355
356 printVisibility(name, GVar->getVisibility());
357
358 O << "\t.type\t" << name << ",@object\n";
359
360 SwitchToSection(TAI->SectionForGlobal(GVar));
361
362 if (C->isNullValue() && !GVar->hasSection() &&
363 !GVar->isThreadLocal() &&
364 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
365
366 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
367
368 if (GVar->hasLocalLinkage())
369 O << "\t.local\t" << name << '\n';
370
371 O << TAI->getCOMMDirective() << name << ',' << Size;
372 if (TAI->getCOMMDirectiveTakesAlignment())
373 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
374
375 if (VerboseAsm) {
376 O << "\t\t" << TAI->getCommentString() << ' ';
377 PrintUnmangledNameSafely(GVar, O);
378 }
379 O << '\n';
380 return;
381 }
382
383 switch (GVar->getLinkage()) {
384 case GlobalValue::CommonLinkage:
385 case GlobalValue::LinkOnceAnyLinkage:
386 case GlobalValue::LinkOnceODRLinkage:
387 case GlobalValue::WeakAnyLinkage:
388 case GlobalValue::WeakODRLinkage:
389 O << "\t.weak\t" << name << '\n';
390 break;
391 case GlobalValue::DLLExportLinkage:
392 case GlobalValue::AppendingLinkage:
393 // FIXME: appending linkage variables should go into a section of
394 // their name or something. For now, just emit them as external.
395 case GlobalValue::ExternalLinkage:
396 // If external or appending, declare as a global symbol
397 O << "\t.globl " << name << '\n';
398 // FALL THROUGH
399 case GlobalValue::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000400 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000401 case GlobalValue::InternalLinkage:
402 break;
403 default:
404 assert(0 && "Unknown linkage type!");
405 }
406
407 // Use 16-bit alignment by default to simplify bunch of stuff
408 EmitAlignment(Align, GVar, 1);
409 O << name << ":";
410 if (VerboseAsm) {
411 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
412 PrintUnmangledNameSafely(GVar, O);
413 }
414 O << '\n';
415 if (TAI->hasDotTypeDotSizeDirective())
416 O << "\t.size\t" << name << ", " << Size << '\n';
417
418 EmitGlobalConstant(C);
419}
Anton Korobeynikov147a9a72009-07-16 14:36:52 +0000420
421// Force static initialization.
422extern "C" void LLVMInitializeSystemZAsmPrinter() {
Anton Korobeynikov147a9a72009-07-16 14:36:52 +0000423 TargetRegistry::RegisterAsmPrinter(TheSystemZTarget,
424 createSystemZCodePrinterPass);
425}