Anton Korobeynikov | 32b7d5b | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 1 | //===-- SystemZAsmPrinter.cpp - SystemZ LLVM assembly writer ---------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains a printer that converts from our internal representation |
| 11 | // of machine-dependent LLVM code to the SystemZ assembly language. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #define DEBUG_TYPE "asm-printer" |
| 16 | #include "SystemZ.h" |
| 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" |
| 34 | |
| 35 | using namespace llvm; |
| 36 | |
| 37 | STATISTIC(EmittedInsts, "Number of machine instrs printed"); |
| 38 | |
| 39 | namespace { |
| 40 | class VISIBILITY_HIDDEN SystemZAsmPrinter : public AsmPrinter { |
| 41 | public: |
| 42 | SystemZAsmPrinter(raw_ostream &O, SystemZTargetMachine &TM, |
| 43 | const TargetAsmInfo *TAI, |
| 44 | CodeGenOpt::Level OL, bool V) |
| 45 | : AsmPrinter(O, TM, TAI, OL, V) {} |
| 46 | |
| 47 | virtual const char *getPassName() const { |
| 48 | return "SystemZ Assembly Printer"; |
| 49 | } |
| 50 | |
| 51 | void printOperand(const MachineInstr *MI, int OpNum, |
| 52 | const char* Modifier = 0); |
Anton Korobeynikov | a58fac9 | 2009-07-16 13:43:18 +0000 | [diff] [blame] | 53 | void printRIAddrOperand(const MachineInstr *MI, int OpNum, |
| 54 | const char* Modifier = 0); |
Anton Korobeynikov | 87b83aa | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 55 | void printRRIAddrOperand(const MachineInstr *MI, int OpNum, |
| 56 | const char* Modifier = 0); |
Anton Korobeynikov | 32b7d5b | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 57 | bool printInstruction(const MachineInstr *MI); // autogenerated. |
| 58 | void printMachineInstruction(const MachineInstr * MI); |
| 59 | |
| 60 | void emitFunctionHeader(const MachineFunction &MF); |
| 61 | bool runOnMachineFunction(MachineFunction &F); |
| 62 | bool doInitialization(Module &M); |
| 63 | bool doFinalization(Module &M); |
| 64 | |
| 65 | void getAnalysisUsage(AnalysisUsage &AU) const { |
| 66 | AsmPrinter::getAnalysisUsage(AU); |
| 67 | AU.setPreservesAll(); |
| 68 | } |
| 69 | }; |
| 70 | } // end of anonymous namespace |
| 71 | |
| 72 | #include "SystemZGenAsmWriter.inc" |
| 73 | |
| 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. |
| 78 | /// |
| 79 | FunctionPass *llvm::createSystemZCodePrinterPass(raw_ostream &o, |
| 80 | SystemZTargetMachine &tm, |
| 81 | CodeGenOpt::Level OptLevel, |
| 82 | bool verbose) { |
| 83 | return new SystemZAsmPrinter(o, tm, tm.getTargetAsmInfo(), OptLevel, verbose); |
| 84 | } |
| 85 | |
| 86 | bool SystemZAsmPrinter::doInitialization(Module &M) { |
| 87 | Mang = new Mangler(M, "", TAI->getPrivateGlobalPrefix()); |
| 88 | return false; // success |
| 89 | } |
| 90 | |
| 91 | |
| 92 | bool SystemZAsmPrinter::doFinalization(Module &M) { |
| 93 | return AsmPrinter::doFinalization(M); |
| 94 | } |
| 95 | |
| 96 | void SystemZAsmPrinter::emitFunctionHeader(const MachineFunction &MF) { |
| 97 | const Function *F = MF.getFunction(); |
| 98 | |
| 99 | SwitchToSection(TAI->SectionForGlobal(F)); |
| 100 | |
| 101 | unsigned FnAlign = 4; |
| 102 | if (F->hasFnAttr(Attribute::OptimizeForSize)) |
| 103 | FnAlign = 1; |
| 104 | |
| 105 | EmitAlignment(FnAlign, F); |
| 106 | |
| 107 | switch (F->getLinkage()) { |
| 108 | default: assert(0 && "Unknown linkage type!"); |
| 109 | case Function::InternalLinkage: // Symbols default to internal. |
| 110 | case Function::PrivateLinkage: |
| 111 | break; |
| 112 | case Function::ExternalLinkage: |
| 113 | O << "\t.globl\t" << CurrentFnName << '\n'; |
| 114 | break; |
| 115 | case Function::LinkOnceAnyLinkage: |
| 116 | case Function::LinkOnceODRLinkage: |
| 117 | case Function::WeakAnyLinkage: |
| 118 | case Function::WeakODRLinkage: |
| 119 | O << "\t.weak\t" << CurrentFnName << '\n'; |
| 120 | break; |
| 121 | } |
| 122 | |
| 123 | printVisibility(CurrentFnName, F->getVisibility()); |
| 124 | |
| 125 | O << "\t.type\t" << CurrentFnName << ",@function\n" |
| 126 | << CurrentFnName << ":\n"; |
| 127 | } |
| 128 | |
| 129 | bool SystemZAsmPrinter::runOnMachineFunction(MachineFunction &MF) { |
| 130 | SetupMachineFunction(MF); |
| 131 | O << "\n\n"; |
| 132 | |
| 133 | // Print the 'header' of function |
| 134 | emitFunctionHeader(MF); |
| 135 | |
| 136 | // Print out code for the function. |
| 137 | for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); |
| 138 | I != E; ++I) { |
| 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. |
| 143 | } else { |
| 144 | printBasicBlockLabel(I, true, true, VerboseAsm); |
| 145 | O << '\n'; |
| 146 | } |
| 147 | |
| 148 | for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end(); |
| 149 | II != E; ++II) |
| 150 | // Print the assembly for the instruction. |
| 151 | printMachineInstruction(II); |
| 152 | } |
| 153 | |
| 154 | if (TAI->hasDotTypeDotSizeDirective()) |
| 155 | O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n'; |
| 156 | |
| 157 | O.flush(); |
| 158 | |
| 159 | // We didn't modify anything |
| 160 | return false; |
| 161 | } |
| 162 | |
| 163 | void SystemZAsmPrinter::printMachineInstruction(const MachineInstr *MI) { |
| 164 | ++EmittedInsts; |
| 165 | |
| 166 | // Call the autogenerated instruction printer routines. |
| 167 | if (printInstruction(MI)) |
| 168 | return; |
| 169 | |
| 170 | assert(0 && "Should not happen"); |
| 171 | } |
| 172 | |
| 173 | void SystemZAsmPrinter::printOperand(const MachineInstr *MI, int OpNum, |
Anton Korobeynikov | a58fac9 | 2009-07-16 13:43:18 +0000 | [diff] [blame] | 174 | const char* Modifier) { |
Anton Korobeynikov | f259c6c | 2009-07-16 13:29:38 +0000 | [diff] [blame] | 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; |
| 181 | return; |
| 182 | case MachineOperand::MO_Immediate: |
| 183 | O << MO.getImm(); |
| 184 | return; |
| 185 | case MachineOperand::MO_MachineBasicBlock: |
| 186 | printBasicBlockLabel(MO.getMBB()); |
| 187 | return; |
| 188 | default: |
| 189 | assert(0 && "Not implemented yet!"); |
| 190 | } |
Anton Korobeynikov | 32b7d5b | 2009-07-16 13:27:25 +0000 | [diff] [blame] | 191 | } |
Anton Korobeynikov | a58fac9 | 2009-07-16 13:43:18 +0000 | [diff] [blame] | 192 | |
| 193 | void SystemZAsmPrinter::printRIAddrOperand(const MachineInstr *MI, int OpNum, |
| 194 | const char* Modifier) { |
| 195 | const MachineOperand &Base = MI->getOperand(OpNum); |
| 196 | |
| 197 | // Print displacement operand. |
| 198 | printOperand(MI, OpNum+1); |
| 199 | |
| 200 | // Print base operand (if any) |
Anton Korobeynikov | 87b83aa | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 201 | if (Base.getReg()) { |
Anton Korobeynikov | a58fac9 | 2009-07-16 13:43:18 +0000 | [diff] [blame] | 202 | O << '('; |
| 203 | printOperand(MI, OpNum); |
| 204 | O << ')'; |
| 205 | } |
| 206 | } |
| 207 | |
Anton Korobeynikov | 87b83aa | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 208 | void SystemZAsmPrinter::printRRIAddrOperand(const MachineInstr *MI, int OpNum, |
| 209 | const char* Modifier) { |
| 210 | const MachineOperand &Base = MI->getOperand(OpNum); |
Anton Korobeynikov | 9773ff8 | 2009-07-16 13:45:22 +0000 | [diff] [blame] | 211 | const MachineOperand &Index = MI->getOperand(OpNum+1); |
Anton Korobeynikov | 87b83aa | 2009-07-16 13:44:00 +0000 | [diff] [blame] | 212 | |
| 213 | // Print displacement operand. |
| 214 | printOperand(MI, OpNum+2); |
| 215 | |
| 216 | // Print base operand (if any) |
| 217 | if (Base.getReg()) { |
| 218 | O << '('; |
| 219 | printOperand(MI, OpNum); |
| 220 | if (Index.getReg()) { |
| 221 | O << ','; |
| 222 | printOperand(MI, OpNum+1); |
| 223 | } |
| 224 | O << ')'; |
| 225 | } else |
| 226 | assert(!Index.getReg() && "Should allocate base register first!"); |
| 227 | } |
| 228 | |