blob: eed46c3218174e49969035a82d30aad65ac4a522 [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);
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +000070 bool doFinalization(Module &M);
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +000071 void printModuleLevelGV(const GlobalVariable* GVar);
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +000072
73 void getAnalysisUsage(AnalysisUsage &AU) const {
74 AsmPrinter::getAnalysisUsage(AU);
75 AU.setPreservesAll();
76 }
77 };
78} // end of anonymous namespace
79
80#include "SystemZGenAsmWriter.inc"
81
82/// createSystemZCodePrinterPass - Returns a pass that prints the SystemZ
83/// assembly code for a MachineFunction to the given output stream,
84/// using the given target machine description. This should work
85/// regardless of whether the function is in SSA form.
86///
Anton Korobeynikov147a9a72009-07-16 14:36:52 +000087FunctionPass *llvm::createSystemZCodePrinterPass(formatted_raw_ostream &o,
88 TargetMachine &tm,
89 bool verbose) {
90 return new SystemZAsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +000091}
92
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +000093bool SystemZAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +000094 // Print out module-level global variables here.
95 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikov0323dee2009-07-16 14:12:00 +000096 I != E; ++I)
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +000097 printModuleLevelGV(I);
98
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +000099 return AsmPrinter::doFinalization(M);
100}
101
102void SystemZAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Anton Korobeynikov147a9a72009-07-16 14:36:52 +0000103 unsigned FnAlign = MF.getAlignment();
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000104 const Function *F = MF.getFunction();
105
106 SwitchToSection(TAI->SectionForGlobal(F));
107
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000108 EmitAlignment(FnAlign, F);
109
110 switch (F->getLinkage()) {
111 default: assert(0 && "Unknown linkage type!");
112 case Function::InternalLinkage: // Symbols default to internal.
113 case Function::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000114 case Function::LinkerPrivateLinkage:
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000115 break;
116 case Function::ExternalLinkage:
117 O << "\t.globl\t" << CurrentFnName << '\n';
118 break;
119 case Function::LinkOnceAnyLinkage:
120 case Function::LinkOnceODRLinkage:
121 case Function::WeakAnyLinkage:
122 case Function::WeakODRLinkage:
123 O << "\t.weak\t" << CurrentFnName << '\n';
124 break;
125 }
126
127 printVisibility(CurrentFnName, F->getVisibility());
128
129 O << "\t.type\t" << CurrentFnName << ",@function\n"
130 << CurrentFnName << ":\n";
131}
132
133bool SystemZAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
134 SetupMachineFunction(MF);
135 O << "\n\n";
136
Anton Korobeynikov67565d62009-07-16 14:19:35 +0000137 // Print out constants referenced by the function
138 EmitConstantPool(MF.getConstantPool());
139
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000140 // Print the 'header' of function
141 emitFunctionHeader(MF);
142
143 // Print out code for the function.
144 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
145 I != E; ++I) {
146 // Print a label for the basic block.
147 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
148 // This is an entry block or a block that's only reachable via a
149 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
150 } else {
151 printBasicBlockLabel(I, true, true, VerboseAsm);
152 O << '\n';
153 }
154
155 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
156 II != E; ++II)
157 // Print the assembly for the instruction.
158 printMachineInstruction(II);
159 }
160
161 if (TAI->hasDotTypeDotSizeDirective())
162 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
163
Anton Korobeynikov23bf4412009-07-16 14:07:50 +0000164 // Print out jump tables referenced by the function.
165 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
166
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000167 O.flush();
168
169 // We didn't modify anything
170 return false;
171}
172
173void SystemZAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
174 ++EmittedInsts;
175
176 // Call the autogenerated instruction printer routines.
177 if (printInstruction(MI))
178 return;
179
Anton Korobeynikov917cbe12009-07-18 13:33:17 +0000180 llvm_unreachable("Unreachable!");
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000181}
182
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +0000183void SystemZAsmPrinter::printPCRelImmOperand(const MachineInstr *MI, int OpNum) {
184 const MachineOperand &MO = MI->getOperand(OpNum);
185 switch (MO.getType()) {
Anton Korobeynikovb4de0b82009-07-16 14:17:07 +0000186 case MachineOperand::MO_Immediate:
187 O << MO.getImm();
188 return;
189 case MachineOperand::MO_MachineBasicBlock:
190 printBasicBlockLabel(MO.getMBB(), false, false, VerboseAsm);
191 return;
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +0000192 case MachineOperand::MO_GlobalAddress: {
193 const GlobalValue *GV = MO.getGlobal();
Anton Korobeynikov147a9a72009-07-16 14:36:52 +0000194 std::string Name = Mang->getMangledName(GV);
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +0000195
196 O << Name;
197
198 // Assemble calls via PLT for externally visible symbols if PIC.
199 if (TM.getRelocationModel() == Reloc::PIC_ &&
200 !GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
201 !GV->hasLocalLinkage())
202 O << "@PLT";
203
204 printOffset(MO.getOffset());
205 return;
206 }
207 case MachineOperand::MO_ExternalSymbol: {
208 std::string Name(TAI->getGlobalPrefix());
209 Name += MO.getSymbolName();
210 O << Name;
211
212 if (TM.getRelocationModel() == Reloc::PIC_)
213 O << "@PLT";
214
215 return;
216 }
217 default:
218 assert(0 && "Not implemented yet!");
219 }
220}
221
222
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000223void SystemZAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
Anton Korobeynikova58fac92009-07-16 13:43:18 +0000224 const char* Modifier) {
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000225 const MachineOperand &MO = MI->getOperand(OpNum);
226 switch (MO.getType()) {
Anton Korobeynikov8a563652009-07-16 14:04:01 +0000227 case MachineOperand::MO_Register: {
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000228 assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
229 "Virtual registers should be already mapped!");
Anton Korobeynikov8a563652009-07-16 14:04:01 +0000230 unsigned Reg = MO.getReg();
231 if (Modifier && strncmp(Modifier, "subreg", 6) == 0) {
232 if (strncmp(Modifier + 7, "even", 4) == 0)
233 Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_EVEN);
234 else if (strncmp(Modifier + 7, "odd", 3) == 0)
235 Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_ODD);
236 else
237 assert(0 && "Invalid subreg modifier");
238 }
239
240 O << '%' << TRI->getAsmName(Reg);
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000241 return;
Anton Korobeynikov8a563652009-07-16 14:04:01 +0000242 }
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000243 case MachineOperand::MO_Immediate:
Anton Korobeynikov8eef4042009-07-16 14:01:10 +0000244 O << MO.getImm();
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000245 return;
246 case MachineOperand::MO_MachineBasicBlock:
247 printBasicBlockLabel(MO.getMBB());
248 return;
Anton Korobeynikov23bf4412009-07-16 14:07:50 +0000249 case MachineOperand::MO_JumpTableIndex:
250 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
251 << MO.getIndex();
252
253 return;
Anton Korobeynikov67565d62009-07-16 14:19:35 +0000254 case MachineOperand::MO_ConstantPoolIndex:
255 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
256 << MO.getIndex();
257
258 printOffset(MO.getOffset());
259 break;
Anton Korobeynikov961cd4a2009-07-16 13:50:21 +0000260 case MachineOperand::MO_GlobalAddress: {
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000261 const GlobalValue *GV = MO.getGlobal();
Anton Korobeynikov147a9a72009-07-16 14:36:52 +0000262 std::string Name = Mang->getMangledName(GV);
Anton Korobeynikov961cd4a2009-07-16 13:50:21 +0000263
264 O << Name;
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +0000265 break;
Anton Korobeynikov961cd4a2009-07-16 13:50:21 +0000266 }
267 case MachineOperand::MO_ExternalSymbol: {
268 std::string Name(TAI->getGlobalPrefix());
269 Name += MO.getSymbolName();
270 O << Name;
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +0000271 break;
Anton Korobeynikov961cd4a2009-07-16 13:50:21 +0000272 }
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000273 default:
274 assert(0 && "Not implemented yet!");
275 }
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +0000276
277 switch (MO.getTargetFlags()) {
278 default:
Anton Korobeynikov917cbe12009-07-18 13:33:17 +0000279 llvm_unreachable("Unknown target flag on GV operand");
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +0000280 case SystemZII::MO_NO_FLAG:
281 break;
282 case SystemZII::MO_GOTENT: O << "@GOTENT"; break;
283 case SystemZII::MO_PLT: O << "@PLT"; break;
284 }
285
286 printOffset(MO.getOffset());
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000287}
Anton Korobeynikova58fac92009-07-16 13:43:18 +0000288
289void SystemZAsmPrinter::printRIAddrOperand(const MachineInstr *MI, int OpNum,
290 const char* Modifier) {
291 const MachineOperand &Base = MI->getOperand(OpNum);
292
293 // Print displacement operand.
294 printOperand(MI, OpNum+1);
295
296 // Print base operand (if any)
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000297 if (Base.getReg()) {
Anton Korobeynikova58fac92009-07-16 13:43:18 +0000298 O << '(';
299 printOperand(MI, OpNum);
300 O << ')';
301 }
302}
303
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000304void SystemZAsmPrinter::printRRIAddrOperand(const MachineInstr *MI, int OpNum,
305 const char* Modifier) {
306 const MachineOperand &Base = MI->getOperand(OpNum);
Anton Korobeynikovf7cefd92009-07-16 13:48:42 +0000307 const MachineOperand &Index = MI->getOperand(OpNum+2);
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000308
309 // Print displacement operand.
Anton Korobeynikovf7cefd92009-07-16 13:48:42 +0000310 printOperand(MI, OpNum+1);
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000311
312 // Print base operand (if any)
313 if (Base.getReg()) {
314 O << '(';
315 printOperand(MI, OpNum);
316 if (Index.getReg()) {
317 O << ',';
Anton Korobeynikovf7cefd92009-07-16 13:48:42 +0000318 printOperand(MI, OpNum+2);
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000319 }
320 O << ')';
321 } else
322 assert(!Index.getReg() && "Should allocate base register first!");
323}
324
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000325/// PrintUnmangledNameSafely - Print out the printable characters in the name.
326/// Don't print things like \\n or \\0.
Anton Korobeynikov147a9a72009-07-16 14:36:52 +0000327static void PrintUnmangledNameSafely(const Value *V, formatted_raw_ostream &OS) {
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000328 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
329 Name != E; ++Name)
330 if (isprint(*Name))
331 OS << *Name;
332}
333
334void SystemZAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
335 const TargetData *TD = TM.getTargetData();
336
337 if (!GVar->hasInitializer())
338 return; // External global require no code
339
340 // Check to see if this is a special global used by LLVM, if so, emit it.
341 if (EmitSpecialLLVMGlobal(GVar))
342 return;
343
Anton Korobeynikov147a9a72009-07-16 14:36:52 +0000344 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000345 Constant *C = GVar->getInitializer();
Anton Korobeynikov147a9a72009-07-16 14:36:52 +0000346 unsigned Size = TD->getTypeAllocSize(C->getType());
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000347 unsigned Align = std::max(1U, TD->getPreferredAlignmentLog(GVar));
348
349 printVisibility(name, GVar->getVisibility());
350
351 O << "\t.type\t" << name << ",@object\n";
352
353 SwitchToSection(TAI->SectionForGlobal(GVar));
354
355 if (C->isNullValue() && !GVar->hasSection() &&
356 !GVar->isThreadLocal() &&
357 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
358
359 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
360
361 if (GVar->hasLocalLinkage())
362 O << "\t.local\t" << name << '\n';
363
364 O << TAI->getCOMMDirective() << name << ',' << Size;
365 if (TAI->getCOMMDirectiveTakesAlignment())
366 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
367
368 if (VerboseAsm) {
369 O << "\t\t" << TAI->getCommentString() << ' ';
370 PrintUnmangledNameSafely(GVar, O);
371 }
372 O << '\n';
373 return;
374 }
375
376 switch (GVar->getLinkage()) {
377 case GlobalValue::CommonLinkage:
378 case GlobalValue::LinkOnceAnyLinkage:
379 case GlobalValue::LinkOnceODRLinkage:
380 case GlobalValue::WeakAnyLinkage:
381 case GlobalValue::WeakODRLinkage:
382 O << "\t.weak\t" << name << '\n';
383 break;
384 case GlobalValue::DLLExportLinkage:
385 case GlobalValue::AppendingLinkage:
386 // FIXME: appending linkage variables should go into a section of
387 // their name or something. For now, just emit them as external.
388 case GlobalValue::ExternalLinkage:
389 // If external or appending, declare as a global symbol
390 O << "\t.globl " << name << '\n';
391 // FALL THROUGH
392 case GlobalValue::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000393 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000394 case GlobalValue::InternalLinkage:
395 break;
396 default:
397 assert(0 && "Unknown linkage type!");
398 }
399
400 // Use 16-bit alignment by default to simplify bunch of stuff
401 EmitAlignment(Align, GVar, 1);
402 O << name << ":";
403 if (VerboseAsm) {
404 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
405 PrintUnmangledNameSafely(GVar, O);
406 }
407 O << '\n';
408 if (TAI->hasDotTypeDotSizeDirective())
409 O << "\t.size\t" << name << ", " << Size << '\n';
410
411 EmitGlobalConstant(C);
412}
Anton Korobeynikov147a9a72009-07-16 14:36:52 +0000413
414// Force static initialization.
415extern "C" void LLVMInitializeSystemZAsmPrinter() {
Anton Korobeynikov147a9a72009-07-16 14:36:52 +0000416 TargetRegistry::RegisterAsmPrinter(TheSystemZTarget,
417 createSystemZCodePrinterPass);
418}