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/CodeGen/AsmPrinter.h"
23 #include "llvm/CodeGen/DwarfWriter.h"
24 #include "llvm/CodeGen/MachineModuleInfo.h"
25 #include "llvm/CodeGen/MachineFunctionPass.h"
26 #include "llvm/CodeGen/MachineConstantPool.h"
27 #include "llvm/CodeGen/MachineInstr.h"
28 #include "llvm/Target/TargetAsmInfo.h"
29 #include "llvm/Target/TargetData.h"
30 #include "llvm/ADT/Statistic.h"
31 #include "llvm/Support/Compiler.h"
32 #include "llvm/Support/Mangler.h"
33 #include "llvm/Support/raw_ostream.h"
37 STATISTIC(EmittedInsts, "Number of machine instrs printed");
40 class VISIBILITY_HIDDEN SystemZAsmPrinter : public AsmPrinter {
42 SystemZAsmPrinter(raw_ostream &O, SystemZTargetMachine &TM,
43 const TargetAsmInfo *TAI,
44 CodeGenOpt::Level OL, bool V)
45 : AsmPrinter(O, TM, TAI, OL, V) {}
47 virtual const char *getPassName() const {
48 return "SystemZ Assembly Printer";
51 void printOperand(const MachineInstr *MI, int OpNum,
52 const char* Modifier = 0);
53 void printRIAddrOperand(const MachineInstr *MI, int OpNum,
54 const char* Modifier = 0);
55 void printRRIAddrOperand(const MachineInstr *MI, int OpNum,
56 const char* Modifier = 0);
57 void printS16ImmOperand(const MachineInstr *MI, int OpNum) {
58 O << (int16_t)MI->getOperand(OpNum).getImm();
60 void printS32ImmOperand(const MachineInstr *MI, int OpNum) {
61 O << (int32_t)MI->getOperand(OpNum).getImm();
64 bool printInstruction(const MachineInstr *MI); // autogenerated.
65 void printMachineInstruction(const MachineInstr * MI);
67 void emitFunctionHeader(const MachineFunction &MF);
68 bool runOnMachineFunction(MachineFunction &F);
69 bool doInitialization(Module &M);
70 bool doFinalization(Module &M);
72 void getAnalysisUsage(AnalysisUsage &AU) const {
73 AsmPrinter::getAnalysisUsage(AU);
77 } // end of anonymous namespace
79 #include "SystemZGenAsmWriter.inc"
81 /// createSystemZCodePrinterPass - Returns a pass that prints the SystemZ
82 /// assembly code for a MachineFunction to the given output stream,
83 /// using the given target machine description. This should work
84 /// regardless of whether the function is in SSA form.
86 FunctionPass *llvm::createSystemZCodePrinterPass(raw_ostream &o,
87 SystemZTargetMachine &tm,
88 CodeGenOpt::Level OptLevel,
90 return new SystemZAsmPrinter(o, tm, tm.getTargetAsmInfo(), OptLevel, verbose);
93 bool SystemZAsmPrinter::doInitialization(Module &M) {
94 Mang = new Mangler(M, "", TAI->getPrivateGlobalPrefix());
95 return false; // success
99 bool SystemZAsmPrinter::doFinalization(Module &M) {
100 return AsmPrinter::doFinalization(M);
103 void SystemZAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
104 const Function *F = MF.getFunction();
106 SwitchToSection(TAI->SectionForGlobal(F));
108 unsigned FnAlign = 4;
109 if (F->hasFnAttr(Attribute::OptimizeForSize))
112 EmitAlignment(FnAlign, F);
114 switch (F->getLinkage()) {
115 default: assert(0 && "Unknown linkage type!");
116 case Function::InternalLinkage: // Symbols default to internal.
117 case Function::PrivateLinkage:
119 case Function::ExternalLinkage:
120 O << "\t.globl\t" << CurrentFnName << '\n';
122 case Function::LinkOnceAnyLinkage:
123 case Function::LinkOnceODRLinkage:
124 case Function::WeakAnyLinkage:
125 case Function::WeakODRLinkage:
126 O << "\t.weak\t" << CurrentFnName << '\n';
130 printVisibility(CurrentFnName, F->getVisibility());
132 O << "\t.type\t" << CurrentFnName << ",@function\n"
133 << CurrentFnName << ":\n";
136 bool SystemZAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
137 SetupMachineFunction(MF);
140 // Print the 'header' of function
141 emitFunctionHeader(MF);
143 // Print out code for the function.
144 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
146 // Print a label for the basic block.
147 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
148 // This is an entry block or a block that's only reachable via a
149 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
151 printBasicBlockLabel(I, true, true, VerboseAsm);
155 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
157 // Print the assembly for the instruction.
158 printMachineInstruction(II);
161 if (TAI->hasDotTypeDotSizeDirective())
162 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
166 // We didn't modify anything
170 void SystemZAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
173 // Call the autogenerated instruction printer routines.
174 if (printInstruction(MI))
177 assert(0 && "Should not happen");
180 void SystemZAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
181 const char* Modifier) {
182 const MachineOperand &MO = MI->getOperand(OpNum);
183 switch (MO.getType()) {
184 case MachineOperand::MO_Register:
185 assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
186 "Virtual registers should be already mapped!");
187 O << '%' << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
189 case MachineOperand::MO_Immediate:
192 case MachineOperand::MO_MachineBasicBlock:
193 printBasicBlockLabel(MO.getMBB());
195 case MachineOperand::MO_GlobalAddress: {
196 std::string Name = Mang->getValueName(MO.getGlobal());
197 assert(MO.getOffset() == 0 && "No offsets allowed!");
203 case MachineOperand::MO_ExternalSymbol: {
204 std::string Name(TAI->getGlobalPrefix());
205 Name += MO.getSymbolName();
210 assert(0 && "Not implemented yet!");
214 void SystemZAsmPrinter::printRIAddrOperand(const MachineInstr *MI, int OpNum,
215 const char* Modifier) {
216 const MachineOperand &Base = MI->getOperand(OpNum);
218 // Print displacement operand.
219 printOperand(MI, OpNum+1);
221 // Print base operand (if any)
224 printOperand(MI, OpNum);
229 void SystemZAsmPrinter::printRRIAddrOperand(const MachineInstr *MI, int OpNum,
230 const char* Modifier) {
231 const MachineOperand &Base = MI->getOperand(OpNum);
232 const MachineOperand &Index = MI->getOperand(OpNum+2);
234 // Print displacement operand.
235 printOperand(MI, OpNum+1);
237 // Print base operand (if any)
240 printOperand(MI, OpNum);
241 if (Index.getReg()) {
243 printOperand(MI, OpNum+2);
247 assert(!Index.getReg() && "Should allocate base register first!");