blob: 07c8ed0f82fac72b2530564cd7055d3c1f7ca623 [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:
121 break;
122 case Function::ExternalLinkage:
123 O << "\t.globl\t" << CurrentFnName << '\n';
124 break;
125 case Function::LinkOnceAnyLinkage:
126 case Function::LinkOnceODRLinkage:
127 case Function::WeakAnyLinkage:
128 case Function::WeakODRLinkage:
129 O << "\t.weak\t" << CurrentFnName << '\n';
130 break;
131 }
132
133 printVisibility(CurrentFnName, F->getVisibility());
134
135 O << "\t.type\t" << CurrentFnName << ",@function\n"
136 << CurrentFnName << ":\n";
137}
138
139bool SystemZAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
140 SetupMachineFunction(MF);
141 O << "\n\n";
142
Anton Korobeynikov67565d62009-07-16 14:19:35 +0000143 // Print out constants referenced by the function
144 EmitConstantPool(MF.getConstantPool());
145
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000146 // Print the 'header' of function
147 emitFunctionHeader(MF);
148
149 // Print out code for the function.
150 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
151 I != E; ++I) {
152 // Print a label for the basic block.
153 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
154 // This is an entry block or a block that's only reachable via a
155 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
156 } else {
157 printBasicBlockLabel(I, true, true, VerboseAsm);
158 O << '\n';
159 }
160
161 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
162 II != E; ++II)
163 // Print the assembly for the instruction.
164 printMachineInstruction(II);
165 }
166
167 if (TAI->hasDotTypeDotSizeDirective())
168 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
169
Anton Korobeynikov23bf4412009-07-16 14:07:50 +0000170 // Print out jump tables referenced by the function.
171 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
172
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000173 O.flush();
174
175 // We didn't modify anything
176 return false;
177}
178
179void SystemZAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
180 ++EmittedInsts;
181
182 // Call the autogenerated instruction printer routines.
183 if (printInstruction(MI))
184 return;
185
Anton Korobeynikov917cbe12009-07-18 13:33:17 +0000186 llvm_unreachable("Unreachable!");
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000187}
188
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +0000189void SystemZAsmPrinter::printPCRelImmOperand(const MachineInstr *MI, int OpNum) {
190 const MachineOperand &MO = MI->getOperand(OpNum);
191 switch (MO.getType()) {
Anton Korobeynikovb4de0b82009-07-16 14:17:07 +0000192 case MachineOperand::MO_Immediate:
193 O << MO.getImm();
194 return;
195 case MachineOperand::MO_MachineBasicBlock:
196 printBasicBlockLabel(MO.getMBB(), false, false, VerboseAsm);
197 return;
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +0000198 case MachineOperand::MO_GlobalAddress: {
199 const GlobalValue *GV = MO.getGlobal();
Anton Korobeynikov147a9a72009-07-16 14:36:52 +0000200 std::string Name = Mang->getMangledName(GV);
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +0000201
202 O << Name;
203
204 // Assemble calls via PLT for externally visible symbols if PIC.
205 if (TM.getRelocationModel() == Reloc::PIC_ &&
206 !GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
207 !GV->hasLocalLinkage())
208 O << "@PLT";
209
210 printOffset(MO.getOffset());
211 return;
212 }
213 case MachineOperand::MO_ExternalSymbol: {
214 std::string Name(TAI->getGlobalPrefix());
215 Name += MO.getSymbolName();
216 O << Name;
217
218 if (TM.getRelocationModel() == Reloc::PIC_)
219 O << "@PLT";
220
221 return;
222 }
223 default:
224 assert(0 && "Not implemented yet!");
225 }
226}
227
228
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000229void SystemZAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
Anton Korobeynikova58fac92009-07-16 13:43:18 +0000230 const char* Modifier) {
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000231 const MachineOperand &MO = MI->getOperand(OpNum);
232 switch (MO.getType()) {
Anton Korobeynikov8a563652009-07-16 14:04:01 +0000233 case MachineOperand::MO_Register: {
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000234 assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
235 "Virtual registers should be already mapped!");
Anton Korobeynikov8a563652009-07-16 14:04:01 +0000236 unsigned Reg = MO.getReg();
237 if (Modifier && strncmp(Modifier, "subreg", 6) == 0) {
238 if (strncmp(Modifier + 7, "even", 4) == 0)
239 Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_EVEN);
240 else if (strncmp(Modifier + 7, "odd", 3) == 0)
241 Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_ODD);
242 else
243 assert(0 && "Invalid subreg modifier");
244 }
245
246 O << '%' << TRI->getAsmName(Reg);
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000247 return;
Anton Korobeynikov8a563652009-07-16 14:04:01 +0000248 }
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000249 case MachineOperand::MO_Immediate:
Anton Korobeynikov8eef4042009-07-16 14:01:10 +0000250 O << MO.getImm();
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000251 return;
252 case MachineOperand::MO_MachineBasicBlock:
253 printBasicBlockLabel(MO.getMBB());
254 return;
Anton Korobeynikov23bf4412009-07-16 14:07:50 +0000255 case MachineOperand::MO_JumpTableIndex:
256 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
257 << MO.getIndex();
258
259 return;
Anton Korobeynikov67565d62009-07-16 14:19:35 +0000260 case MachineOperand::MO_ConstantPoolIndex:
261 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
262 << MO.getIndex();
263
264 printOffset(MO.getOffset());
265 break;
Anton Korobeynikov961cd4a2009-07-16 13:50:21 +0000266 case MachineOperand::MO_GlobalAddress: {
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000267 const GlobalValue *GV = MO.getGlobal();
Anton Korobeynikov147a9a72009-07-16 14:36:52 +0000268 std::string Name = Mang->getMangledName(GV);
Anton Korobeynikov961cd4a2009-07-16 13:50:21 +0000269
270 O << Name;
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +0000271 break;
Anton Korobeynikov961cd4a2009-07-16 13:50:21 +0000272 }
273 case MachineOperand::MO_ExternalSymbol: {
274 std::string Name(TAI->getGlobalPrefix());
275 Name += MO.getSymbolName();
276 O << Name;
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +0000277 break;
Anton Korobeynikov961cd4a2009-07-16 13:50:21 +0000278 }
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000279 default:
280 assert(0 && "Not implemented yet!");
281 }
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +0000282
283 switch (MO.getTargetFlags()) {
284 default:
Anton Korobeynikov917cbe12009-07-18 13:33:17 +0000285 llvm_unreachable("Unknown target flag on GV operand");
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +0000286 case SystemZII::MO_NO_FLAG:
287 break;
288 case SystemZII::MO_GOTENT: O << "@GOTENT"; break;
289 case SystemZII::MO_PLT: O << "@PLT"; break;
290 }
291
292 printOffset(MO.getOffset());
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000293}
Anton Korobeynikova58fac92009-07-16 13:43:18 +0000294
295void SystemZAsmPrinter::printRIAddrOperand(const MachineInstr *MI, int OpNum,
296 const char* Modifier) {
297 const MachineOperand &Base = MI->getOperand(OpNum);
298
299 // Print displacement operand.
300 printOperand(MI, OpNum+1);
301
302 // Print base operand (if any)
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000303 if (Base.getReg()) {
Anton Korobeynikova58fac92009-07-16 13:43:18 +0000304 O << '(';
305 printOperand(MI, OpNum);
306 O << ')';
307 }
308}
309
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000310void SystemZAsmPrinter::printRRIAddrOperand(const MachineInstr *MI, int OpNum,
311 const char* Modifier) {
312 const MachineOperand &Base = MI->getOperand(OpNum);
Anton Korobeynikovf7cefd92009-07-16 13:48:42 +0000313 const MachineOperand &Index = MI->getOperand(OpNum+2);
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000314
315 // Print displacement operand.
Anton Korobeynikovf7cefd92009-07-16 13:48:42 +0000316 printOperand(MI, OpNum+1);
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000317
318 // Print base operand (if any)
319 if (Base.getReg()) {
320 O << '(';
321 printOperand(MI, OpNum);
322 if (Index.getReg()) {
323 O << ',';
Anton Korobeynikovf7cefd92009-07-16 13:48:42 +0000324 printOperand(MI, OpNum+2);
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000325 }
326 O << ')';
327 } else
328 assert(!Index.getReg() && "Should allocate base register first!");
329}
330
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000331/// PrintUnmangledNameSafely - Print out the printable characters in the name.
332/// Don't print things like \\n or \\0.
Anton Korobeynikov147a9a72009-07-16 14:36:52 +0000333static void PrintUnmangledNameSafely(const Value *V, formatted_raw_ostream &OS) {
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000334 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
335 Name != E; ++Name)
336 if (isprint(*Name))
337 OS << *Name;
338}
339
340void SystemZAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
341 const TargetData *TD = TM.getTargetData();
342
343 if (!GVar->hasInitializer())
344 return; // External global require no code
345
346 // Check to see if this is a special global used by LLVM, if so, emit it.
347 if (EmitSpecialLLVMGlobal(GVar))
348 return;
349
Anton Korobeynikov147a9a72009-07-16 14:36:52 +0000350 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000351 Constant *C = GVar->getInitializer();
Anton Korobeynikov147a9a72009-07-16 14:36:52 +0000352 unsigned Size = TD->getTypeAllocSize(C->getType());
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000353 unsigned Align = std::max(1U, TD->getPreferredAlignmentLog(GVar));
354
355 printVisibility(name, GVar->getVisibility());
356
357 O << "\t.type\t" << name << ",@object\n";
358
359 SwitchToSection(TAI->SectionForGlobal(GVar));
360
361 if (C->isNullValue() && !GVar->hasSection() &&
362 !GVar->isThreadLocal() &&
363 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
364
365 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
366
367 if (GVar->hasLocalLinkage())
368 O << "\t.local\t" << name << '\n';
369
370 O << TAI->getCOMMDirective() << name << ',' << Size;
371 if (TAI->getCOMMDirectiveTakesAlignment())
372 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
373
374 if (VerboseAsm) {
375 O << "\t\t" << TAI->getCommentString() << ' ';
376 PrintUnmangledNameSafely(GVar, O);
377 }
378 O << '\n';
379 return;
380 }
381
382 switch (GVar->getLinkage()) {
383 case GlobalValue::CommonLinkage:
384 case GlobalValue::LinkOnceAnyLinkage:
385 case GlobalValue::LinkOnceODRLinkage:
386 case GlobalValue::WeakAnyLinkage:
387 case GlobalValue::WeakODRLinkage:
388 O << "\t.weak\t" << name << '\n';
389 break;
390 case GlobalValue::DLLExportLinkage:
391 case GlobalValue::AppendingLinkage:
392 // FIXME: appending linkage variables should go into a section of
393 // their name or something. For now, just emit them as external.
394 case GlobalValue::ExternalLinkage:
395 // If external or appending, declare as a global symbol
396 O << "\t.globl " << name << '\n';
397 // FALL THROUGH
398 case GlobalValue::PrivateLinkage:
399 case GlobalValue::InternalLinkage:
400 break;
401 default:
402 assert(0 && "Unknown linkage type!");
403 }
404
405 // Use 16-bit alignment by default to simplify bunch of stuff
406 EmitAlignment(Align, GVar, 1);
407 O << name << ":";
408 if (VerboseAsm) {
409 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
410 PrintUnmangledNameSafely(GVar, O);
411 }
412 O << '\n';
413 if (TAI->hasDotTypeDotSizeDirective())
414 O << "\t.size\t" << name << ", " << Size << '\n';
415
416 EmitGlobalConstant(C);
417}
Anton Korobeynikov147a9a72009-07-16 14:36:52 +0000418
419// Force static initialization.
420extern "C" void LLVMInitializeSystemZAsmPrinter() {
421 extern Target TheSystemZTarget;
422 TargetRegistry::RegisterAsmPrinter(TheSystemZTarget,
423 createSystemZCodePrinterPass);
424}