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 bool printInstruction(const MachineInstr *MI); // autogenerated.
54 void printMachineInstruction(const MachineInstr * MI);
56 void emitFunctionHeader(const MachineFunction &MF);
57 bool runOnMachineFunction(MachineFunction &F);
58 bool doInitialization(Module &M);
59 bool doFinalization(Module &M);
61 void getAnalysisUsage(AnalysisUsage &AU) const {
62 AsmPrinter::getAnalysisUsage(AU);
66 } // end of anonymous namespace
68 #include "SystemZGenAsmWriter.inc"
70 /// createSystemZCodePrinterPass - Returns a pass that prints the SystemZ
71 /// assembly code for a MachineFunction to the given output stream,
72 /// using the given target machine description. This should work
73 /// regardless of whether the function is in SSA form.
75 FunctionPass *llvm::createSystemZCodePrinterPass(raw_ostream &o,
76 SystemZTargetMachine &tm,
77 CodeGenOpt::Level OptLevel,
79 return new SystemZAsmPrinter(o, tm, tm.getTargetAsmInfo(), OptLevel, verbose);
82 bool SystemZAsmPrinter::doInitialization(Module &M) {
83 Mang = new Mangler(M, "", TAI->getPrivateGlobalPrefix());
84 return false; // success
88 bool SystemZAsmPrinter::doFinalization(Module &M) {
89 return AsmPrinter::doFinalization(M);
92 void SystemZAsmPrinter::emitFunctionHeader(const MachineFunction &MF) {
93 const Function *F = MF.getFunction();
95 SwitchToSection(TAI->SectionForGlobal(F));
98 if (F->hasFnAttr(Attribute::OptimizeForSize))
101 EmitAlignment(FnAlign, F);
103 switch (F->getLinkage()) {
104 default: assert(0 && "Unknown linkage type!");
105 case Function::InternalLinkage: // Symbols default to internal.
106 case Function::PrivateLinkage:
108 case Function::ExternalLinkage:
109 O << "\t.globl\t" << CurrentFnName << '\n';
111 case Function::LinkOnceAnyLinkage:
112 case Function::LinkOnceODRLinkage:
113 case Function::WeakAnyLinkage:
114 case Function::WeakODRLinkage:
115 O << "\t.weak\t" << CurrentFnName << '\n';
119 printVisibility(CurrentFnName, F->getVisibility());
121 O << "\t.type\t" << CurrentFnName << ",@function\n"
122 << CurrentFnName << ":\n";
125 bool SystemZAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
126 SetupMachineFunction(MF);
129 // Print the 'header' of function
130 emitFunctionHeader(MF);
132 // Print out code for the function.
133 for (MachineFunction::const_iterator I = MF.begin(), E = MF.end();
135 // Print a label for the basic block.
136 if (!VerboseAsm && (I->pred_empty() || I->isOnlyReachableByFallthrough())) {
137 // This is an entry block or a block that's only reachable via a
138 // fallthrough edge. In non-VerboseAsm mode, don't print the label.
140 printBasicBlockLabel(I, true, true, VerboseAsm);
144 for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end();
146 // Print the assembly for the instruction.
147 printMachineInstruction(II);
150 if (TAI->hasDotTypeDotSizeDirective())
151 O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n';
155 // We didn't modify anything
159 void SystemZAsmPrinter::printMachineInstruction(const MachineInstr *MI) {
162 // Call the autogenerated instruction printer routines.
163 if (printInstruction(MI))
166 assert(0 && "Should not happen");
169 void SystemZAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
170 const char* Modifier) {
171 const MachineOperand &MO = MI->getOperand(OpNum);
172 switch (MO.getType()) {
173 case MachineOperand::MO_Register:
174 assert (TargetRegisterInfo::isPhysicalRegister(MO.getReg()) &&
175 "Virtual registers should be already mapped!");
176 O << '%' << TM.getRegisterInfo()->get(MO.getReg()).AsmName;
178 case MachineOperand::MO_Immediate:
181 case MachineOperand::MO_MachineBasicBlock:
182 printBasicBlockLabel(MO.getMBB());
185 assert(0 && "Not implemented yet!");