blob: 8289010254465202527e811f98f886f428539ad9 [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"
36#include "llvm/Support/Compiler.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 {
45 class VISIBILITY_HIDDEN SystemZAsmPrinter : public AsmPrinter {
46 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.
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +000070 void printMachineInstruction(const MachineInstr * MI);
71
72 void emitFunctionHeader(const MachineFunction &MF);
73 bool runOnMachineFunction(MachineFunction &F);
Chris Lattnerae982212009-07-21 18:38:57 +000074 void PrintGlobalVariable(const GlobalVariable* GVar);
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +000075
76 void getAnalysisUsage(AnalysisUsage &AU) const {
77 AsmPrinter::getAnalysisUsage(AU);
78 AU.setPreservesAll();
79 }
80 };
81} // end of anonymous namespace
82
83#include "SystemZGenAsmWriter.inc"
84
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +000085void SystemZAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Anton Korobeynikov147a9a72009-07-16 14:36:52 +000086 unsigned FnAlign = MF.getAlignment();
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +000087 const Function *F = MF.getFunction();
88
Chris Lattner73266f92009-08-19 05:49:37 +000089 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +000090
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +000091 EmitAlignment(FnAlign, F);
92
93 switch (F->getLinkage()) {
94 default: assert(0 && "Unknown linkage type!");
95 case Function::InternalLinkage: // Symbols default to internal.
96 case Function::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +000097 case Function::LinkerPrivateLinkage:
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +000098 break;
99 case Function::ExternalLinkage:
100 O << "\t.globl\t" << CurrentFnName << '\n';
101 break;
102 case Function::LinkOnceAnyLinkage:
103 case Function::LinkOnceODRLinkage:
104 case Function::WeakAnyLinkage:
105 case Function::WeakODRLinkage:
106 O << "\t.weak\t" << CurrentFnName << '\n';
107 break;
108 }
109
110 printVisibility(CurrentFnName, F->getVisibility());
111
112 O << "\t.type\t" << CurrentFnName << ",@function\n"
113 << CurrentFnName << ":\n";
114}
115
116bool SystemZAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
117 SetupMachineFunction(MF);
118 O << "\n\n";
119
Anton Korobeynikov67565d62009-07-16 14:19:35 +0000120 // Print out constants referenced by the function
121 EmitConstantPool(MF.getConstantPool());
122
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000123 // Print the 'header' of function
124 emitFunctionHeader(MF);
125
126 // Print out code for the function.
127 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
128 I != E; ++I) {
129 // Print a label for the basic block.
130 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
131 // This is an entry block or a block that's only reachable via a
132 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
133 } else {
Chris Lattner2faa4ef2009-09-13 18:25:37 +0000134 EmitBasicBlockStart(I);
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000135 O << '\n';
136 }
137
138 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
139 II != E; ++II)
140 // Print the assembly for the instruction.
141 printMachineInstruction(II);
142 }
143
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000144 if (MAI->hasDotTypeDotSizeDirective())
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000145 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
146
Anton Korobeynikov23bf4412009-07-16 14:07:50 +0000147 // Print out jump tables referenced by the function.
148 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
149
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000150 // We didn't modify anything
151 return false;
152}
153
154void SystemZAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
155 ++EmittedInsts;
156
Chris Lattner32d4cc72009-09-09 23:14:36 +0000157 processDebugLoc(MI->getDebugLoc());
158
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000159 // Call the autogenerated instruction printer routines.
Chris Lattner7d337492009-08-08 00:05:42 +0000160 printInstruction(MI);
Chris Lattner32d4cc72009-09-09 23:14:36 +0000161
162 if (VerboseAsm && !MI->getDebugLoc().isUnknown())
163 EmitComments(*MI);
164 O << '\n';
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000165}
166
Chris Lattnerc6f802d2009-09-13 17:14:04 +0000167void SystemZAsmPrinter::printPCRelImmOperand(const MachineInstr *MI, int OpNum){
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +0000168 const MachineOperand &MO = MI->getOperand(OpNum);
169 switch (MO.getType()) {
Anton Korobeynikovb4de0b82009-07-16 14:17:07 +0000170 case MachineOperand::MO_Immediate:
171 O << MO.getImm();
172 return;
173 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerc6f802d2009-09-13 17:14:04 +0000174 GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
Anton Korobeynikovb4de0b82009-07-16 14:17:07 +0000175 return;
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +0000176 case MachineOperand::MO_GlobalAddress: {
177 const GlobalValue *GV = MO.getGlobal();
Anton Korobeynikov147a9a72009-07-16 14:36:52 +0000178 std::string Name = Mang->getMangledName(GV);
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +0000179
180 O << Name;
181
182 // Assemble calls via PLT for externally visible symbols if PIC.
183 if (TM.getRelocationModel() == Reloc::PIC_ &&
184 !GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
185 !GV->hasLocalLinkage())
186 O << "@PLT";
187
188 printOffset(MO.getOffset());
189 return;
190 }
191 case MachineOperand::MO_ExternalSymbol: {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000192 std::string Name(MAI->getGlobalPrefix());
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +0000193 Name += MO.getSymbolName();
194 O << Name;
195
196 if (TM.getRelocationModel() == Reloc::PIC_)
197 O << "@PLT";
198
199 return;
200 }
201 default:
202 assert(0 && "Not implemented yet!");
203 }
204}
205
206
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000207void SystemZAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
Anton Korobeynikova58fac92009-07-16 13:43:18 +0000208 const char* Modifier) {
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000209 const MachineOperand &MO = MI->getOperand(OpNum);
210 switch (MO.getType()) {
Anton Korobeynikov8a563652009-07-16 14:04:01 +0000211 case MachineOperand::MO_Register: {
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000212 assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
213 "Virtual registers should be already mapped!");
Anton Korobeynikov8a563652009-07-16 14:04:01 +0000214 unsigned Reg = MO.getReg();
215 if (Modifier && strncmp(Modifier, "subreg", 6) == 0) {
216 if (strncmp(Modifier + 7, "even", 4) == 0)
217 Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_EVEN);
218 else if (strncmp(Modifier + 7, "odd", 3) == 0)
219 Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_ODD);
220 else
221 assert(0 && "Invalid subreg modifier");
222 }
223
224 O << '%' << TRI->getAsmName(Reg);
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000225 return;
Anton Korobeynikov8a563652009-07-16 14:04:01 +0000226 }
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000227 case MachineOperand::MO_Immediate:
Anton Korobeynikov8eef4042009-07-16 14:01:10 +0000228 O << MO.getImm();
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000229 return;
230 case MachineOperand::MO_MachineBasicBlock:
Chris Lattnerc6f802d2009-09-13 17:14:04 +0000231 GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000232 return;
Anton Korobeynikov23bf4412009-07-16 14:07:50 +0000233 case MachineOperand::MO_JumpTableIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000234 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
Anton Korobeynikov23bf4412009-07-16 14:07:50 +0000235 << MO.getIndex();
236
237 return;
Anton Korobeynikov67565d62009-07-16 14:19:35 +0000238 case MachineOperand::MO_ConstantPoolIndex:
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000239 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
Anton Korobeynikov67565d62009-07-16 14:19:35 +0000240 << MO.getIndex();
241
242 printOffset(MO.getOffset());
243 break;
Anton Korobeynikov961cd4a2009-07-16 13:50:21 +0000244 case MachineOperand::MO_GlobalAddress: {
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000245 const GlobalValue *GV = MO.getGlobal();
Anton Korobeynikov147a9a72009-07-16 14:36:52 +0000246 std::string Name = Mang->getMangledName(GV);
Anton Korobeynikov961cd4a2009-07-16 13:50:21 +0000247
248 O << Name;
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +0000249 break;
Anton Korobeynikov961cd4a2009-07-16 13:50:21 +0000250 }
251 case MachineOperand::MO_ExternalSymbol: {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000252 std::string Name(MAI->getGlobalPrefix());
Anton Korobeynikov961cd4a2009-07-16 13:50:21 +0000253 Name += MO.getSymbolName();
254 O << Name;
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +0000255 break;
Anton Korobeynikov961cd4a2009-07-16 13:50:21 +0000256 }
Anton Korobeynikovf259c6c2009-07-16 13:29:38 +0000257 default:
258 assert(0 && "Not implemented yet!");
259 }
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +0000260
261 switch (MO.getTargetFlags()) {
262 default:
Anton Korobeynikov917cbe12009-07-18 13:33:17 +0000263 llvm_unreachable("Unknown target flag on GV operand");
Anton Korobeynikov1c2eedb2009-07-16 14:16:05 +0000264 case SystemZII::MO_NO_FLAG:
265 break;
266 case SystemZII::MO_GOTENT: O << "@GOTENT"; break;
267 case SystemZII::MO_PLT: O << "@PLT"; break;
268 }
269
270 printOffset(MO.getOffset());
Anton Korobeynikov32b7d5b2009-07-16 13:27:25 +0000271}
Anton Korobeynikova58fac92009-07-16 13:43:18 +0000272
273void SystemZAsmPrinter::printRIAddrOperand(const MachineInstr *MI, int OpNum,
274 const char* Modifier) {
275 const MachineOperand &Base = MI->getOperand(OpNum);
276
277 // Print displacement operand.
278 printOperand(MI, OpNum+1);
279
280 // Print base operand (if any)
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000281 if (Base.getReg()) {
Anton Korobeynikova58fac92009-07-16 13:43:18 +0000282 O << '(';
283 printOperand(MI, OpNum);
284 O << ')';
285 }
286}
287
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000288void SystemZAsmPrinter::printRRIAddrOperand(const MachineInstr *MI, int OpNum,
289 const char* Modifier) {
290 const MachineOperand &Base = MI->getOperand(OpNum);
Anton Korobeynikovf7cefd92009-07-16 13:48:42 +0000291 const MachineOperand &Index = MI->getOperand(OpNum+2);
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000292
293 // Print displacement operand.
Anton Korobeynikovf7cefd92009-07-16 13:48:42 +0000294 printOperand(MI, OpNum+1);
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000295
296 // Print base operand (if any)
297 if (Base.getReg()) {
298 O << '(';
299 printOperand(MI, OpNum);
300 if (Index.getReg()) {
301 O << ',';
Anton Korobeynikovf7cefd92009-07-16 13:48:42 +0000302 printOperand(MI, OpNum+2);
Anton Korobeynikov87b83aa2009-07-16 13:44:00 +0000303 }
304 O << ')';
305 } else
306 assert(!Index.getReg() && "Should allocate base register first!");
307}
308
Chris Lattnerae982212009-07-21 18:38:57 +0000309void SystemZAsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000310 const TargetData *TD = TM.getTargetData();
311
312 if (!GVar->hasInitializer())
313 return; // External global require no code
314
315 // Check to see if this is a special global used by LLVM, if so, emit it.
316 if (EmitSpecialLLVMGlobal(GVar))
317 return;
318
Anton Korobeynikov147a9a72009-07-16 14:36:52 +0000319 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000320 Constant *C = GVar->getInitializer();
Anton Korobeynikov147a9a72009-07-16 14:36:52 +0000321 unsigned Size = TD->getTypeAllocSize(C->getType());
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000322 unsigned Align = std::max(1U, TD->getPreferredAlignmentLog(GVar));
323
324 printVisibility(name, GVar->getVisibility());
325
326 O << "\t.type\t" << name << ",@object\n";
327
Chris Lattner73266f92009-08-19 05:49:37 +0000328 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GVar, Mang,
329 TM));
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000330
331 if (C->isNullValue() && !GVar->hasSection() &&
332 !GVar->isThreadLocal() &&
333 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
334
335 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
336
337 if (GVar->hasLocalLinkage())
338 O << "\t.local\t" << name << '\n';
339
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000340 O << MAI->getCOMMDirective() << name << ',' << Size;
341 if (MAI->getCOMMDirectiveTakesAlignment())
342 O << ',' << (MAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000343
344 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000345 O << "\t\t" << MAI->getCommentString() << ' ';
Dan Gohman2aa282f2009-08-13 01:36:44 +0000346 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000347 }
348 O << '\n';
349 return;
350 }
351
352 switch (GVar->getLinkage()) {
353 case GlobalValue::CommonLinkage:
354 case GlobalValue::LinkOnceAnyLinkage:
355 case GlobalValue::LinkOnceODRLinkage:
356 case GlobalValue::WeakAnyLinkage:
357 case GlobalValue::WeakODRLinkage:
358 O << "\t.weak\t" << name << '\n';
359 break;
360 case GlobalValue::DLLExportLinkage:
361 case GlobalValue::AppendingLinkage:
362 // FIXME: appending linkage variables should go into a section of
363 // their name or something. For now, just emit them as external.
364 case GlobalValue::ExternalLinkage:
365 // If external or appending, declare as a global symbol
366 O << "\t.globl " << name << '\n';
367 // FALL THROUGH
368 case GlobalValue::PrivateLinkage:
Bill Wendling41a07852009-07-20 01:03:30 +0000369 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000370 case GlobalValue::InternalLinkage:
371 break;
372 default:
373 assert(0 && "Unknown linkage type!");
374 }
375
376 // Use 16-bit alignment by default to simplify bunch of stuff
377 EmitAlignment(Align, GVar, 1);
378 O << name << ":";
379 if (VerboseAsm) {
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000380 O << "\t\t\t\t" << MAI->getCommentString() << ' ';
Dan Gohman2aa282f2009-08-13 01:36:44 +0000381 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000382 }
383 O << '\n';
Chris Lattnera5ef4d32009-08-22 21:43:10 +0000384 if (MAI->hasDotTypeDotSizeDirective())
Anton Korobeynikovfafa2de2009-07-16 14:04:22 +0000385 O << "\t.size\t" << name << ", " << Size << '\n';
386
387 EmitGlobalConstant(C);
388}
Anton Korobeynikov147a9a72009-07-16 14:36:52 +0000389
390// Force static initialization.
391extern "C" void LLVMInitializeSystemZAsmPrinter() {
Daniel Dunbarc680b012009-07-25 06:49:55 +0000392 RegisterAsmPrinter<SystemZAsmPrinter> X(TheSystemZTarget);
Anton Korobeynikov147a9a72009-07-16 14:36:52 +0000393}