blob: ae449be9808ccd6a206e3a7a98f6d86bb4443bdb [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"
Anton Korobeynikov7df84622009-07-16 14:36:52 +000030#include "llvm/Target/TargetRegistry.h"
Anton Korobeynikov4403b932009-07-16 13:27:25 +000031#include "llvm/ADT/Statistic.h"
32#include "llvm/Support/Compiler.h"
Anton Korobeynikov7df84622009-07-16 14:36:52 +000033#include "llvm/Support/FormattedStream.h"
Anton Korobeynikov4403b932009-07-16 13:27:25 +000034#include "llvm/Support/Mangler.h"
Anton Korobeynikov4403b932009-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 Korobeynikov7df84622009-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 Korobeynikov4403b932009-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 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);
Chris Lattner40bbebd2009-07-21 18:38:57 +000070 void PrintGlobalVariable(const GlobalVariable* GVar);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000071
72 void getAnalysisUsage(AnalysisUsage &AU) const {
73 AsmPrinter::getAnalysisUsage(AU);
74 AU.setPreservesAll();
75 }
76 };
77} // end of anonymous namespace
78
79#include "SystemZGenAsmWriter.inc"
80
81/// createSystemZCodePrinterPass - Returns a pass that prints the SystemZ
82/// assembly code for a MachineFunction to the given output stream,
83/// using the given target machine description. This should work
84/// regardless of whether the function is in SSA form.
85///
Anton Korobeynikov7df84622009-07-16 14:36:52 +000086FunctionPass *llvm::createSystemZCodePrinterPass(formatted_raw_ostream &o,
87 TargetMachine &tm,
88 bool verbose) {
89 return new SystemZAsmPrinter(o, tm, tm.getTargetAsmInfo(), verbose);
Anton Korobeynikov4403b932009-07-16 13:27:25 +000090}
91
Anton Korobeynikov4403b932009-07-16 13:27:25 +000092void SystemZAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
Anton Korobeynikov7df84622009-07-16 14:36:52 +000093 unsigned FnAlign = MF.getAlignment();
Anton Korobeynikov4403b932009-07-16 13:27:25 +000094 const Function *F = MF.getFunction();
95
96 SwitchToSection(TAI->SectionForGlobal(F));
97
Anton Korobeynikov4403b932009-07-16 13:27:25 +000098 EmitAlignment(FnAlign, F);
99
100 switch (F->getLinkage()) {
101 default: assert(0 && "Unknown linkage type!");
102 case Function::InternalLinkage: // Symbols default to internal.
103 case Function::PrivateLinkage:
Bill Wendling3d10a5a2009-07-20 01:03:30 +0000104 case Function::LinkerPrivateLinkage:
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000105 break;
106 case Function::ExternalLinkage:
107 O << "\t.globl\t" << CurrentFnName << '\n';
108 break;
109 case Function::LinkOnceAnyLinkage:
110 case Function::LinkOnceODRLinkage:
111 case Function::WeakAnyLinkage:
112 case Function::WeakODRLinkage:
113 O << "\t.weak\t" << CurrentFnName << '\n';
114 break;
115 }
116
117 printVisibility(CurrentFnName, F->getVisibility());
118
119 O << "\t.type\t" << CurrentFnName << ",@function\n"
120 << CurrentFnName << ":\n";
121}
122
123bool SystemZAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
124 SetupMachineFunction(MF);
125 O << "\n\n";
126
Anton Korobeynikovae535672009-07-16 14:19:35 +0000127 // Print out constants referenced by the function
128 EmitConstantPool(MF.getConstantPool());
129
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000130 // Print the 'header' of function
131 emitFunctionHeader(MF);
132
133 // Print out code for the function.
134 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
135 I != E; ++I) {
136 // Print a label for the basic block.
137 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
138 // This is an entry block or a block that's only reachable via a
139 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
140 } else {
141 printBasicBlockLabel(I, true, true, VerboseAsm);
142 O << '\n';
143 }
144
145 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
146 II != E; ++II)
147 // Print the assembly for the instruction.
148 printMachineInstruction(II);
149 }
150
151 if (TAI->hasDotTypeDotSizeDirective())
152 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
153
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +0000154 // Print out jump tables referenced by the function.
155 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
156
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000157 O.flush();
158
159 // We didn't modify anything
160 return false;
161}
162
163void SystemZAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
164 ++EmittedInsts;
165
166 // Call the autogenerated instruction printer routines.
167 if (printInstruction(MI))
168 return;
169
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000170 llvm_unreachable("Unreachable!");
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000171}
172
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000173void SystemZAsmPrinter::printPCRelImmOperand(const MachineInstr *MI, int OpNum) {
174 const MachineOperand &MO = MI->getOperand(OpNum);
175 switch (MO.getType()) {
Anton Korobeynikovcd3dfaf2009-07-16 14:17:07 +0000176 case MachineOperand::MO_Immediate:
177 O << MO.getImm();
178 return;
179 case MachineOperand::MO_MachineBasicBlock:
180 printBasicBlockLabel(MO.getMBB(), false, false, VerboseAsm);
181 return;
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000182 case MachineOperand::MO_GlobalAddress: {
183 const GlobalValue *GV = MO.getGlobal();
Anton Korobeynikov7df84622009-07-16 14:36:52 +0000184 std::string Name = Mang->getMangledName(GV);
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000185
186 O << Name;
187
188 // Assemble calls via PLT for externally visible symbols if PIC.
189 if (TM.getRelocationModel() == Reloc::PIC_ &&
190 !GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
191 !GV->hasLocalLinkage())
192 O << "@PLT";
193
194 printOffset(MO.getOffset());
195 return;
196 }
197 case MachineOperand::MO_ExternalSymbol: {
198 std::string Name(TAI->getGlobalPrefix());
199 Name += MO.getSymbolName();
200 O << Name;
201
202 if (TM.getRelocationModel() == Reloc::PIC_)
203 O << "@PLT";
204
205 return;
206 }
207 default:
208 assert(0 && "Not implemented yet!");
209 }
210}
211
212
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000213void SystemZAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000214 const char* Modifier) {
Anton Korobeynikov1cc9dc72009-07-16 13:29:38 +0000215 const MachineOperand &MO = MI->getOperand(OpNum);
216 switch (MO.getType()) {
Anton Korobeynikoved002122009-07-16 14:04:01 +0000217 case MachineOperand::MO_Register: {
Anton Korobeynikov1cc9dc72009-07-16 13:29:38 +0000218 assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
219 "Virtual registers should be already mapped!");
Anton Korobeynikoved002122009-07-16 14:04:01 +0000220 unsigned Reg = MO.getReg();
221 if (Modifier && strncmp(Modifier, "subreg", 6) == 0) {
222 if (strncmp(Modifier + 7, "even", 4) == 0)
223 Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_EVEN);
224 else if (strncmp(Modifier + 7, "odd", 3) == 0)
225 Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_ODD);
226 else
227 assert(0 && "Invalid subreg modifier");
228 }
229
230 O << '%' << TRI->getAsmName(Reg);
Anton Korobeynikov1cc9dc72009-07-16 13:29:38 +0000231 return;
Anton Korobeynikoved002122009-07-16 14:04:01 +0000232 }
Anton Korobeynikov1cc9dc72009-07-16 13:29:38 +0000233 case MachineOperand::MO_Immediate:
Anton Korobeynikov4cc7ce02009-07-16 14:01:10 +0000234 O << MO.getImm();
Anton Korobeynikov1cc9dc72009-07-16 13:29:38 +0000235 return;
236 case MachineOperand::MO_MachineBasicBlock:
237 printBasicBlockLabel(MO.getMBB());
238 return;
Anton Korobeynikovc16cdc52009-07-16 14:07:50 +0000239 case MachineOperand::MO_JumpTableIndex:
240 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
241 << MO.getIndex();
242
243 return;
Anton Korobeynikovae535672009-07-16 14:19:35 +0000244 case MachineOperand::MO_ConstantPoolIndex:
245 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
246 << MO.getIndex();
247
248 printOffset(MO.getOffset());
249 break;
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000250 case MachineOperand::MO_GlobalAddress: {
Anton Korobeynikov70f717f2009-07-16 14:04:22 +0000251 const GlobalValue *GV = MO.getGlobal();
Anton Korobeynikov7df84622009-07-16 14:36:52 +0000252 std::string Name = Mang->getMangledName(GV);
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000253
254 O << Name;
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000255 break;
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000256 }
257 case MachineOperand::MO_ExternalSymbol: {
258 std::string Name(TAI->getGlobalPrefix());
259 Name += MO.getSymbolName();
260 O << Name;
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000261 break;
Anton Korobeynikovba249e42009-07-16 13:50:21 +0000262 }
Anton Korobeynikov1cc9dc72009-07-16 13:29:38 +0000263 default:
264 assert(0 && "Not implemented yet!");
265 }
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000266
267 switch (MO.getTargetFlags()) {
268 default:
Anton Korobeynikov31e87442009-07-18 13:33:17 +0000269 llvm_unreachable("Unknown target flag on GV operand");
Anton Korobeynikov6fe326c2009-07-16 14:16:05 +0000270 case SystemZII::MO_NO_FLAG:
271 break;
272 case SystemZII::MO_GOTENT: O << "@GOTENT"; break;
273 case SystemZII::MO_PLT: O << "@PLT"; break;
274 }
275
276 printOffset(MO.getOffset());
Anton Korobeynikov4403b932009-07-16 13:27:25 +0000277}
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000278
279void SystemZAsmPrinter::printRIAddrOperand(const MachineInstr *MI, int OpNum,
280 const char* Modifier) {
281 const MachineOperand &Base = MI->getOperand(OpNum);
282
283 // Print displacement operand.
284 printOperand(MI, OpNum+1);
285
286 // Print base operand (if any)
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000287 if (Base.getReg()) {
Anton Korobeynikov9e4816e2009-07-16 13:43:18 +0000288 O << '(';
289 printOperand(MI, OpNum);
290 O << ')';
291 }
292}
293
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000294void SystemZAsmPrinter::printRRIAddrOperand(const MachineInstr *MI, int OpNum,
295 const char* Modifier) {
296 const MachineOperand &Base = MI->getOperand(OpNum);
Anton Korobeynikovc4368a12009-07-16 13:48:42 +0000297 const MachineOperand &Index = MI->getOperand(OpNum+2);
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000298
299 // Print displacement operand.
Anton Korobeynikovc4368a12009-07-16 13:48:42 +0000300 printOperand(MI, OpNum+1);
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000301
302 // Print base operand (if any)
303 if (Base.getReg()) {
304 O << '(';
305 printOperand(MI, OpNum);
306 if (Index.getReg()) {
307 O << ',';
Anton Korobeynikovc4368a12009-07-16 13:48:42 +0000308 printOperand(MI, OpNum+2);
Anton Korobeynikov3360da92009-07-16 13:44:00 +0000309 }
310 O << ')';
311 } else
312 assert(!Index.getReg() && "Should allocate base register first!");
313}
314
Anton Korobeynikov70f717f2009-07-16 14:04:22 +0000315/// PrintUnmangledNameSafely - Print out the printable characters in the name.
316/// Don't print things like \\n or \\0.
Anton Korobeynikov7df84622009-07-16 14:36:52 +0000317static void PrintUnmangledNameSafely(const Value *V, formatted_raw_ostream &OS) {
Anton Korobeynikov70f717f2009-07-16 14:04:22 +0000318 for (const char *Name = V->getNameStart(), *E = Name+V->getNameLen();
319 Name != E; ++Name)
320 if (isprint(*Name))
321 OS << *Name;
322}
323
Chris Lattner40bbebd2009-07-21 18:38:57 +0000324void SystemZAsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
Anton Korobeynikov70f717f2009-07-16 14:04:22 +0000325 const TargetData *TD = TM.getTargetData();
326
327 if (!GVar->hasInitializer())
328 return; // External global require no code
329
330 // Check to see if this is a special global used by LLVM, if so, emit it.
331 if (EmitSpecialLLVMGlobal(GVar))
332 return;
333
Anton Korobeynikov7df84622009-07-16 14:36:52 +0000334 std::string name = Mang->getMangledName(GVar);
Anton Korobeynikov70f717f2009-07-16 14:04:22 +0000335 Constant *C = GVar->getInitializer();
Anton Korobeynikov7df84622009-07-16 14:36:52 +0000336 unsigned Size = TD->getTypeAllocSize(C->getType());
Anton Korobeynikov70f717f2009-07-16 14:04:22 +0000337 unsigned Align = std::max(1U, TD->getPreferredAlignmentLog(GVar));
338
339 printVisibility(name, GVar->getVisibility());
340
341 O << "\t.type\t" << name << ",@object\n";
342
343 SwitchToSection(TAI->SectionForGlobal(GVar));
344
345 if (C->isNullValue() && !GVar->hasSection() &&
346 !GVar->isThreadLocal() &&
347 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
348
349 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
350
351 if (GVar->hasLocalLinkage())
352 O << "\t.local\t" << name << '\n';
353
354 O << TAI->getCOMMDirective() << name << ',' << Size;
355 if (TAI->getCOMMDirectiveTakesAlignment())
356 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
357
358 if (VerboseAsm) {
359 O << "\t\t" << TAI->getCommentString() << ' ';
360 PrintUnmangledNameSafely(GVar, O);
361 }
362 O << '\n';
363 return;
364 }
365
366 switch (GVar->getLinkage()) {
367 case GlobalValue::CommonLinkage:
368 case GlobalValue::LinkOnceAnyLinkage:
369 case GlobalValue::LinkOnceODRLinkage:
370 case GlobalValue::WeakAnyLinkage:
371 case GlobalValue::WeakODRLinkage:
372 O << "\t.weak\t" << name << '\n';
373 break;
374 case GlobalValue::DLLExportLinkage:
375 case GlobalValue::AppendingLinkage:
376 // FIXME: appending linkage variables should go into a section of
377 // their name or something. For now, just emit them as external.
378 case GlobalValue::ExternalLinkage:
379 // If external or appending, declare as a global symbol
380 O << "\t.globl " << name << '\n';
381 // FALL THROUGH
382 case GlobalValue::PrivateLinkage:
Bill Wendling3d10a5a2009-07-20 01:03:30 +0000383 case GlobalValue::LinkerPrivateLinkage:
Anton Korobeynikov70f717f2009-07-16 14:04:22 +0000384 case GlobalValue::InternalLinkage:
385 break;
386 default:
387 assert(0 && "Unknown linkage type!");
388 }
389
390 // Use 16-bit alignment by default to simplify bunch of stuff
391 EmitAlignment(Align, GVar, 1);
392 O << name << ":";
393 if (VerboseAsm) {
394 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
395 PrintUnmangledNameSafely(GVar, O);
396 }
397 O << '\n';
398 if (TAI->hasDotTypeDotSizeDirective())
399 O << "\t.size\t" << name << ", " << Size << '\n';
400
401 EmitGlobalConstant(C);
402}
Anton Korobeynikov7df84622009-07-16 14:36:52 +0000403
404// Force static initialization.
405extern "C" void LLVMInitializeSystemZAsmPrinter() {
Anton Korobeynikov7df84622009-07-16 14:36:52 +0000406 TargetRegistry::RegisterAsmPrinter(TheSystemZTarget,
407 createSystemZCodePrinterPass);
408}