1 //===-- SystemZAsmPrinter.cpp - SystemZ LLVM assembly writer ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains a printer that converts from our internal representation
11 // of machine-dependent LLVM code to the SystemZ assembly language.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "asm-printer"
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/Assembly/Writer.h"
23 #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"
29 #include "llvm/MC/MCStreamer.h"
30 #include "llvm/MC/MCAsmInfo.h"
31 #include "llvm/Target/TargetData.h"
32 #include "llvm/Target/TargetLoweringObjectFile.h"
33 #include "llvm/Target/TargetRegistry.h"
34 #include "llvm/ADT/Statistic.h"
35 #include "llvm/Support/Compiler.h"
36 #include "llvm/Support/FormattedStream.h"
37 #include "llvm/Support/Mangler.h"
41 STATISTIC(EmittedInsts, "Number of machine instrs printed");
44 class VISIBILITY_HIDDEN SystemZAsmPrinter : public AsmPrinter {
46 SystemZAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
47 const MCAsmInfo *MAI, bool V)
48 : AsmPrinter(O, TM, MAI, V) {}
50 virtual const char *getPassName() const {
51 return "SystemZ Assembly Printer";
54 void printOperand(const MachineInstr *MI, int OpNum,
55 const char* Modifier = 0);
56 void printPCRelImmOperand(const MachineInstr *MI, int OpNum);
57 void printRIAddrOperand(const MachineInstr *MI, int OpNum,
58 const char* Modifier = 0);
59 void printRRIAddrOperand(const MachineInstr *MI, int OpNum,
60 const char* Modifier = 0);
61 void printS16ImmOperand(const MachineInstr *MI, int OpNum) {
62 O << (int16_t)MI->getOperand(OpNum).getImm();
64 void printS32ImmOperand(const MachineInstr *MI, int OpNum) {
65 O << (int32_t)MI->getOperand(OpNum).getImm();
68 void printInstruction(const MachineInstr *MI); // autogenerated.
69 void printMachineInstruction(const MachineInstr * MI);
71 void emitFunctionHeader(const MachineFunction &MF);
72 bool runOnMachineFunction(MachineFunction &F);
73 void PrintGlobalVariable(const GlobalVariable* GVar);
75 void getAnalysisUsage(AnalysisUsage &AU) const {
76 AsmPrinter::getAnalysisUsage(AU);
80 } // end of anonymous namespace
82 #include "SystemZGenAsmWriter.inc"
84 void SystemZAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
85 unsigned FnAlign = MF.getAlignment();
86 const Function *F = MF.getFunction();
88 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
90 EmitAlignment(FnAlign, F);
92 switch (F->getLinkage()) {
93 default: assert(0 && "Unknown linkage type!");
94 case Function::InternalLinkage: // Symbols default to internal.
95 case Function::PrivateLinkage:
96 case Function::LinkerPrivateLinkage:
98 case Function::ExternalLinkage:
99 O << "\t.globl\t" << CurrentFnName << '\n';
101 case Function::LinkOnceAnyLinkage:
102 case Function::LinkOnceODRLinkage:
103 case Function::WeakAnyLinkage:
104 case Function::WeakODRLinkage:
105 O << "\t.weak\t" << CurrentFnName << '\n';
109 printVisibility(CurrentFnName, F->getVisibility());
111 O << "\t.type\t" << CurrentFnName << ",@function\n"
112 << CurrentFnName << ":\n";
115 bool SystemZAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
116 SetupMachineFunction(MF);
119 // Print out constants referenced by the function
120 EmitConstantPool(MF.getConstantPool());
122 // Print the 'header' of function
123 emitFunctionHeader(MF);
125 // Print out code for the function.
126 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
128 // Print a label for the basic block.
129 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
130 // This is an entry block or a block that's only reachable via a
131 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
133 printBasicBlockLabel(I, true, true, VerboseAsm);
137 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
139 // Print the assembly for the instruction.
140 printMachineInstruction(II);
143 if (MAI->hasDotTypeDotSizeDirective())
144 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
146 // Print out jump tables referenced by the function.
147 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
149 // We didn't modify anything
153 void SystemZAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
156 processDebugLoc(MI->getDebugLoc());
158 // Call the autogenerated instruction printer routines.
159 printInstruction(MI);
161 if (VerboseAsm && !MI->getDebugLoc().isUnknown())
166 void SystemZAsmPrinter::printPCRelImmOperand(const MachineInstr *MI, int OpNum) {
167 const MachineOperand &MO = MI->getOperand(OpNum);
168 switch (MO.getType()) {
169 case MachineOperand::MO_Immediate:
172 case MachineOperand::MO_MachineBasicBlock:
173 printBasicBlockLabel(MO.getMBB(), false, false, VerboseAsm);
175 case MachineOperand::MO_GlobalAddress: {
176 const GlobalValue *GV = MO.getGlobal();
177 std::string Name = Mang->getMangledName(GV);
181 // Assemble calls via PLT for externally visible symbols if PIC.
182 if (TM.getRelocationModel() == Reloc::PIC_ &&
183 !GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
184 !GV->hasLocalLinkage())
187 printOffset(MO.getOffset());
190 case MachineOperand::MO_ExternalSymbol: {
191 std::string Name(MAI->getGlobalPrefix());
192 Name += MO.getSymbolName();
195 if (TM.getRelocationModel() == Reloc::PIC_)
201 assert(0 && "Not implemented yet!");
206 void SystemZAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
207 const char* Modifier) {
208 const MachineOperand &MO = MI->getOperand(OpNum);
209 switch (MO.getType()) {
210 case MachineOperand::MO_Register: {
211 assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
212 "Virtual registers should be already mapped!");
213 unsigned Reg = MO.getReg();
214 if (Modifier && strncmp(Modifier, "subreg", 6) == 0) {
215 if (strncmp(Modifier + 7, "even", 4) == 0)
216 Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_EVEN);
217 else if (strncmp(Modifier + 7, "odd", 3) == 0)
218 Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_ODD);
220 assert(0 && "Invalid subreg modifier");
223 O << '%' << TRI->getAsmName(Reg);
226 case MachineOperand::MO_Immediate:
229 case MachineOperand::MO_MachineBasicBlock:
230 printBasicBlockLabel(MO.getMBB());
232 case MachineOperand::MO_JumpTableIndex:
233 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
237 case MachineOperand::MO_ConstantPoolIndex:
238 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
241 printOffset(MO.getOffset());
243 case MachineOperand::MO_GlobalAddress: {
244 const GlobalValue *GV = MO.getGlobal();
245 std::string Name = Mang->getMangledName(GV);
250 case MachineOperand::MO_ExternalSymbol: {
251 std::string Name(MAI->getGlobalPrefix());
252 Name += MO.getSymbolName();
257 assert(0 && "Not implemented yet!");
260 switch (MO.getTargetFlags()) {
262 llvm_unreachable("Unknown target flag on GV operand");
263 case SystemZII::MO_NO_FLAG:
265 case SystemZII::MO_GOTENT: O << "@GOTENT"; break;
266 case SystemZII::MO_PLT: O << "@PLT"; break;
269 printOffset(MO.getOffset());
272 void SystemZAsmPrinter::printRIAddrOperand(const MachineInstr *MI, int OpNum,
273 const char* Modifier) {
274 const MachineOperand &Base = MI->getOperand(OpNum);
276 // Print displacement operand.
277 printOperand(MI, OpNum+1);
279 // Print base operand (if any)
282 printOperand(MI, OpNum);
287 void SystemZAsmPrinter::printRRIAddrOperand(const MachineInstr *MI, int OpNum,
288 const char* Modifier) {
289 const MachineOperand &Base = MI->getOperand(OpNum);
290 const MachineOperand &Index = MI->getOperand(OpNum+2);
292 // Print displacement operand.
293 printOperand(MI, OpNum+1);
295 // Print base operand (if any)
298 printOperand(MI, OpNum);
299 if (Index.getReg()) {
301 printOperand(MI, OpNum+2);
305 assert(!Index.getReg() && "Should allocate base register first!");
308 void SystemZAsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
309 const TargetData *TD = TM.getTargetData();
311 if (!GVar->hasInitializer())
312 return; // External global require no code
314 // Check to see if this is a special global used by LLVM, if so, emit it.
315 if (EmitSpecialLLVMGlobal(GVar))
318 std::string name = Mang->getMangledName(GVar);
319 Constant *C = GVar->getInitializer();
320 unsigned Size = TD->getTypeAllocSize(C->getType());
321 unsigned Align = std::max(1U, TD->getPreferredAlignmentLog(GVar));
323 printVisibility(name, GVar->getVisibility());
325 O << "\t.type\t" << name << ",@object\n";
327 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GVar, Mang,
330 if (C->isNullValue() && !GVar->hasSection() &&
331 !GVar->isThreadLocal() &&
332 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
334 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
336 if (GVar->hasLocalLinkage())
337 O << "\t.local\t" << name << '\n';
339 O << MAI->getCOMMDirective() << name << ',' << Size;
340 if (MAI->getCOMMDirectiveTakesAlignment())
341 O << ',' << (MAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
344 O << "\t\t" << MAI->getCommentString() << ' ';
345 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
351 switch (GVar->getLinkage()) {
352 case GlobalValue::CommonLinkage:
353 case GlobalValue::LinkOnceAnyLinkage:
354 case GlobalValue::LinkOnceODRLinkage:
355 case GlobalValue::WeakAnyLinkage:
356 case GlobalValue::WeakODRLinkage:
357 O << "\t.weak\t" << name << '\n';
359 case GlobalValue::DLLExportLinkage:
360 case GlobalValue::AppendingLinkage:
361 // FIXME: appending linkage variables should go into a section of
362 // their name or something. For now, just emit them as external.
363 case GlobalValue::ExternalLinkage:
364 // If external or appending, declare as a global symbol
365 O << "\t.globl " << name << '\n';
367 case GlobalValue::PrivateLinkage:
368 case GlobalValue::LinkerPrivateLinkage:
369 case GlobalValue::InternalLinkage:
372 assert(0 && "Unknown linkage type!");
375 // Use 16-bit alignment by default to simplify bunch of stuff
376 EmitAlignment(Align, GVar, 1);
379 O << "\t\t\t\t" << MAI->getCommentString() << ' ';
380 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
383 if (MAI->hasDotTypeDotSizeDirective())
384 O << "\t.size\t" << name << ", " << Size << '\n';
386 EmitGlobalConstant(C);
389 // Force static initialization.
390 extern "C" void LLVMInitializeSystemZAsmPrinter() {
391 RegisterAsmPrinter<SystemZAsmPrinter> X(TheSystemZTarget);