blob: 9ec58816df4982249c89e34396f40c34f8753a81 [file] [log] [blame]
Anton Korobeynikov4403b932009-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"
30#include "llvm/ADT/Statistic.h"
31#include "llvm/Support/Compiler.h"
32#include "llvm/Support/Mangler.h"
33#include "llvm/Support/raw_ostream.h"
34
35using namespace llvm;
36
37STATISTIC(EmittedInsts, "Number of machine instrs printed");
38
39namespace {
40 class VISIBILITY_HIDDEN SystemZAsmPrinter : public AsmPrinter {
41 public:
42 SystemZAsmPrinter(raw_ostream &O, SystemZTargetMachine &TM,
43 const TargetAsmInfo *TAI,
44 CodeGenOpt::Level OL, bool V)
45 : AsmPrinter(O, TM, TAI, OL, V) {}
46
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 Korobeynikov6fe326c2009-07-16 14:16:05 +000053 void printPCRelImmOperand(const MachineInstr *MI, int OpNum);
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +000054 void printRIAddrOperand(const MachineInstr *MI, int OpNum,
55 const char* Modifier = 0);
Anton Korobeynikov3360da92009-07-16 13:44:00 +000056 void printRRIAddrOperand(const MachineInstr *MI, int OpNum,
57 const char* Modifier = 0);
Anton Korobeynikovd3ba2f22009-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 Korobeynikov4403b932009-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 Korobeynikov70f717f2009-07-16 14:04:22 +000072 void printModuleLevelGV(const GlobalVariable* GVar);
Anton Korobeynikov4403b932009-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///
88FunctionPass *llvm::createSystemZCodePrinterPass(raw_ostream &o,
89 SystemZTargetMachine &tm,
90 CodeGenOpt::Level OptLevel,
91 bool verbose) {
92 return new SystemZAsmPrinter(o, tm, tm.getTargetAsmInfo(), OptLevel, verbose);
93}
94
95bool SystemZAsmPrinter::doInitialization(Module &M) {
96 Mang = new Mangler(M, "", TAI->getPrivateGlobalPrefix());
97 return false; // success
98}
99
100
101bool SystemZAsmPrinter::doFinalization(Module &M) {
Anton Korobeynikov70f717f2009-07-16 14:04:22 +0000102 // Print out module-level global variables here.
103 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
Anton Korobeynikov78085ee2009-07-16 14:12:00 +0000104 I != E; ++I)
Anton Korobeynikov70f717f2009-07-16 14:04:22 +0000105 printModuleLevelGV(I);
106
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000107 return AsmPrinter::doFinalization(M);
108}
109
110void SystemZAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
111 const Function *F = MF.getFunction();
112
113 SwitchToSection(TAI->SectionForGlobal(F));
114
115 unsigned FnAlign = 4;
116 if (F->hasFnAttr(Attribute::OptimizeForSize))
117 FnAlign = 1;
118
119 EmitAlignment(FnAlign, F);
120
121 switch (F->getLinkage()) {
122 default: assert(0 && "Unknown linkage type!");
123 case Function::InternalLinkage: // Symbols default to internal.
124 case Function::PrivateLinkage:
125 break;
126 case Function::ExternalLinkage:
127 O << "\t.globl\t" << CurrentFnName << '\n';
128 break;
129 case Function::LinkOnceAnyLinkage:
130 case Function::LinkOnceODRLinkage:
131 case Function::WeakAnyLinkage:
132 case Function::WeakODRLinkage:
133 O << "\t.weak\t" << CurrentFnName << '\n';
134 break;
135 }
136
137 printVisibility(CurrentFnName, F->getVisibility());
138
139 O << "\t.type\t" << CurrentFnName << ",@function\n"
140 << CurrentFnName << ":\n";
141}
142
143bool SystemZAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
144 SetupMachineFunction(MF);
145 O << "\n\n";
146
147 // 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 Korobeynikovc16cdc52009-07-16 14:07:50 +0000171 // Print out jump tables referenced by the function.
172 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
173
Anton Korobeynikov4403b932009-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
187 assert(0 && "Should not happen");
188}
189
Anton Korobeynikov6fe326c2009-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 Korobeynikovcd3dfaf2009-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 Korobeynikov6fe326c2009-07-16 14:16:05 +0000199 case MachineOperand::MO_GlobalAddress: {
200 const GlobalValue *GV = MO.getGlobal();
201 std::string Name = Mang->getValueName(GV);
202
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 Korobeynikov4403b932009-07-16 13:27:25 +0000230void SystemZAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000231 const char* Modifier) {
Anton Korobeynikov1cc9dc72009-07-16 13:29:38 +0000232 const MachineOperand &MO = MI->getOperand(OpNum);
233 switch (MO.getType()) {
Anton Korobeynikoved002122009-07-16 14:04:01 +0000234 case MachineOperand::MO_Register: {
Anton Korobeynikov1cc9dc72009-07-16 13:29:38 +0000235 assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
236 "Virtual registers should be already mapped!");
Anton Korobeynikoved002122009-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 Korobeynikov1cc9dc72009-07-16 13:29:38 +0000248 return;
Anton Korobeynikoved002122009-07-16 14:04:01 +0000249 }
Anton Korobeynikov1cc9dc72009-07-16 13:29:38 +0000250 case MachineOperand::MO_Immediate:
Anton Korobeynikov4cc7ce02009-07-16 14:01:10 +0000251 O << MO.getImm();
Anton Korobeynikov1cc9dc72009-07-16 13:29:38 +0000252 return;
253 case MachineOperand::MO_MachineBasicBlock:
254 printBasicBlockLabel(MO.getMBB());
255 return;
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +0000256 case MachineOperand::MO_JumpTableIndex:
257 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
258 << MO.getIndex();
259
260 return;
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000261 case MachineOperand::MO_GlobalAddress: {
Anton Korobeynikov70f717f2009-07-16 14:04:22 +0000262 const GlobalValue *GV = MO.getGlobal();
Anton Korobeynikov70f717f2009-07-16 14:04:22 +0000263 std::string Name = Mang->getValueName(GV);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000264
265 O << Name;
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000266 break;
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000267 }
268 case MachineOperand::MO_ExternalSymbol: {
269 std::string Name(TAI->getGlobalPrefix());
270 Name += MO.getSymbolName();
271 O << Name;
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000272 break;
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000273 }
Anton Korobeynikov1cc9dc72009-07-16 13:29:38 +0000274 default:
275 assert(0 && "Not implemented yet!");
276 }
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000277
278 switch (MO.getTargetFlags()) {
279 default:
280 assert(0 && "Unknown target flag on GV operand");
281 case SystemZII::MO_NO_FLAG:
282 break;
283 case SystemZII::MO_GOTENT: O << "@GOTENT"; break;
284 case SystemZII::MO_PLT: O << "@PLT"; break;
285 }
286
287 printOffset(MO.getOffset());
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000288}
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000289
290void SystemZAsmPrinter::printRIAddrOperand(const MachineInstr *MI, int OpNum,
291 const char* Modifier) {
292 const MachineOperand &Base = MI->getOperand(OpNum);
293
294 // Print displacement operand.
295 printOperand(MI, OpNum+1);
296
297 // Print base operand (if any)
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000298 if (Base.getReg()) {
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000299 O << '(';
300 printOperand(MI, OpNum);
301 O << ')';
302 }
303}
304
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000305void SystemZAsmPrinter::printRRIAddrOperand(const MachineInstr *MI, int OpNum,
306 const char* Modifier) {
307 const MachineOperand &Base = MI->getOperand(OpNum);
Anton Korobeynikovc4368a12009-07-16 13:48:42 +0000308 const MachineOperand &Index = MI->getOperand(OpNum+2);
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000309
310 // Print displacement operand.
Anton Korobeynikovc4368a12009-07-16 13:48:42 +0000311 printOperand(MI, OpNum+1);
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000312
313 // Print base operand (if any)
314 if (Base.getReg()) {
315 O << '(';
316 printOperand(MI, OpNum);
317 if (Index.getReg()) {
318 O << ',';
Anton Korobeynikovc4368a12009-07-16 13:48:42 +0000319 printOperand(MI, OpNum+2);
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000320 }
321 O << ')';
322 } else
323 assert(!Index.getReg() && "Should allocate base register first!");
324}
325
Anton Korobeynikov70f717f2009-07-16 14:04:22 +0000326/// PrintUnmangledNameSafely - Print out the printable characters in the name.
327/// Don't print things like \\n or \\0.
328static void PrintUnmangledNameSafely(const Value *V, raw_ostream &OS) {
329 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
330 Name != E; ++Name)
331 if (isprint(*Name))
332 OS << *Name;
333}
334
335void SystemZAsmPrinter::printModuleLevelGV(const GlobalVariable* GVar) {
336 const TargetData *TD = TM.getTargetData();
337
338 if (!GVar->hasInitializer())
339 return; // External global require no code
340
341 // Check to see if this is a special global used by LLVM, if so, emit it.
342 if (EmitSpecialLLVMGlobal(GVar))
343 return;
344
345 std::string name = Mang->getValueName(GVar);
346 Constant *C = GVar->getInitializer();
347 const Type *Type = C->getType();
348 unsigned Size = TD->getTypeAllocSize(Type);
349 unsigned Align = std::max(1U, TD->getPreferredAlignmentLog(GVar));
350
351 printVisibility(name, GVar->getVisibility());
352
353 O << "\t.type\t" << name << ",@object\n";
354
355 SwitchToSection(TAI->SectionForGlobal(GVar));
356
357 if (C->isNullValue() && !GVar->hasSection() &&
358 !GVar->isThreadLocal() &&
359 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
360
361 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
362
363 if (GVar->hasLocalLinkage())
364 O << "\t.local\t" << name << '\n';
365
366 O << TAI->getCOMMDirective() << name << ',' << Size;
367 if (TAI->getCOMMDirectiveTakesAlignment())
368 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
369
370 if (VerboseAsm) {
371 O << "\t\t" << TAI->getCommentString() << ' ';
372 PrintUnmangledNameSafely(GVar, O);
373 }
374 O << '\n';
375 return;
376 }
377
378 switch (GVar->getLinkage()) {
379 case GlobalValue::CommonLinkage:
380 case GlobalValue::LinkOnceAnyLinkage:
381 case GlobalValue::LinkOnceODRLinkage:
382 case GlobalValue::WeakAnyLinkage:
383 case GlobalValue::WeakODRLinkage:
384 O << "\t.weak\t" << name << '\n';
385 break;
386 case GlobalValue::DLLExportLinkage:
387 case GlobalValue::AppendingLinkage:
388 // FIXME: appending linkage variables should go into a section of
389 // their name or something. For now, just emit them as external.
390 case GlobalValue::ExternalLinkage:
391 // If external or appending, declare as a global symbol
392 O << "\t.globl " << name << '\n';
393 // FALL THROUGH
394 case GlobalValue::PrivateLinkage:
395 case GlobalValue::InternalLinkage:
396 break;
397 default:
398 assert(0 && "Unknown linkage type!");
399 }
400
401 // Use 16-bit alignment by default to simplify bunch of stuff
402 EmitAlignment(Align, GVar, 1);
403 O << name << ":";
404 if (VerboseAsm) {
405 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
406 PrintUnmangledNameSafely(GVar, O);
407 }
408 O << '\n';
409 if (TAI->hasDotTypeDotSizeDirective())
410 O << "\t.size\t" << name << ", " << Size << '\n';
411
412 EmitGlobalConstant(C);
413}