Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame^] | 1 | //===-- MipsAsmPrinter.cpp - Mips LLVM assembly writer --------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Bruno Cardoso Lopes and is distributed under the |
| 6 | // University of Illinois Open Source 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 GAS-format MIPS assembly language. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #define DEBUG_TYPE "mips-asm-printer" |
| 16 | |
| 17 | #include "Mips.h" |
| 18 | #include "MipsInstrInfo.h" |
| 19 | #include "MipsTargetMachine.h" |
| 20 | #include "llvm/Constants.h" |
| 21 | #include "llvm/DerivedTypes.h" |
| 22 | #include "llvm/Module.h" |
| 23 | #include "llvm/CodeGen/AsmPrinter.h" |
| 24 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 25 | #include "llvm/CodeGen/MachineConstantPool.h" |
| 26 | #include "llvm/CodeGen/MachineInstr.h" |
| 27 | #include "llvm/Target/TargetAsmInfo.h" |
| 28 | #include "llvm/Target/TargetData.h" |
| 29 | #include "llvm/Target/TargetMachine.h" |
| 30 | #include "llvm/Support/Mangler.h" |
| 31 | #include "llvm/ADT/Statistic.h" |
| 32 | #include "llvm/ADT/StringExtras.h" |
| 33 | #include "llvm/Support/CommandLine.h" |
| 34 | #include "llvm/Support/MathExtras.h" |
| 35 | #include <cctype> |
| 36 | |
| 37 | using namespace llvm; |
| 38 | |
| 39 | STATISTIC(EmittedInsts, "Number of machine instrs printed"); |
| 40 | |
| 41 | namespace { |
| 42 | struct VISIBILITY_HIDDEN MipsAsmPrinter : public AsmPrinter { |
| 43 | MipsAsmPrinter(std::ostream &O, MipsTargetMachine &TM, |
| 44 | const TargetAsmInfo *T): |
| 45 | AsmPrinter(O, TM, T) {} |
| 46 | |
| 47 | virtual const char *getPassName() const { |
| 48 | return "Mips Assembly Printer"; |
| 49 | } |
| 50 | |
| 51 | void printOperand(const MachineInstr *MI, int opNum); |
| 52 | void printMemOperand(const MachineInstr *MI, int opNum, |
| 53 | const char *Modifier = 0); |
| 54 | |
| 55 | bool printInstruction(const MachineInstr *MI); // autogenerated. |
| 56 | bool runOnMachineFunction(MachineFunction &F); |
| 57 | bool doInitialization(Module &M); |
| 58 | bool doFinalization(Module &M); |
| 59 | }; |
| 60 | } // end of anonymous namespace |
| 61 | |
| 62 | #include "MipsGenAsmWriter.inc" |
| 63 | |
| 64 | /// createMipsCodePrinterPass - Returns a pass that prints the MIPS |
| 65 | /// assembly code for a MachineFunction to the given output stream, |
| 66 | /// using the given target machine description. This should work |
| 67 | /// regardless of whether the function is in SSA form. |
| 68 | FunctionPass *llvm::createMipsCodePrinterPass(std::ostream &o, |
| 69 | MipsTargetMachine &tm) |
| 70 | { |
| 71 | return new MipsAsmPrinter(o, tm, tm.getTargetAsmInfo()); |
| 72 | } |
| 73 | |
| 74 | /// runOnMachineFunction - This uses the printMachineInstruction() |
| 75 | /// method to print assembly for each instruction. |
| 76 | bool MipsAsmPrinter:: |
| 77 | runOnMachineFunction(MachineFunction &MF) |
| 78 | { |
| 79 | SetupMachineFunction(MF); |
| 80 | |
| 81 | // Print out constants referenced by the function |
| 82 | EmitConstantPool(MF.getConstantPool()); |
| 83 | |
| 84 | O << "\n\n"; |
| 85 | |
| 86 | // What's my mangled name? |
| 87 | CurrentFnName = Mang->getValueName(MF.getFunction()); |
| 88 | |
| 89 | // Print out the label for the function. |
| 90 | const Function *F = MF.getFunction(); |
| 91 | SwitchToTextSection(getSectionForFunction(*F).c_str(), F); |
| 92 | |
| 93 | // On Mips GAS if .align #n is present, #n means the number of bits |
| 94 | // to be cleared to align. So, if we want 4 byte alignment, we must |
| 95 | // have .align 2 |
| 96 | // TODO: |
| 97 | // add gas ".mask" and ".fmask" |
| 98 | EmitAlignment(1, F); |
| 99 | O << "\t.globl\t" << CurrentFnName << "\n"; |
| 100 | O << "\t.ent\t" << CurrentFnName << "\n"; |
| 101 | O << "\t.type\t" << CurrentFnName << ", @function\n"; |
| 102 | O << CurrentFnName << ":\n"; |
| 103 | |
| 104 | // Print out code for the function. |
| 105 | for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); |
| 106 | I != E; ++I) { |
| 107 | |
| 108 | // Print a label for the basic block. |
| 109 | if (I != MF.begin()) { |
| 110 | printBasicBlockLabel(I, true); |
| 111 | O << '\n'; |
| 112 | } |
| 113 | |
| 114 | for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end(); |
| 115 | II != E; ++II) { |
| 116 | // Print the assembly for the instruction. |
| 117 | O << "\t"; |
| 118 | printInstruction(II); |
| 119 | ++EmittedInsts; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | // close function with asm directive |
| 124 | O << "\t.end\t" << CurrentFnName << "\n"; |
| 125 | |
| 126 | // We didn't modify anything. |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | void MipsAsmPrinter:: |
| 131 | printOperand(const MachineInstr *MI, int opNum) |
| 132 | { |
| 133 | const MachineOperand &MO = MI->getOperand(opNum); |
| 134 | const MRegisterInfo &RI = *TM.getRegisterInfo(); |
| 135 | bool closeP=false; |
| 136 | |
| 137 | // %hi and %lo used on mips gas to break large constants |
| 138 | if (MI->getOpcode() == Mips::LUi && !MO.isRegister() |
| 139 | && !MO.isImmediate()) { |
| 140 | O << "%hi("; |
| 141 | closeP = true; |
| 142 | } else if ((MI->getOpcode() == Mips::ADDiu) && !MO.isRegister() |
| 143 | && !MO.isImmediate()) { |
| 144 | O << "%lo("; |
| 145 | closeP = true; |
| 146 | } |
| 147 | |
| 148 | switch (MO.getType()) |
| 149 | { |
| 150 | case MachineOperand::MO_Register: |
| 151 | if (MRegisterInfo::isPhysicalRegister(MO.getReg())) |
| 152 | O << "$" << LowercaseString (RI.get(MO.getReg()).Name); |
| 153 | else |
| 154 | O << "$" << MO.getReg(); |
| 155 | break; |
| 156 | |
| 157 | case MachineOperand::MO_Immediate: |
| 158 | if ((MI->getOpcode() == Mips::SLTiu) || (MI->getOpcode() == Mips::ORi) || |
| 159 | (MI->getOpcode() == Mips::LUi) || (MI->getOpcode() == Mips::ANDi)) |
| 160 | O << (unsigned int)MO.getImmedValue(); |
| 161 | else |
| 162 | O << (int)MO.getImmedValue(); |
| 163 | break; |
| 164 | |
| 165 | case MachineOperand::MO_MachineBasicBlock: |
| 166 | printBasicBlockLabel(MO.getMachineBasicBlock()); |
| 167 | return; |
| 168 | |
| 169 | case MachineOperand::MO_GlobalAddress: |
| 170 | O << Mang->getValueName(MO.getGlobal()); |
| 171 | break; |
| 172 | |
| 173 | case MachineOperand::MO_ExternalSymbol: |
| 174 | O << MO.getSymbolName(); |
| 175 | break; |
| 176 | |
| 177 | case MachineOperand::MO_ConstantPoolIndex: |
| 178 | O << TAI->getPrivateGlobalPrefix() << "CPI" |
| 179 | << getFunctionNumber() << "_" << MO.getConstantPoolIndex(); |
| 180 | break; |
| 181 | |
| 182 | default: |
| 183 | O << "<unknown operand type>"; abort (); break; |
| 184 | } |
| 185 | |
| 186 | if (closeP) O << ")"; |
| 187 | } |
| 188 | |
| 189 | void MipsAsmPrinter:: |
| 190 | printMemOperand(const MachineInstr *MI, int opNum, const char *Modifier) |
| 191 | { |
| 192 | // lw/sw $reg, MemOperand |
| 193 | // will turn into : |
| 194 | // lw/sw $reg, imm($reg) |
| 195 | printOperand(MI, opNum); |
| 196 | O << "("; |
| 197 | printOperand(MI, opNum+1); |
| 198 | O << ")"; |
| 199 | } |
| 200 | |
| 201 | bool MipsAsmPrinter:: |
| 202 | doInitialization(Module &M) |
| 203 | { |
| 204 | Mang = new Mangler(M); |
| 205 | return false; // success |
| 206 | } |
| 207 | |
| 208 | bool MipsAsmPrinter:: |
| 209 | doFinalization(Module &M) |
| 210 | { |
| 211 | const TargetData *TD = TM.getTargetData(); |
| 212 | |
| 213 | // Print out module-level global variables here. |
| 214 | for (Module::const_global_iterator I = M.global_begin(), |
| 215 | E = M.global_end(); I != E; ++I) |
| 216 | |
| 217 | // External global require no code |
| 218 | if (I->hasInitializer()) { |
| 219 | |
| 220 | // Check to see if this is a special global |
| 221 | // used by LLVM, if so, emit it. |
| 222 | if (EmitSpecialLLVMGlobal(I)) |
| 223 | continue; |
| 224 | |
| 225 | O << "\n\n"; |
| 226 | std::string name = Mang->getValueName(I); |
| 227 | Constant *C = I->getInitializer(); |
| 228 | unsigned Size = TD->getTypeSize(C->getType()); |
| 229 | unsigned Align = TD->getPrefTypeAlignment(C->getType()); |
| 230 | |
| 231 | if (C->isNullValue() && (I->hasLinkOnceLinkage() || |
| 232 | I->hasInternalLinkage() || I->hasWeakLinkage() |
| 233 | /* FIXME: Verify correct */)) { |
| 234 | |
| 235 | SwitchToDataSection(".data", I); |
| 236 | if (I->hasInternalLinkage()) |
| 237 | O << "\t.local " << name << "\n"; |
| 238 | |
| 239 | O << "\t.comm " << name << "," |
| 240 | << TD->getTypeSize(C->getType()) |
| 241 | << "," << Align << "\n"; |
| 242 | |
| 243 | } else { |
| 244 | |
| 245 | switch (I->getLinkage()) |
| 246 | { |
| 247 | case GlobalValue::LinkOnceLinkage: |
| 248 | case GlobalValue::WeakLinkage: |
| 249 | // FIXME: Verify correct for weak. |
| 250 | // Nonnull linkonce -> weak |
| 251 | O << "\t.weak " << name << "\n"; |
| 252 | SwitchToDataSection("", I); |
| 253 | O << "\t.section\t\".llvm.linkonce.d." << name |
| 254 | << "\",\"aw\",@progbits\n"; |
| 255 | break; |
| 256 | case GlobalValue::AppendingLinkage: |
| 257 | // FIXME: appending linkage variables |
| 258 | // should go into a section of their name or |
| 259 | // something. For now, just emit them as external. |
| 260 | case GlobalValue::ExternalLinkage: |
| 261 | // If external or appending, declare as a global symbol |
| 262 | O << "\t.globl " << name << "\n"; |
| 263 | case GlobalValue::InternalLinkage: |
| 264 | if (C->isNullValue()) |
| 265 | SwitchToDataSection(".bss", I); |
| 266 | else |
| 267 | SwitchToDataSection(".data", I); |
| 268 | break; |
| 269 | case GlobalValue::GhostLinkage: |
| 270 | cerr << "Should not have any" |
| 271 | << "unmaterialized functions!\n"; |
| 272 | abort(); |
| 273 | case GlobalValue::DLLImportLinkage: |
| 274 | cerr << "DLLImport linkage is" |
| 275 | << "not supported by this target!\n"; |
| 276 | abort(); |
| 277 | case GlobalValue::DLLExportLinkage: |
| 278 | cerr << "DLLExport linkage is" |
| 279 | << "not supported by this target!\n"; |
| 280 | abort(); |
| 281 | default: |
| 282 | assert(0 && "Unknown linkage type!"); |
| 283 | } |
| 284 | O << "\t.align " << Align << "\n"; |
| 285 | O << "\t.type " << name << ",@object\n"; |
| 286 | O << "\t.size " << name << "," << Size << "\n"; |
| 287 | O << name << ":\n"; |
| 288 | EmitGlobalConstant(C); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | AsmPrinter::doFinalization(M); |
| 293 | return false; // success |
| 294 | } |