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 *TAI, bool V)
48 : AsmPrinter(O, TM, TAI, 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 (TAI->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 // Call the autogenerated instruction printer routines.
157 printInstruction(MI);
160 void SystemZAsmPrinter::printPCRelImmOperand(const MachineInstr *MI, int OpNum) {
161 const MachineOperand &MO = MI->getOperand(OpNum);
162 switch (MO.getType()) {
163 case MachineOperand::MO_Immediate:
166 case MachineOperand::MO_MachineBasicBlock:
167 printBasicBlockLabel(MO.getMBB(), false, false, VerboseAsm);
169 case MachineOperand::MO_GlobalAddress: {
170 const GlobalValue *GV = MO.getGlobal();
171 std::string Name = Mang->getMangledName(GV);
175 // Assemble calls via PLT for externally visible symbols if PIC.
176 if (TM.getRelocationModel() == Reloc::PIC_ &&
177 !GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
178 !GV->hasLocalLinkage())
181 printOffset(MO.getOffset());
184 case MachineOperand::MO_ExternalSymbol: {
185 std::string Name(TAI->getGlobalPrefix());
186 Name += MO.getSymbolName();
189 if (TM.getRelocationModel() == Reloc::PIC_)
195 assert(0 && "Not implemented yet!");
200 void SystemZAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
201 const char* Modifier) {
202 const MachineOperand &MO = MI->getOperand(OpNum);
203 switch (MO.getType()) {
204 case MachineOperand::MO_Register: {
205 assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
206 "Virtual registers should be already mapped!");
207 unsigned Reg = MO.getReg();
208 if (Modifier && strncmp(Modifier, "subreg", 6) == 0) {
209 if (strncmp(Modifier + 7, "even", 4) == 0)
210 Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_EVEN);
211 else if (strncmp(Modifier + 7, "odd", 3) == 0)
212 Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_ODD);
214 assert(0 && "Invalid subreg modifier");
217 O << '%' << TRI->getAsmName(Reg);
220 case MachineOperand::MO_Immediate:
223 case MachineOperand::MO_MachineBasicBlock:
224 printBasicBlockLabel(MO.getMBB());
226 case MachineOperand::MO_JumpTableIndex:
227 O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
231 case MachineOperand::MO_ConstantPoolIndex:
232 O << TAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
235 printOffset(MO.getOffset());
237 case MachineOperand::MO_GlobalAddress: {
238 const GlobalValue *GV = MO.getGlobal();
239 std::string Name = Mang->getMangledName(GV);
244 case MachineOperand::MO_ExternalSymbol: {
245 std::string Name(TAI->getGlobalPrefix());
246 Name += MO.getSymbolName();
251 assert(0 && "Not implemented yet!");
254 switch (MO.getTargetFlags()) {
256 llvm_unreachable("Unknown target flag on GV operand");
257 case SystemZII::MO_NO_FLAG:
259 case SystemZII::MO_GOTENT: O << "@GOTENT"; break;
260 case SystemZII::MO_PLT: O << "@PLT"; break;
263 printOffset(MO.getOffset());
266 void SystemZAsmPrinter::printRIAddrOperand(const MachineInstr *MI, int OpNum,
267 const char* Modifier) {
268 const MachineOperand &Base = MI->getOperand(OpNum);
270 // Print displacement operand.
271 printOperand(MI, OpNum+1);
273 // Print base operand (if any)
276 printOperand(MI, OpNum);
281 void SystemZAsmPrinter::printRRIAddrOperand(const MachineInstr *MI, int OpNum,
282 const char* Modifier) {
283 const MachineOperand &Base = MI->getOperand(OpNum);
284 const MachineOperand &Index = MI->getOperand(OpNum+2);
286 // Print displacement operand.
287 printOperand(MI, OpNum+1);
289 // Print base operand (if any)
292 printOperand(MI, OpNum);
293 if (Index.getReg()) {
295 printOperand(MI, OpNum+2);
299 assert(!Index.getReg() && "Should allocate base register first!");
302 void SystemZAsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
303 const TargetData *TD = TM.getTargetData();
305 if (!GVar->hasInitializer())
306 return; // External global require no code
308 // Check to see if this is a special global used by LLVM, if so, emit it.
309 if (EmitSpecialLLVMGlobal(GVar))
312 std::string name = Mang->getMangledName(GVar);
313 Constant *C = GVar->getInitializer();
314 unsigned Size = TD->getTypeAllocSize(C->getType());
315 unsigned Align = std::max(1U, TD->getPreferredAlignmentLog(GVar));
317 printVisibility(name, GVar->getVisibility());
319 O << "\t.type\t" << name << ",@object\n";
321 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GVar, Mang,
324 if (C->isNullValue() && !GVar->hasSection() &&
325 !GVar->isThreadLocal() &&
326 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
328 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
330 if (GVar->hasLocalLinkage())
331 O << "\t.local\t" << name << '\n';
333 O << TAI->getCOMMDirective() << name << ',' << Size;
334 if (TAI->getCOMMDirectiveTakesAlignment())
335 O << ',' << (TAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
338 O << "\t\t" << TAI->getCommentString() << ' ';
339 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
345 switch (GVar->getLinkage()) {
346 case GlobalValue::CommonLinkage:
347 case GlobalValue::LinkOnceAnyLinkage:
348 case GlobalValue::LinkOnceODRLinkage:
349 case GlobalValue::WeakAnyLinkage:
350 case GlobalValue::WeakODRLinkage:
351 O << "\t.weak\t" << name << '\n';
353 case GlobalValue::DLLExportLinkage:
354 case GlobalValue::AppendingLinkage:
355 // FIXME: appending linkage variables should go into a section of
356 // their name or something. For now, just emit them as external.
357 case GlobalValue::ExternalLinkage:
358 // If external or appending, declare as a global symbol
359 O << "\t.globl " << name << '\n';
361 case GlobalValue::PrivateLinkage:
362 case GlobalValue::LinkerPrivateLinkage:
363 case GlobalValue::InternalLinkage:
366 assert(0 && "Unknown linkage type!");
369 // Use 16-bit alignment by default to simplify bunch of stuff
370 EmitAlignment(Align, GVar, 1);
373 O << "\t\t\t\t" << TAI->getCommentString() << ' ';
374 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
377 if (TAI->hasDotTypeDotSizeDirective())
378 O << "\t.size\t" << name << ", " << Size << '\n';
380 EmitGlobalConstant(C);
383 // Force static initialization.
384 extern "C" void LLVMInitializeSystemZAsmPrinter() {
385 RegisterAsmPrinter<SystemZAsmPrinter> X(TheSystemZTarget);