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 bool printInstruction(const MachineInstr *MI); // autogenerated.
56 void printMachineInstruction(const MachineInstr * MI);
58 void emitFunctionHeader(const MachineFunction &MF);
59 bool runOnMachineFunction(MachineFunction &F);
60 bool doInitialization(Module &M);
61 bool doFinalization(Module &M);
63 void getAnalysisUsage(AnalysisUsage &AU) const {
64 AsmPrinter::getAnalysisUsage(AU);
68 } // end of anonymous namespace
70 #include "SystemZGenAsmWriter.inc"
72 /// createSystemZCodePrinterPass - Returns a pass that prints the SystemZ
73 /// assembly code for a MachineFunction to the given output stream,
74 /// using the given target machine description. This should work
75 /// regardless of whether the function is in SSA form.
77 FunctionPass *llvm::createSystemZCodePrinterPass(raw_ostream &o,
78 SystemZTargetMachine &tm,
79 CodeGenOpt::Level OptLevel,
81 return new SystemZAsmPrinter(o, tm, tm.getTargetAsmInfo(), OptLevel, verbose);
84 bool SystemZAsmPrinter::doInitialization(Module &M) {
85 Mang = new Mangler(M, "", TAI->getPrivateGlobalPrefix());
86 return false; // success
90 bool SystemZAsmPrinter::doFinalization(Module &M) {
91 return AsmPrinter::doFinalization(M);
94 void SystemZAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
95 const Function *F = MF.getFunction();
97 SwitchToSection(TAI->SectionForGlobal(F));
100 if (F->hasFnAttr(Attribute::OptimizeForSize))
103 EmitAlignment(FnAlign, F);
105 switch (F->getLinkage()) {
106 default: assert(0 && "Unknown linkage type!");
107 case Function::InternalLinkage: // Symbols default to internal.
108 case Function::PrivateLinkage:
110 case Function::ExternalLinkage:
111 O << "\t.globl\t" << CurrentFnName << '\n';
113 case Function::LinkOnceAnyLinkage:
114 case Function::LinkOnceODRLinkage:
115 case Function::WeakAnyLinkage:
116 case Function::WeakODRLinkage:
117 O << "\t.weak\t" << CurrentFnName << '\n';
121 printVisibility(CurrentFnName, F->getVisibility());
123 O << "\t.type\t" << CurrentFnName << ",@function\n"
124 << CurrentFnName << ":\n";
127 bool SystemZAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
128 SetupMachineFunction(MF);
131 // Print the 'header' of function
132 emitFunctionHeader(MF);
134 // Print out code for the function.
135 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
137 // Print a label for the basic block.
138 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
139 // This is an entry block or a block that's only reachable via a
140 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
142 printBasicBlockLabel(I, true, true, VerboseAsm);
146 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
148 // Print the assembly for the instruction.
149 printMachineInstruction(II);
152 if (TAI->hasDotTypeDotSizeDirective())
153 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
157 // We didn't modify anything
161 void SystemZAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
164 // Call the autogenerated instruction printer routines.
165 if (printInstruction(MI))
168 assert(0 && "Should not happen");
171 void SystemZAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
172 const char* Modifier) {
173 const MachineOperand &MO = MI->getOperand(OpNum);
174 switch (MO.getType()) {
175 case MachineOperand::MO_Register:
176 assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
177 "Virtual registers should be already mapped!");
178 O << '%' << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
180 case MachineOperand::MO_Immediate:
183 case MachineOperand::MO_MachineBasicBlock:
184 printBasicBlockLabel(MO.getMBB());
187 assert(0 && "Not implemented yet!");
191 void SystemZAsmPrinter::printRIAddrOperand(const MachineInstr *MI, int OpNum,
192 const char* Modifier) {
193 const MachineOperand &Base = MI->getOperand(OpNum);
195 // Print displacement operand.
196 printOperand(MI, OpNum+1);
198 // Print base operand (if any)
199 if (!(Base.isReg() && Base.getReg() == SystemZ::R0D)) {
201 printOperand(MI, OpNum);