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/MC/MCSymbol.h"
32 #include "llvm/Target/TargetData.h"
33 #include "llvm/Target/TargetLoweringObjectFile.h"
34 #include "llvm/Target/TargetRegistry.h"
35 #include "llvm/ADT/Statistic.h"
36 #include "llvm/Support/Compiler.h"
37 #include "llvm/Support/FormattedStream.h"
38 #include "llvm/Support/Mangler.h"
42 STATISTIC(EmittedInsts, "Number of machine instrs printed");
45 class VISIBILITY_HIDDEN SystemZAsmPrinter : public AsmPrinter {
47 SystemZAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
48 const MCAsmInfo *MAI, bool V)
49 : AsmPrinter(O, TM, MAI, V) {}
51 virtual const char *getPassName() const {
52 return "SystemZ Assembly Printer";
55 void printOperand(const MachineInstr *MI, int OpNum,
56 const char* Modifier = 0);
57 void printPCRelImmOperand(const MachineInstr *MI, int OpNum);
58 void printRIAddrOperand(const MachineInstr *MI, int OpNum,
59 const char* Modifier = 0);
60 void printRRIAddrOperand(const MachineInstr *MI, int OpNum,
61 const char* Modifier = 0);
62 void printS16ImmOperand(const MachineInstr *MI, int OpNum) {
63 O << (int16_t)MI->getOperand(OpNum).getImm();
65 void printS32ImmOperand(const MachineInstr *MI, int OpNum) {
66 O << (int32_t)MI->getOperand(OpNum).getImm();
69 void printInstruction(const MachineInstr *MI); // autogenerated.
70 static const char *getRegisterName(unsigned RegNo);
72 void printMachineInstruction(const MachineInstr * MI);
74 void emitFunctionHeader(const MachineFunction &MF);
75 bool runOnMachineFunction(MachineFunction &F);
76 void PrintGlobalVariable(const GlobalVariable* GVar);
78 void getAnalysisUsage(AnalysisUsage &AU) const {
79 AsmPrinter::getAnalysisUsage(AU);
83 } // end of anonymous namespace
85 #include "SystemZGenAsmWriter.inc"
87 void SystemZAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
88 unsigned FnAlign = MF.getAlignment();
89 const Function *F = MF.getFunction();
91 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
93 EmitAlignment(FnAlign, F);
95 switch (F->getLinkage()) {
96 default: assert(0 && "Unknown linkage type!");
97 case Function::InternalLinkage: // Symbols default to internal.
98 case Function::PrivateLinkage:
99 case Function::LinkerPrivateLinkage:
101 case Function::ExternalLinkage:
102 O << "\t.globl\t" << CurrentFnName << '\n';
104 case Function::LinkOnceAnyLinkage:
105 case Function::LinkOnceODRLinkage:
106 case Function::WeakAnyLinkage:
107 case Function::WeakODRLinkage:
108 O << "\t.weak\t" << CurrentFnName << '\n';
112 printVisibility(CurrentFnName, F->getVisibility());
114 O << "\t.type\t" << CurrentFnName << ",@function\n"
115 << CurrentFnName << ":\n";
118 bool SystemZAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
119 SetupMachineFunction(MF);
122 // Print out constants referenced by the function
123 EmitConstantPool(MF.getConstantPool());
125 // Print the 'header' of function
126 emitFunctionHeader(MF);
128 // Print out code for the function.
129 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
131 // Print a label for the basic block.
132 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
133 // This is an entry block or a block that's only reachable via a
134 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
136 EmitBasicBlockStart(I);
140 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
142 // Print the assembly for the instruction.
143 printMachineInstruction(II);
146 if (MAI->hasDotTypeDotSizeDirective())
147 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
149 // Print out jump tables referenced by the function.
150 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
152 // We didn't modify anything
156 void SystemZAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
159 processDebugLoc(MI, true);
161 // Call the autogenerated instruction printer routines.
162 printInstruction(MI);
164 if (VerboseAsm && !MI->getDebugLoc().isUnknown())
168 processDebugLoc(MI, false);
171 void SystemZAsmPrinter::printPCRelImmOperand(const MachineInstr *MI, int OpNum){
172 const MachineOperand &MO = MI->getOperand(OpNum);
173 switch (MO.getType()) {
174 case MachineOperand::MO_Immediate:
177 case MachineOperand::MO_MachineBasicBlock:
178 GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
180 case MachineOperand::MO_GlobalAddress: {
181 const GlobalValue *GV = MO.getGlobal();
182 std::string Name = Mang->getMangledName(GV);
186 // Assemble calls via PLT for externally visible symbols if PIC.
187 if (TM.getRelocationModel() == Reloc::PIC_ &&
188 !GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
189 !GV->hasLocalLinkage())
192 printOffset(MO.getOffset());
195 case MachineOperand::MO_ExternalSymbol: {
196 std::string Name(MAI->getGlobalPrefix());
197 Name += MO.getSymbolName();
200 if (TM.getRelocationModel() == Reloc::PIC_)
206 assert(0 && "Not implemented yet!");
211 void SystemZAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
212 const char* Modifier) {
213 const MachineOperand &MO = MI->getOperand(OpNum);
214 switch (MO.getType()) {
215 case MachineOperand::MO_Register: {
216 assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
217 "Virtual registers should be already mapped!");
218 unsigned Reg = MO.getReg();
219 if (Modifier && strncmp(Modifier, "subreg", 6) == 0) {
220 if (strncmp(Modifier + 7, "even", 4) == 0)
221 Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_EVEN);
222 else if (strncmp(Modifier + 7, "odd", 3) == 0)
223 Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_ODD);
225 assert(0 && "Invalid subreg modifier");
228 O << '%' << getRegisterName(Reg);
231 case MachineOperand::MO_Immediate:
234 case MachineOperand::MO_MachineBasicBlock:
235 GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
237 case MachineOperand::MO_JumpTableIndex:
238 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
242 case MachineOperand::MO_ConstantPoolIndex:
243 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
246 printOffset(MO.getOffset());
248 case MachineOperand::MO_GlobalAddress: {
249 const GlobalValue *GV = MO.getGlobal();
250 std::string Name = Mang->getMangledName(GV);
255 case MachineOperand::MO_ExternalSymbol: {
256 std::string Name(MAI->getGlobalPrefix());
257 Name += MO.getSymbolName();
262 assert(0 && "Not implemented yet!");
265 switch (MO.getTargetFlags()) {
267 llvm_unreachable("Unknown target flag on GV operand");
268 case SystemZII::MO_NO_FLAG:
270 case SystemZII::MO_GOTENT: O << "@GOTENT"; break;
271 case SystemZII::MO_PLT: O << "@PLT"; break;
274 printOffset(MO.getOffset());
277 void SystemZAsmPrinter::printRIAddrOperand(const MachineInstr *MI, int OpNum,
278 const char* Modifier) {
279 const MachineOperand &Base = MI->getOperand(OpNum);
281 // Print displacement operand.
282 printOperand(MI, OpNum+1);
284 // Print base operand (if any)
287 printOperand(MI, OpNum);
292 void SystemZAsmPrinter::printRRIAddrOperand(const MachineInstr *MI, int OpNum,
293 const char* Modifier) {
294 const MachineOperand &Base = MI->getOperand(OpNum);
295 const MachineOperand &Index = MI->getOperand(OpNum+2);
297 // Print displacement operand.
298 printOperand(MI, OpNum+1);
300 // Print base operand (if any)
303 printOperand(MI, OpNum);
304 if (Index.getReg()) {
306 printOperand(MI, OpNum+2);
310 assert(!Index.getReg() && "Should allocate base register first!");
313 void SystemZAsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
314 const TargetData *TD = TM.getTargetData();
316 if (!GVar->hasInitializer())
317 return; // External global require no code
319 // Check to see if this is a special global used by LLVM, if so, emit it.
320 if (EmitSpecialLLVMGlobal(GVar))
323 std::string name = Mang->getMangledName(GVar);
324 Constant *C = GVar->getInitializer();
325 unsigned Size = TD->getTypeAllocSize(C->getType());
326 unsigned Align = std::max(1U, TD->getPreferredAlignmentLog(GVar));
328 printVisibility(name, GVar->getVisibility());
330 O << "\t.type\t" << name << ",@object\n";
332 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GVar, Mang,
335 if (C->isNullValue() && !GVar->hasSection() &&
336 !GVar->isThreadLocal() &&
337 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
339 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
341 if (GVar->hasLocalLinkage())
342 O << "\t.local\t" << name << '\n';
344 O << MAI->getCOMMDirective() << name << ',' << Size;
345 if (MAI->getCOMMDirectiveTakesAlignment())
346 O << ',' << (MAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
349 O << "\t\t" << MAI->getCommentString() << ' ';
350 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
356 switch (GVar->getLinkage()) {
357 case GlobalValue::CommonLinkage:
358 case GlobalValue::LinkOnceAnyLinkage:
359 case GlobalValue::LinkOnceODRLinkage:
360 case GlobalValue::WeakAnyLinkage:
361 case GlobalValue::WeakODRLinkage:
362 O << "\t.weak\t" << name << '\n';
364 case GlobalValue::DLLExportLinkage:
365 case GlobalValue::AppendingLinkage:
366 // FIXME: appending linkage variables should go into a section of
367 // their name or something. For now, just emit them as external.
368 case GlobalValue::ExternalLinkage:
369 // If external or appending, declare as a global symbol
370 O << "\t.globl " << name << '\n';
372 case GlobalValue::PrivateLinkage:
373 case GlobalValue::LinkerPrivateLinkage:
374 case GlobalValue::InternalLinkage:
377 assert(0 && "Unknown linkage type!");
380 // Use 16-bit alignment by default to simplify bunch of stuff
381 EmitAlignment(Align, GVar, 1);
384 O << "\t\t\t\t" << MAI->getCommentString() << ' ';
385 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
388 if (MAI->hasDotTypeDotSizeDirective())
389 O << "\t.size\t" << name << ", " << Size << '\n';
391 EmitGlobalConstant(C);
394 // Force static initialization.
395 extern "C" void LLVMInitializeSystemZAsmPrinter() {
396 RegisterAsmPrinter<SystemZAsmPrinter> X(TheSystemZTarget);