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"
40 STATISTIC(EmittedInsts, "Number of machine instrs printed");
43 class SystemZAsmPrinter : public AsmPrinter {
45 SystemZAsmPrinter(formatted_raw_ostream &O, TargetMachine &TM,
46 const MCAsmInfo *MAI, bool V)
47 : AsmPrinter(O, TM, MAI, V) {}
49 virtual const char *getPassName() const {
50 return "SystemZ Assembly Printer";
53 void printOperand(const MachineInstr *MI, int OpNum,
54 const char* Modifier = 0);
55 void printPCRelImmOperand(const MachineInstr *MI, int OpNum);
56 void printRIAddrOperand(const MachineInstr *MI, int OpNum,
57 const char* Modifier = 0);
58 void printRRIAddrOperand(const MachineInstr *MI, int OpNum,
59 const char* Modifier = 0);
60 void printS16ImmOperand(const MachineInstr *MI, int OpNum) {
61 O << (int16_t)MI->getOperand(OpNum).getImm();
63 void printS32ImmOperand(const MachineInstr *MI, int OpNum) {
64 O << (int32_t)MI->getOperand(OpNum).getImm();
67 void printInstruction(const MachineInstr *MI); // autogenerated.
68 static const char *getRegisterName(unsigned RegNo);
70 void printMachineInstruction(const MachineInstr * MI);
72 void emitFunctionHeader(const MachineFunction &MF);
73 bool runOnMachineFunction(MachineFunction &F);
74 void PrintGlobalVariable(const GlobalVariable* GVar);
76 void getAnalysisUsage(AnalysisUsage &AU) const {
77 AsmPrinter::getAnalysisUsage(AU);
81 } // end of anonymous namespace
83 #include "SystemZGenAsmWriter.inc"
85 void SystemZAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
86 unsigned FnAlign = MF.getAlignment();
87 const Function *F = MF.getFunction();
89 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(F, Mang, TM));
91 EmitAlignment(FnAlign, F);
93 switch (F->getLinkage()) {
94 default: assert(0 && "Unknown linkage type!");
95 case Function::InternalLinkage: // Symbols default to internal.
96 case Function::PrivateLinkage:
97 case Function::LinkerPrivateLinkage:
99 case Function::ExternalLinkage:
101 CurrentFnSym->print(O, MAI);
104 case Function::LinkOnceAnyLinkage:
105 case Function::LinkOnceODRLinkage:
106 case Function::WeakAnyLinkage:
107 case Function::WeakODRLinkage:
109 CurrentFnSym->print(O, MAI);
114 printVisibility(CurrentFnSym, F->getVisibility());
117 CurrentFnSym->print(O, MAI);
119 CurrentFnSym->print(O, MAI);
123 bool SystemZAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
124 SetupMachineFunction(MF);
127 // Print out constants referenced by the function
128 EmitConstantPool(MF.getConstantPool());
130 // Print the 'header' of function
131 emitFunctionHeader(MF);
133 // Print out code for the function.
134 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
136 // Print a label for the basic block.
137 EmitBasicBlockStart(I);
139 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
141 // Print the assembly for the instruction.
142 printMachineInstruction(II);
145 if (MAI->hasDotTypeDotSizeDirective()) {
147 CurrentFnSym->print(O, MAI);
149 CurrentFnSym->print(O, MAI);
153 // Print out jump tables referenced by the function.
154 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
156 // We didn't modify anything
160 void SystemZAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
163 processDebugLoc(MI, true);
165 // Call the autogenerated instruction printer routines.
166 printInstruction(MI);
172 processDebugLoc(MI, false);
175 void SystemZAsmPrinter::printPCRelImmOperand(const MachineInstr *MI, int OpNum){
176 const MachineOperand &MO = MI->getOperand(OpNum);
177 switch (MO.getType()) {
178 case MachineOperand::MO_Immediate:
181 case MachineOperand::MO_MachineBasicBlock:
182 GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
184 case MachineOperand::MO_GlobalAddress: {
185 const GlobalValue *GV = MO.getGlobal();
186 GetGlobalValueSymbol(GV)->print(O, MAI);
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())
194 printOffset(MO.getOffset());
197 case MachineOperand::MO_ExternalSymbol: {
198 std::string Name(MAI->getGlobalPrefix());
199 Name += MO.getSymbolName();
202 if (TM.getRelocationModel() == Reloc::PIC_)
208 assert(0 && "Not implemented yet!");
213 void SystemZAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
214 const char* Modifier) {
215 const MachineOperand &MO = MI->getOperand(OpNum);
216 switch (MO.getType()) {
217 case MachineOperand::MO_Register: {
218 assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
219 "Virtual registers should be already mapped!");
220 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);
227 assert(0 && "Invalid subreg modifier");
230 O << '%' << getRegisterName(Reg);
233 case MachineOperand::MO_Immediate:
236 case MachineOperand::MO_MachineBasicBlock:
237 GetMBBSymbol(MO.getMBB()->getNumber())->print(O, MAI);
239 case MachineOperand::MO_JumpTableIndex:
240 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
244 case MachineOperand::MO_ConstantPoolIndex:
245 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
248 printOffset(MO.getOffset());
250 case MachineOperand::MO_GlobalAddress:
251 GetGlobalValueSymbol(MO.getGlobal())->print(O, MAI);
253 case MachineOperand::MO_ExternalSymbol: {
254 GetExternalSymbolSymbol(MO.getSymbolName())->print(O, MAI);
258 assert(0 && "Not implemented yet!");
261 switch (MO.getTargetFlags()) {
263 llvm_unreachable("Unknown target flag on GV operand");
264 case SystemZII::MO_NO_FLAG:
266 case SystemZII::MO_GOTENT: O << "@GOTENT"; break;
267 case SystemZII::MO_PLT: O << "@PLT"; break;
270 printOffset(MO.getOffset());
273 void SystemZAsmPrinter::printRIAddrOperand(const MachineInstr *MI, int OpNum,
274 const char* Modifier) {
275 const MachineOperand &Base = MI->getOperand(OpNum);
277 // Print displacement operand.
278 printOperand(MI, OpNum+1);
280 // Print base operand (if any)
283 printOperand(MI, OpNum);
288 void SystemZAsmPrinter::printRRIAddrOperand(const MachineInstr *MI, int OpNum,
289 const char* Modifier) {
290 const MachineOperand &Base = MI->getOperand(OpNum);
291 const MachineOperand &Index = MI->getOperand(OpNum+2);
293 // Print displacement operand.
294 printOperand(MI, OpNum+1);
296 // Print base operand (if any)
299 printOperand(MI, OpNum);
300 if (Index.getReg()) {
302 printOperand(MI, OpNum+2);
306 assert(!Index.getReg() && "Should allocate base register first!");
309 void SystemZAsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
310 const TargetData *TD = TM.getTargetData();
312 if (!GVar->hasInitializer())
313 return; // External global require no code
315 // Check to see if this is a special global used by LLVM, if so, emit it.
316 if (EmitSpecialLLVMGlobal(GVar))
319 MCSymbol *GVarSym = GetGlobalValueSymbol(GVar);
320 Constant *C = GVar->getInitializer();
321 unsigned Size = TD->getTypeAllocSize(C->getType());
322 unsigned Align = std::max(1U, TD->getPreferredAlignmentLog(GVar));
324 printVisibility(GVarSym, GVar->getVisibility());
327 GVarSym->print(O, MAI);
330 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GVar, Mang,
333 if (C->isNullValue() && !GVar->hasSection() &&
334 !GVar->isThreadLocal() &&
335 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
337 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
339 if (GVar->hasLocalLinkage()) {
341 GVarSym->print(O, MAI);
345 O << MAI->getCOMMDirective();
346 GVarSym->print(O, MAI);
348 if (MAI->getCOMMDirectiveTakesAlignment())
349 O << ',' << (MAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
352 O << "\t\t" << MAI->getCommentString() << ' ';
353 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
359 switch (GVar->getLinkage()) {
360 case GlobalValue::CommonLinkage:
361 case GlobalValue::LinkOnceAnyLinkage:
362 case GlobalValue::LinkOnceODRLinkage:
363 case GlobalValue::WeakAnyLinkage:
364 case GlobalValue::WeakODRLinkage:
366 GVarSym->print(O, MAI);
369 case GlobalValue::DLLExportLinkage:
370 case GlobalValue::AppendingLinkage:
371 // FIXME: appending linkage variables should go into a section of
372 // their name or something. For now, just emit them as external.
373 case GlobalValue::ExternalLinkage:
374 // If external or appending, declare as a global symbol
376 GVarSym->print(O, MAI);
379 case GlobalValue::PrivateLinkage:
380 case GlobalValue::LinkerPrivateLinkage:
381 case GlobalValue::InternalLinkage:
384 assert(0 && "Unknown linkage type!");
387 // Use 16-bit alignment by default to simplify bunch of stuff
388 EmitAlignment(Align, GVar, 1);
389 GVarSym->print(O, MAI);
392 O << "\t\t\t\t" << MAI->getCommentString() << ' ';
393 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
396 if (MAI->hasDotTypeDotSizeDirective()) {
398 GVarSym->print(O, MAI);
399 O << ", " << Size << '\n';
402 EmitGlobalConstant(C);
405 // Force static initialization.
406 extern "C" void LLVMInitializeSystemZAsmPrinter() {
407 RegisterAsmPrinter<SystemZAsmPrinter> X(TheSystemZTarget);