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/ErrorHandling.h"
37 #include "llvm/Support/FormattedStream.h"
38 #include "llvm/Support/Mangler.h"
41 STATISTIC(EmittedInsts, "Number of machine instrs printed");
44 class 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 static const char *getRegisterName(unsigned RegNo);
71 void printMachineInstruction(const MachineInstr * MI);
73 void emitFunctionHeader(const MachineFunction &MF);
74 bool runOnMachineFunction(MachineFunction &F);
75 void PrintGlobalVariable(const GlobalVariable* GVar);
77 void getAnalysisUsage(AnalysisUsage &AU) const {
78 AsmPrinter::getAnalysisUsage(AU);
82 } // end of anonymous namespace
84 #include "SystemZGenAsmWriter.inc"
86 void SystemZAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
87 unsigned FnAlign = MF.getAlignment();
88 const Function *F = MF.getFunction();
90 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
92 EmitAlignment(FnAlign, F);
94 switch (F->getLinkage()) {
95 default: assert(0 && "Unknown linkage type!");
96 case Function::InternalLinkage: // Symbols default to internal.
97 case Function::PrivateLinkage:
98 case Function::LinkerPrivateLinkage:
100 case Function::ExternalLinkage:
102 CurrentFnSym->print(O, MAI);
105 case Function::LinkOnceAnyLinkage:
106 case Function::LinkOnceODRLinkage:
107 case Function::WeakAnyLinkage:
108 case Function::WeakODRLinkage:
110 CurrentFnSym->print(O, MAI);
115 printVisibility(CurrentFnSym, F->getVisibility());
118 CurrentFnSym->print(O, MAI);
120 CurrentFnSym->print(O, MAI);
124 bool SystemZAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
125 SetupMachineFunction(MF);
128 // Print out constants referenced by the function
129 EmitConstantPool(MF.getConstantPool());
131 // Print the 'header' of function
132 emitFunctionHeader(MF);
134 // Print out code for the function.
135 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
137 // Print a label for the basic block.
138 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()) {
148 CurrentFnSym->print(O, MAI);
150 CurrentFnSym->print(O, MAI);
154 // Print out jump tables referenced by the function.
155 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
157 // We didn't modify anything
161 void SystemZAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
164 processDebugLoc(MI, true);
166 // Call the autogenerated instruction printer routines.
167 printInstruction(MI);
173 processDebugLoc(MI, false);
176 void SystemZAsmPrinter::printPCRelImmOperand(const MachineInstr *MI, int OpNum){
177 const MachineOperand &MO = MI->getOperand(OpNum);
178 switch (MO.getType()) {
179 case MachineOperand::MO_Immediate:
182 case MachineOperand::MO_MachineBasicBlock:
183 GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
185 case MachineOperand::MO_GlobalAddress: {
186 const GlobalValue *GV = MO.getGlobal();
187 std::string Name = Mang->getMangledName(GV);
191 // Assemble calls via PLT for externally visible symbols if PIC.
192 if (TM.getRelocationModel() == Reloc::PIC_ &&
193 !GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
194 !GV->hasLocalLinkage())
197 printOffset(MO.getOffset());
200 case MachineOperand::MO_ExternalSymbol: {
201 std::string Name(MAI->getGlobalPrefix());
202 Name += MO.getSymbolName();
205 if (TM.getRelocationModel() == Reloc::PIC_)
211 assert(0 && "Not implemented yet!");
216 void SystemZAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
217 const char* Modifier) {
218 const MachineOperand &MO = MI->getOperand(OpNum);
219 switch (MO.getType()) {
220 case MachineOperand::MO_Register: {
221 assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
222 "Virtual registers should be already mapped!");
223 unsigned Reg = MO.getReg();
224 if (Modifier && strncmp(Modifier, "subreg", 6) == 0) {
225 if (strncmp(Modifier + 7, "even", 4) == 0)
226 Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_EVEN);
227 else if (strncmp(Modifier + 7, "odd", 3) == 0)
228 Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_ODD);
230 assert(0 && "Invalid subreg modifier");
233 O << '%' << getRegisterName(Reg);
236 case MachineOperand::MO_Immediate:
239 case MachineOperand::MO_MachineBasicBlock:
240 GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
242 case MachineOperand::MO_JumpTableIndex:
243 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
247 case MachineOperand::MO_ConstantPoolIndex:
248 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
251 printOffset(MO.getOffset());
253 case MachineOperand::MO_GlobalAddress: {
254 const GlobalValue *GV = MO.getGlobal();
255 std::string Name = Mang->getMangledName(GV);
260 case MachineOperand::MO_ExternalSymbol: {
261 std::string Name(MAI->getGlobalPrefix());
262 Name += MO.getSymbolName();
267 assert(0 && "Not implemented yet!");
270 switch (MO.getTargetFlags()) {
272 llvm_unreachable("Unknown target flag on GV operand");
273 case SystemZII::MO_NO_FLAG:
275 case SystemZII::MO_GOTENT: O << "@GOTENT"; break;
276 case SystemZII::MO_PLT: O << "@PLT"; break;
279 printOffset(MO.getOffset());
282 void SystemZAsmPrinter::printRIAddrOperand(const MachineInstr *MI, int OpNum,
283 const char* Modifier) {
284 const MachineOperand &Base = MI->getOperand(OpNum);
286 // Print displacement operand.
287 printOperand(MI, OpNum+1);
289 // Print base operand (if any)
292 printOperand(MI, OpNum);
297 void SystemZAsmPrinter::printRRIAddrOperand(const MachineInstr *MI, int OpNum,
298 const char* Modifier) {
299 const MachineOperand &Base = MI->getOperand(OpNum);
300 const MachineOperand &Index = MI->getOperand(OpNum+2);
302 // Print displacement operand.
303 printOperand(MI, OpNum+1);
305 // Print base operand (if any)
308 printOperand(MI, OpNum);
309 if (Index.getReg()) {
311 printOperand(MI, OpNum+2);
315 assert(!Index.getReg() && "Should allocate base register first!");
318 void SystemZAsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
319 const TargetData *TD = TM.getTargetData();
321 if (!GVar->hasInitializer())
322 return; // External global require no code
324 // Check to see if this is a special global used by LLVM, if so, emit it.
325 if (EmitSpecialLLVMGlobal(GVar))
328 MCSymbol *GVarSym = GetGlobalValueSymbol(GVar);
329 Constant *C = GVar->getInitializer();
330 unsigned Size = TD->getTypeAllocSize(C->getType());
331 unsigned Align = std::max(1U, TD->getPreferredAlignmentLog(GVar));
333 printVisibility(GVarSym, GVar->getVisibility());
336 GVarSym->print(O, MAI);
339 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GVar, Mang,
342 if (C->isNullValue() && !GVar->hasSection() &&
343 !GVar->isThreadLocal() &&
344 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
346 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
348 if (GVar->hasLocalLinkage()) {
350 GVarSym->print(O, MAI);
354 O << MAI->getCOMMDirective();
355 GVarSym->print(O, MAI);
357 if (MAI->getCOMMDirectiveTakesAlignment())
358 O << ',' << (MAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
361 O << "\t\t" << MAI->getCommentString() << ' ';
362 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
368 switch (GVar->getLinkage()) {
369 case GlobalValue::CommonLinkage:
370 case GlobalValue::LinkOnceAnyLinkage:
371 case GlobalValue::LinkOnceODRLinkage:
372 case GlobalValue::WeakAnyLinkage:
373 case GlobalValue::WeakODRLinkage:
375 GVarSym->print(O, MAI);
378 case GlobalValue::DLLExportLinkage:
379 case GlobalValue::AppendingLinkage:
380 // FIXME: appending linkage variables should go into a section of
381 // their name or something. For now, just emit them as external.
382 case GlobalValue::ExternalLinkage:
383 // If external or appending, declare as a global symbol
385 GVarSym->print(O, MAI);
388 case GlobalValue::PrivateLinkage:
389 case GlobalValue::LinkerPrivateLinkage:
390 case GlobalValue::InternalLinkage:
393 assert(0 && "Unknown linkage type!");
396 // Use 16-bit alignment by default to simplify bunch of stuff
397 EmitAlignment(Align, GVar, 1);
398 GVarSym->print(O, MAI);
401 O << "\t\t\t\t" << MAI->getCommentString() << ' ';
402 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
405 if (MAI->hasDotTypeDotSizeDirective()) {
407 GVarSym->print(O, MAI);
408 O << ", " << Size << '\n';
411 EmitGlobalConstant(C);
414 // Force static initialization.
415 extern "C" void LLVMInitializeSystemZAsmPrinter() {
416 RegisterAsmPrinter<SystemZAsmPrinter> X(TheSystemZTarget);