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 bool printInstruction(const MachineInstr *MI); // autogenerated.
58 void printMachineInstruction(const MachineInstr * MI);
60 void emitFunctionHeader(const MachineFunction &MF);
61 bool runOnMachineFunction(MachineFunction &F);
62 bool doInitialization(Module &M);
63 bool doFinalization(Module &M);
65 void getAnalysisUsage(AnalysisUsage &AU) const {
66 AsmPrinter::getAnalysisUsage(AU);
70 } // end of anonymous namespace
72 #include "SystemZGenAsmWriter.inc"
74 /// createSystemZCodePrinterPass - Returns a pass that prints the SystemZ
75 /// assembly code for a MachineFunction to the given output stream,
76 /// using the given target machine description. This should work
77 /// regardless of whether the function is in SSA form.
79 FunctionPass *llvm::createSystemZCodePrinterPass(raw_ostream &o,
80 SystemZTargetMachine &tm,
81 CodeGenOpt::Level OptLevel,
83 return new SystemZAsmPrinter(o, tm, tm.getTargetAsmInfo(), OptLevel, verbose);
86 bool SystemZAsmPrinter::doInitialization(Module &M) {
87 Mang = new Mangler(M, "", TAI->getPrivateGlobalPrefix());
88 return false; // success
92 bool SystemZAsmPrinter::doFinalization(Module &M) {
93 return AsmPrinter::doFinalization(M);
96 void SystemZAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
97 const Function *F = MF.getFunction();
99 SwitchToSection(TAI->SectionForGlobal(F));
101 unsigned FnAlign = 4;
102 if (F->hasFnAttr(Attribute::OptimizeForSize))
105 EmitAlignment(FnAlign, F);
107 switch (F->getLinkage()) {
108 default: assert(0 && "Unknown linkage type!");
109 case Function::InternalLinkage: // Symbols default to internal.
110 case Function::PrivateLinkage:
112 case Function::ExternalLinkage:
113 O << "\t.globl\t" << CurrentFnName << '\n';
115 case Function::LinkOnceAnyLinkage:
116 case Function::LinkOnceODRLinkage:
117 case Function::WeakAnyLinkage:
118 case Function::WeakODRLinkage:
119 O << "\t.weak\t" << CurrentFnName << '\n';
123 printVisibility(CurrentFnName, F->getVisibility());
125 O << "\t.type\t" << CurrentFnName << ",@function\n"
126 << CurrentFnName << ":\n";
129 bool SystemZAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
130 SetupMachineFunction(MF);
133 // Print the 'header' of function
134 emitFunctionHeader(MF);
136 // Print out code for the function.
137 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
139 // Print a label for the basic block.
140 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
141 // This is an entry block or a block that's only reachable via a
142 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
144 printBasicBlockLabel(I, true, true, VerboseAsm);
148 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
150 // Print the assembly for the instruction.
151 printMachineInstruction(II);
154 if (TAI->hasDotTypeDotSizeDirective())
155 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
159 // We didn't modify anything
163 void SystemZAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
166 // Call the autogenerated instruction printer routines.
167 if (printInstruction(MI))
170 assert(0 && "Should not happen");
173 void SystemZAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
174 const char* Modifier) {
175 const MachineOperand &MO = MI->getOperand(OpNum);
176 switch (MO.getType()) {
177 case MachineOperand::MO_Register:
178 assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
179 "Virtual registers should be already mapped!");
180 O << '%' << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
182 case MachineOperand::MO_Immediate:
185 case MachineOperand::MO_MachineBasicBlock:
186 printBasicBlockLabel(MO.getMBB());
189 assert(0 && "Not implemented yet!");
193 void SystemZAsmPrinter::printRIAddrOperand(const MachineInstr *MI, int OpNum,
194 const char* Modifier) {
195 const MachineOperand &Base = MI->getOperand(OpNum);
197 // Print displacement operand.
198 printOperand(MI, OpNum+1);
200 // Print base operand (if any)
203 printOperand(MI, OpNum);
208 void SystemZAsmPrinter::printRRIAddrOperand(const MachineInstr *MI, int OpNum,
209 const char* Modifier) {
210 const MachineOperand &Base = MI->getOperand(OpNum);
211 const MachineOperand &Index = MI->getOperand(OpNum+2);
213 // Print displacement operand.
214 printOperand(MI, OpNum+1);
216 // Print base operand (if any)
219 printOperand(MI, OpNum);
220 if (Index.getReg()) {
222 printOperand(MI, OpNum+2);
226 assert(!Index.getReg() && "Should allocate base register first!");