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 bool runOnMachineFunction(MachineFunction &F);
74 void getAnalysisUsage(AnalysisUsage &AU) const {
75 AsmPrinter::getAnalysisUsage(AU);
79 } // end of anonymous namespace
81 #include "SystemZGenAsmWriter.inc"
85 bool SystemZAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
86 SetupMachineFunction(MF);
89 // Print the 'header' of function
92 // Print out code for the function.
93 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
95 // Print a label for the basic block.
96 EmitBasicBlockStart(I);
98 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
100 // Print the assembly for the instruction.
101 printMachineInstruction(II);
104 if (MAI->hasDotTypeDotSizeDirective())
105 O << "\t.size\t" << *CurrentFnSym << ", .-" << *CurrentFnSym << '\n';
107 // Print out jump tables referenced by the function.
110 // We didn't modify anything
114 void SystemZAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
117 processDebugLoc(MI, true);
119 // Call the autogenerated instruction printer routines.
120 printInstruction(MI);
126 processDebugLoc(MI, false);
129 void SystemZAsmPrinter::printPCRelImmOperand(const MachineInstr *MI, int OpNum){
130 const MachineOperand &MO = MI->getOperand(OpNum);
131 switch (MO.getType()) {
132 case MachineOperand::MO_Immediate:
135 case MachineOperand::MO_MachineBasicBlock:
136 O << *MO.getMBB()->getSymbol(OutContext);
138 case MachineOperand::MO_GlobalAddress: {
139 const GlobalValue *GV = MO.getGlobal();
140 O << *GetGlobalValueSymbol(GV);
142 // Assemble calls via PLT for externally visible symbols if PIC.
143 if (TM.getRelocationModel() == Reloc::PIC_ &&
144 !GV->hasHiddenVisibility() && !GV->hasProtectedVisibility() &&
145 !GV->hasLocalLinkage())
148 printOffset(MO.getOffset());
151 case MachineOperand::MO_ExternalSymbol: {
152 std::string Name(MAI->getGlobalPrefix());
153 Name += MO.getSymbolName();
156 if (TM.getRelocationModel() == Reloc::PIC_)
162 assert(0 && "Not implemented yet!");
167 void SystemZAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
168 const char* Modifier) {
169 const MachineOperand &MO = MI->getOperand(OpNum);
170 switch (MO.getType()) {
171 case MachineOperand::MO_Register: {
172 assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
173 "Virtual registers should be already mapped!");
174 unsigned Reg = MO.getReg();
175 if (Modifier && strncmp(Modifier, "subreg", 6) == 0) {
176 if (strncmp(Modifier + 7, "even", 4) == 0)
177 Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_EVEN);
178 else if (strncmp(Modifier + 7, "odd", 3) == 0)
179 Reg = TRI->getSubReg(Reg, SystemZ::SUBREG_ODD);
181 assert(0 && "Invalid subreg modifier");
184 O << '%' << getRegisterName(Reg);
187 case MachineOperand::MO_Immediate:
190 case MachineOperand::MO_MachineBasicBlock:
191 O << *MO.getMBB()->getSymbol(OutContext);
193 case MachineOperand::MO_JumpTableIndex:
194 O << MAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() << '_'
198 case MachineOperand::MO_ConstantPoolIndex:
199 O << MAI->getPrivateGlobalPrefix() << "CPI" << getFunctionNumber() << '_'
202 printOffset(MO.getOffset());
204 case MachineOperand::MO_GlobalAddress:
205 O << *GetGlobalValueSymbol(MO.getGlobal());
207 case MachineOperand::MO_ExternalSymbol: {
208 O << *GetExternalSymbolSymbol(MO.getSymbolName());
212 assert(0 && "Not implemented yet!");
215 switch (MO.getTargetFlags()) {
217 llvm_unreachable("Unknown target flag on GV operand");
218 case SystemZII::MO_NO_FLAG:
220 case SystemZII::MO_GOTENT: O << "@GOTENT"; break;
221 case SystemZII::MO_PLT: O << "@PLT"; break;
224 printOffset(MO.getOffset());
227 void SystemZAsmPrinter::printRIAddrOperand(const MachineInstr *MI, int OpNum,
228 const char* Modifier) {
229 const MachineOperand &Base = MI->getOperand(OpNum);
231 // Print displacement operand.
232 printOperand(MI, OpNum+1);
234 // Print base operand (if any)
237 printOperand(MI, OpNum);
242 void SystemZAsmPrinter::printRRIAddrOperand(const MachineInstr *MI, int OpNum,
243 const char* Modifier) {
244 const MachineOperand &Base = MI->getOperand(OpNum);
245 const MachineOperand &Index = MI->getOperand(OpNum+2);
247 // Print displacement operand.
248 printOperand(MI, OpNum+1);
250 // Print base operand (if any)
253 printOperand(MI, OpNum);
254 if (Index.getReg()) {
256 printOperand(MI, OpNum+2);
260 assert(!Index.getReg() && "Should allocate base register first!");
263 // Force static initialization.
264 extern "C" void LLVMInitializeSystemZAsmPrinter() {
265 RegisterAsmPrinter<SystemZAsmPrinter> X(TheSystemZTarget);