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:
100 O << "\t.globl\t" << *CurrentFnSym << '\n';
102 case Function::LinkOnceAnyLinkage:
103 case Function::LinkOnceODRLinkage:
104 case Function::WeakAnyLinkage:
105 case Function::WeakODRLinkage:
106 O << "\t.weak\t" << *CurrentFnSym << '\n';
110 printVisibility(CurrentFnSym, F->getVisibility());
112 O << "\t.type\t" << *CurrentFnSym << ",@function\n";
113 O << *CurrentFnSym << ":\n";
116 bool SystemZAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
117 SetupMachineFunction(MF);
120 // Print out constants referenced by the function
121 EmitConstantPool(MF.getConstantPool());
123 // Print the 'header' of function
124 emitFunctionHeader(MF);
126 // Print out code for the function.
127 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
129 // Print a label for the basic block.
130 EmitBasicBlockStart(I);
132 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
134 // Print the assembly for the instruction.
135 printMachineInstruction(II);
138 if (MAI->hasDotTypeDotSizeDirective())
139 O << "\t.size\t" << *CurrentFnSym << ", .-" << *CurrentFnSym << '\n';
141 // Print out jump tables referenced by the function.
142 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
144 // We didn't modify anything
148 void SystemZAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
151 processDebugLoc(MI, true);
153 // Call the autogenerated instruction printer routines.
154 printInstruction(MI);
160 processDebugLoc(MI, false);
163 void SystemZAsmPrinter::printPCRelImmOperand(const MachineInstr *MI, int OpNum){
164 const MachineOperand &MO = MI->getOperand(OpNum);
165 switch (MO.getType()) {
166 case MachineOperand::MO_Immediate:
169 case MachineOperand::MO_MachineBasicBlock:
170 O << *GetMBBSymbol(MO.getMBB()->getNumber());
172 case MachineOperand::MO_GlobalAddress: {
173 const GlobalValue *GV = MO.getGlobal();
174 O << *GetGlobalValueSymbol(GV);
176 // Assemble calls via PLT for externally visible symbols if PIC.
177 if (TM.getRelocationModel() == Reloc::PIC_ &&
178 !GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
179 !GV->hasLocalLinkage())
182 printOffset(MO.getOffset());
185 case MachineOperand::MO_ExternalSymbol: {
186 std::string Name(MAI->getGlobalPrefix());
187 Name += MO.getSymbolName();
190 if (TM.getRelocationModel() == Reloc::PIC_)
196 assert(0 && "Not implemented yet!");
201 void SystemZAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
202 const char* Modifier) {
203 const MachineOperand &MO = MI->getOperand(OpNum);
204 switch (MO.getType()) {
205 case MachineOperand::MO_Register: {
206 assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
207 "Virtual registers should be already mapped!");
208 unsigned Reg = MO.getReg();
209 if (Modifier && strncmp(Modifier, "subreg", 6) == 0) {
210 if (strncmp(Modifier + 7, "even", 4) == 0)
211 Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_EVEN);
212 else if (strncmp(Modifier + 7, "odd", 3) == 0)
213 Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_ODD);
215 assert(0 && "Invalid subreg modifier");
218 O << '%' << getRegisterName(Reg);
221 case MachineOperand::MO_Immediate:
224 case MachineOperand::MO_MachineBasicBlock:
225 O << *GetMBBSymbol(MO.getMBB()->getNumber());
227 case MachineOperand::MO_JumpTableIndex:
228 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
232 case MachineOperand::MO_ConstantPoolIndex:
233 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
236 printOffset(MO.getOffset());
238 case MachineOperand::MO_GlobalAddress:
239 O << *GetGlobalValueSymbol(MO.getGlobal());
241 case MachineOperand::MO_ExternalSymbol: {
242 O << *GetExternalSymbolSymbol(MO.getSymbolName());
246 assert(0 && "Not implemented yet!");
249 switch (MO.getTargetFlags()) {
251 llvm_unreachable("Unknown target flag on GV operand");
252 case SystemZII::MO_NO_FLAG:
254 case SystemZII::MO_GOTENT: O << "@GOTENT"; break;
255 case SystemZII::MO_PLT: O << "@PLT"; break;
258 printOffset(MO.getOffset());
261 void SystemZAsmPrinter::printRIAddrOperand(const MachineInstr *MI, int OpNum,
262 const char* Modifier) {
263 const MachineOperand &Base = MI->getOperand(OpNum);
265 // Print displacement operand.
266 printOperand(MI, OpNum+1);
268 // Print base operand (if any)
271 printOperand(MI, OpNum);
276 void SystemZAsmPrinter::printRRIAddrOperand(const MachineInstr *MI, int OpNum,
277 const char* Modifier) {
278 const MachineOperand &Base = MI->getOperand(OpNum);
279 const MachineOperand &Index = MI->getOperand(OpNum+2);
281 // Print displacement operand.
282 printOperand(MI, OpNum+1);
284 // Print base operand (if any)
287 printOperand(MI, OpNum);
288 if (Index.getReg()) {
290 printOperand(MI, OpNum+2);
294 assert(!Index.getReg() && "Should allocate base register first!");
297 void SystemZAsmPrinter::PrintGlobalVariable(const GlobalVariable* GVar) {
298 const TargetData *TD = TM.getTargetData();
300 if (!GVar->hasInitializer())
301 return; // External global require no code
303 // Check to see if this is a special global used by LLVM, if so, emit it.
304 if (EmitSpecialLLVMGlobal(GVar))
307 MCSymbol *GVarSym = GetGlobalValueSymbol(GVar);
308 Constant *C = GVar->getInitializer();
309 unsigned Size = TD->getTypeAllocSize(C->getType());
310 unsigned Align = std::max(1U, TD->getPreferredAlignmentLog(GVar));
312 printVisibility(GVarSym, GVar->getVisibility());
314 O << "\t.type\t" << *GVarSym << ",@object\n";
316 OutStreamer.SwitchSection(getObjFileLowering().SectionForGlobal(GVar, Mang,
319 if (C->isNullValue() && !GVar->hasSection() &&
320 !GVar->isThreadLocal() &&
321 (GVar->hasLocalLinkage() || GVar->isWeakForLinker())) {
323 if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it.
325 if (GVar->hasLocalLinkage())
326 O << "\t.local\t" << *GVarSym << '\n';
328 O << MAI->getCOMMDirective() << *GVarSym << ',' << Size;
329 if (MAI->getCOMMDirectiveTakesAlignment())
330 O << ',' << (MAI->getAlignmentIsInBytes() ? (1 << Align) : Align);
333 O << "\t\t" << MAI->getCommentString() << ' ';
334 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
340 switch (GVar->getLinkage()) {
341 case GlobalValue::CommonLinkage:
342 case GlobalValue::LinkOnceAnyLinkage:
343 case GlobalValue::LinkOnceODRLinkage:
344 case GlobalValue::WeakAnyLinkage:
345 case GlobalValue::WeakODRLinkage:
346 O << "\t.weak\t" << *GVarSym << '\n';
348 case GlobalValue::DLLExportLinkage:
349 case GlobalValue::AppendingLinkage:
350 // FIXME: appending linkage variables should go into a section of
351 // their name or something. For now, just emit them as external.
352 case GlobalValue::ExternalLinkage:
353 // If external or appending, declare as a global symbol
354 O << "\t.globl " << *GVarSym << '\n';
356 case GlobalValue::PrivateLinkage:
357 case GlobalValue::LinkerPrivateLinkage:
358 case GlobalValue::InternalLinkage:
361 assert(0 && "Unknown linkage type!");
364 // Use 16-bit alignment by default to simplify bunch of stuff
365 EmitAlignment(Align, GVar, 1);
366 O << *GVarSym << ":";
368 O << "\t\t\t\t" << MAI->getCommentString() << ' ';
369 WriteAsOperand(O, GVar, /*PrintType=*/false, GVar->getParent());
372 if (MAI->hasDotTypeDotSizeDirective())
373 O << "\t.size\t" << *GVarSym << ", " << Size << '\n';
375 EmitGlobalConstant(C);
378 // Force static initialization.
379 extern "C" void LLVMInitializeSystemZAsmPrinter() {
380 RegisterAsmPrinter<SystemZAsmPrinter> X(TheSystemZTarget);