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);
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" << *CurrentFnSym << '\n';
101 case Function::LinkOnceAnyLinkage:
102 case Function::LinkOnceODRLinkage:
103 case Function::WeakAnyLinkage:
104 case Function::WeakODRLinkage:
105 O << "\t.weak\t" << *CurrentFnSym << '\n';
109 printVisibility(CurrentFnSym, F->getVisibility());
111 O << "\t.type\t" << *CurrentFnSym << ",@function\n";
112 O << *CurrentFnSym << ":\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 EmitBasicBlockStart(I);
131 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
133 // Print the assembly for the instruction.
134 printMachineInstruction(II);
137 if (MAI->hasDotTypeDotSizeDirective())
138 O << "\t.size\t" << *CurrentFnSym << ", .-" << *CurrentFnSym << '\n';
140 // Print out jump tables referenced by the function.
141 EmitJumpTableInfo(MF.getJumpTableInfo(), MF);
143 // We didn't modify anything
147 void SystemZAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
150 processDebugLoc(MI, true);
152 // Call the autogenerated instruction printer routines.
153 printInstruction(MI);
159 processDebugLoc(MI, false);
162 void SystemZAsmPrinter::printPCRelImmOperand(const MachineInstr *MI, int OpNum){
163 const MachineOperand &MO = MI->getOperand(OpNum);
164 switch (MO.getType()) {
165 case MachineOperand::MO_Immediate:
168 case MachineOperand::MO_MachineBasicBlock:
169 O << *GetMBBSymbol(MO.getMBB()->getNumber());
171 case MachineOperand::MO_GlobalAddress: {
172 const GlobalValue *GV = MO.getGlobal();
173 O << *GetGlobalValueSymbol(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(MAI->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 << '%' << getRegisterName(Reg);
220 case MachineOperand::MO_Immediate:
223 case MachineOperand::MO_MachineBasicBlock:
224 O << *GetMBBSymbol(MO.getMBB()->getNumber());
226 case MachineOperand::MO_JumpTableIndex:
227 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
231 case MachineOperand::MO_ConstantPoolIndex:
232 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
235 printOffset(MO.getOffset());
237 case MachineOperand::MO_GlobalAddress:
238 O << *GetGlobalValueSymbol(MO.getGlobal());
240 case MachineOperand::MO_ExternalSymbol: {
241 O << *GetExternalSymbolSymbol(MO.getSymbolName());
245 assert(0 && "Not implemented yet!");
248 switch (MO.getTargetFlags()) {
250 llvm_unreachable("Unknown target flag on GV operand");
251 case SystemZII::MO_NO_FLAG:
253 case SystemZII::MO_GOTENT: O << "@GOTENT"; break;
254 case SystemZII::MO_PLT: O << "@PLT"; break;
257 printOffset(MO.getOffset());
260 void SystemZAsmPrinter::printRIAddrOperand(const MachineInstr *MI, int OpNum,
261 const char* Modifier) {
262 const MachineOperand &Base = MI->getOperand(OpNum);
264 // Print displacement operand.
265 printOperand(MI, OpNum+1);
267 // Print base operand (if any)
270 printOperand(MI, OpNum);
275 void SystemZAsmPrinter::printRRIAddrOperand(const MachineInstr *MI, int OpNum,
276 const char* Modifier) {
277 const MachineOperand &Base = MI->getOperand(OpNum);
278 const MachineOperand &Index = MI->getOperand(OpNum+2);
280 // Print displacement operand.
281 printOperand(MI, OpNum+1);
283 // Print base operand (if any)
286 printOperand(MI, OpNum);
287 if (Index.getReg()) {
289 printOperand(MI, OpNum+2);
293 assert(!Index.getReg() && "Should allocate base register first!");
296 // Force static initialization.
297 extern "C" void LLVMInitializeSystemZAsmPrinter() {
298 RegisterAsmPrinter<SystemZAsmPrinter> X(TheSystemZTarget);