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 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 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" |
Bruno Cardoso Lopes | 43d526d | 2008-07-14 14:42:54 +0000 | [diff] [blame] | 18 | #include "MipsSubtarget.h" |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 19 | #include "MipsInstrInfo.h" |
| 20 | #include "MipsTargetMachine.h" |
Bruno Cardoso Lopes | a4e8200 | 2007-07-11 23:24:41 +0000 | [diff] [blame] | 21 | #include "MipsMachineFunction.h" |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 22 | #include "llvm/Constants.h" |
| 23 | #include "llvm/DerivedTypes.h" |
| 24 | #include "llvm/Module.h" |
| 25 | #include "llvm/CodeGen/AsmPrinter.h" |
| 26 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 27 | #include "llvm/CodeGen/MachineConstantPool.h" |
Bruno Cardoso Lopes | a4e8200 | 2007-07-11 23:24:41 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/MachineInstr.h" |
| 30 | #include "llvm/Target/TargetAsmInfo.h" |
| 31 | #include "llvm/Target/TargetData.h" |
| 32 | #include "llvm/Target/TargetMachine.h" |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 33 | #include "llvm/Target/TargetOptions.h" |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Mangler.h" |
| 35 | #include "llvm/ADT/Statistic.h" |
| 36 | #include "llvm/ADT/StringExtras.h" |
Bruno Cardoso Lopes | a4e8200 | 2007-07-11 23:24:41 +0000 | [diff] [blame] | 37 | #include "llvm/Support/Debug.h" |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 38 | #include "llvm/Support/CommandLine.h" |
| 39 | #include "llvm/Support/MathExtras.h" |
| 40 | #include <cctype> |
| 41 | |
| 42 | using namespace llvm; |
| 43 | |
| 44 | STATISTIC(EmittedInsts, "Number of machine instrs printed"); |
| 45 | |
| 46 | namespace { |
| 47 | struct VISIBILITY_HIDDEN MipsAsmPrinter : public AsmPrinter { |
Bruno Cardoso Lopes | 43d526d | 2008-07-14 14:42:54 +0000 | [diff] [blame] | 48 | |
| 49 | const MipsSubtarget *Subtarget; |
| 50 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 51 | MipsAsmPrinter(std::ostream &O, MipsTargetMachine &TM, |
| 52 | const TargetAsmInfo *T): |
Bruno Cardoso Lopes | 43d526d | 2008-07-14 14:42:54 +0000 | [diff] [blame] | 53 | AsmPrinter(O, TM, T) { |
| 54 | Subtarget = &TM.getSubtarget<MipsSubtarget>(); |
| 55 | } |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 56 | |
| 57 | virtual const char *getPassName() const { |
| 58 | return "Mips Assembly Printer"; |
| 59 | } |
| 60 | |
Anton Korobeynikov | ae408e6 | 2008-07-19 13:16:11 +0000 | [diff] [blame] | 61 | virtual std::string getSectionForFunction(const Function &F) const; |
Bruno Cardoso Lopes | 91ef849 | 2008-08-02 19:42:36 +0000 | [diff] [blame^] | 62 | bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, |
| 63 | unsigned AsmVariant, const char *ExtraCode); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 64 | void printOperand(const MachineInstr *MI, int opNum); |
| 65 | void printMemOperand(const MachineInstr *MI, int opNum, |
| 66 | const char *Modifier = 0); |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 67 | void printFCCOperand(const MachineInstr *MI, int opNum, |
| 68 | const char *Modifier = 0); |
Anton Korobeynikov | ae408e6 | 2008-07-19 13:16:11 +0000 | [diff] [blame] | 69 | void printModuleLevelGV(const GlobalVariable* GVar); |
Bruno Cardoso Lopes | dc0c04c | 2007-08-28 05:06:17 +0000 | [diff] [blame] | 70 | unsigned int getSavedRegsBitmask(bool isFloat, MachineFunction &MF); |
Bruno Cardoso Lopes | a4e8200 | 2007-07-11 23:24:41 +0000 | [diff] [blame] | 71 | void printHex32(unsigned int Value); |
Bruno Cardoso Lopes | dc0c04c | 2007-08-28 05:06:17 +0000 | [diff] [blame] | 72 | |
Bruno Cardoso Lopes | 43d526d | 2008-07-14 14:42:54 +0000 | [diff] [blame] | 73 | const char *emitCurrentABIString(void); |
Bruno Cardoso Lopes | a4e8200 | 2007-07-11 23:24:41 +0000 | [diff] [blame] | 74 | void emitFunctionStart(MachineFunction &MF); |
Bruno Cardoso Lopes | 0a60400 | 2007-10-09 03:01:19 +0000 | [diff] [blame] | 75 | void emitFunctionEnd(MachineFunction &MF); |
Bruno Cardoso Lopes | a4e8200 | 2007-07-11 23:24:41 +0000 | [diff] [blame] | 76 | void emitFrameDirective(MachineFunction &MF); |
| 77 | void emitMaskDirective(MachineFunction &MF); |
Bruno Cardoso Lopes | dc0c04c | 2007-08-28 05:06:17 +0000 | [diff] [blame] | 78 | void emitFMaskDirective(MachineFunction &MF); |
Anton Korobeynikov | ae408e6 | 2008-07-19 13:16:11 +0000 | [diff] [blame] | 79 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 80 | bool printInstruction(const MachineInstr *MI); // autogenerated. |
| 81 | bool runOnMachineFunction(MachineFunction &F); |
| 82 | bool doInitialization(Module &M); |
| 83 | bool doFinalization(Module &M); |
| 84 | }; |
| 85 | } // end of anonymous namespace |
| 86 | |
| 87 | #include "MipsGenAsmWriter.inc" |
| 88 | |
| 89 | /// createMipsCodePrinterPass - Returns a pass that prints the MIPS |
| 90 | /// assembly code for a MachineFunction to the given output stream, |
| 91 | /// using the given target machine description. This should work |
| 92 | /// regardless of whether the function is in SSA form. |
| 93 | FunctionPass *llvm::createMipsCodePrinterPass(std::ostream &o, |
| 94 | MipsTargetMachine &tm) |
| 95 | { |
| 96 | return new MipsAsmPrinter(o, tm, tm.getTargetAsmInfo()); |
| 97 | } |
| 98 | |
Bruno Cardoso Lopes | dc0c04c | 2007-08-28 05:06:17 +0000 | [diff] [blame] | 99 | //===----------------------------------------------------------------------===// |
| 100 | // |
| 101 | // Mips Asm Directives |
| 102 | // |
| 103 | // -- Frame directive "frame Stackpointer, Stacksize, RARegister" |
| 104 | // Describe the stack frame. |
| 105 | // |
| 106 | // -- Mask directives "(f)mask bitmask, offset" |
| 107 | // Tells the assembler which registers are saved and where. |
| 108 | // bitmask - contain a little endian bitset indicating which registers are |
| 109 | // saved on function prologue (e.g. with a 0x80000000 mask, the |
| 110 | // assembler knows the register 31 (RA) is saved at prologue. |
| 111 | // offset - the position before stack pointer subtraction indicating where |
| 112 | // the first saved register on prologue is located. (e.g. with a |
| 113 | // |
| 114 | // Consider the following function prologue: |
| 115 | // |
Bill Wendling | 6ef781f | 2008-02-27 06:33:05 +0000 | [diff] [blame] | 116 | // .frame $fp,48,$ra |
| 117 | // .mask 0xc0000000,-8 |
| 118 | // addiu $sp, $sp, -48 |
| 119 | // sw $ra, 40($sp) |
| 120 | // sw $fp, 36($sp) |
Bruno Cardoso Lopes | dc0c04c | 2007-08-28 05:06:17 +0000 | [diff] [blame] | 121 | // |
| 122 | // With a 0xc0000000 mask, the assembler knows the register 31 (RA) and |
| 123 | // 30 (FP) are saved at prologue. As the save order on prologue is from |
| 124 | // left to right, RA is saved first. A -8 offset means that after the |
| 125 | // stack pointer subtration, the first register in the mask (RA) will be |
| 126 | // saved at address 48-8=40. |
| 127 | // |
| 128 | //===----------------------------------------------------------------------===// |
| 129 | |
Bruno Cardoso Lopes | 43d526d | 2008-07-14 14:42:54 +0000 | [diff] [blame] | 130 | //===----------------------------------------------------------------------===// |
| 131 | // Mask directives |
| 132 | //===----------------------------------------------------------------------===// |
| 133 | |
Bruno Cardoso Lopes | dc0c04c | 2007-08-28 05:06:17 +0000 | [diff] [blame] | 134 | /// Mask directive for GPR |
| 135 | void MipsAsmPrinter:: |
| 136 | emitMaskDirective(MachineFunction &MF) |
| 137 | { |
| 138 | MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>(); |
| 139 | |
| 140 | int StackSize = MF.getFrameInfo()->getStackSize(); |
| 141 | int Offset = (!MipsFI->getTopSavedRegOffset()) ? 0 : |
| 142 | (-(StackSize-MipsFI->getTopSavedRegOffset())); |
Anton Korobeynikov | 055a76b | 2008-07-19 13:16:32 +0000 | [diff] [blame] | 143 | |
Bruno Cardoso Lopes | dc0c04c | 2007-08-28 05:06:17 +0000 | [diff] [blame] | 144 | #ifndef NDEBUG |
Anton Korobeynikov | 055a76b | 2008-07-19 13:16:32 +0000 | [diff] [blame] | 145 | DOUT << "--> emitMaskDirective" << '\n'; |
| 146 | DOUT << "StackSize : " << StackSize << '\n'; |
| 147 | DOUT << "getTopSavedReg : " << MipsFI->getTopSavedRegOffset() << '\n'; |
Bruno Cardoso Lopes | dc0c04c | 2007-08-28 05:06:17 +0000 | [diff] [blame] | 148 | DOUT << "Offset : " << Offset << "\n\n"; |
| 149 | #endif |
| 150 | |
| 151 | unsigned int Bitmask = getSavedRegsBitmask(false, MF); |
Bruno Cardoso Lopes | b42abeb | 2007-09-24 20:15:11 +0000 | [diff] [blame] | 152 | O << "\t.mask \t"; |
Bruno Cardoso Lopes | dc0c04c | 2007-08-28 05:06:17 +0000 | [diff] [blame] | 153 | printHex32(Bitmask); |
Anton Korobeynikov | 055a76b | 2008-07-19 13:16:32 +0000 | [diff] [blame] | 154 | O << ',' << Offset << '\n'; |
Bruno Cardoso Lopes | dc0c04c | 2007-08-28 05:06:17 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Bruno Cardoso Lopes | 7b76da1 | 2008-07-09 04:45:36 +0000 | [diff] [blame] | 157 | /// TODO: Mask Directive for Floating Point |
Bruno Cardoso Lopes | dc0c04c | 2007-08-28 05:06:17 +0000 | [diff] [blame] | 158 | void MipsAsmPrinter:: |
| 159 | emitFMaskDirective(MachineFunction &MF) |
| 160 | { |
| 161 | unsigned int Bitmask = getSavedRegsBitmask(true, MF); |
| 162 | |
| 163 | O << "\t.fmask\t"; |
| 164 | printHex32(Bitmask); |
Anton Korobeynikov | 055a76b | 2008-07-19 13:16:32 +0000 | [diff] [blame] | 165 | O << ",0" << '\n'; |
Bruno Cardoso Lopes | dc0c04c | 2007-08-28 05:06:17 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Bruno Cardoso Lopes | dc0c04c | 2007-08-28 05:06:17 +0000 | [diff] [blame] | 168 | // Create a bitmask with all callee saved registers for CPU |
Bruno Cardoso Lopes | 7b76da1 | 2008-07-09 04:45:36 +0000 | [diff] [blame] | 169 | // or Floating Point registers. For CPU registers consider RA, |
Bruno Cardoso Lopes | dc0c04c | 2007-08-28 05:06:17 +0000 | [diff] [blame] | 170 | // GP and FP for saving if necessary. |
| 171 | unsigned int MipsAsmPrinter:: |
| 172 | getSavedRegsBitmask(bool isFloat, MachineFunction &MF) |
| 173 | { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 174 | const TargetRegisterInfo &RI = *TM.getRegisterInfo(); |
Bruno Cardoso Lopes | dc0c04c | 2007-08-28 05:06:17 +0000 | [diff] [blame] | 175 | |
Bruno Cardoso Lopes | 7b76da1 | 2008-07-09 04:45:36 +0000 | [diff] [blame] | 176 | // Floating Point Registers, TODO |
Bruno Cardoso Lopes | dc0c04c | 2007-08-28 05:06:17 +0000 | [diff] [blame] | 177 | if (isFloat) |
| 178 | return 0; |
| 179 | |
| 180 | // CPU Registers |
| 181 | unsigned int Bitmask = 0; |
| 182 | |
| 183 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 184 | const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo(); |
| 185 | for (unsigned i = 0, e = CSI.size(); i != e; ++i) |
| 186 | Bitmask |= (1 << MipsRegisterInfo::getRegisterNumbering(CSI[i].getReg())); |
| 187 | |
| 188 | if (RI.hasFP(MF)) |
Bruno Cardoso Lopes | 0a60400 | 2007-10-09 03:01:19 +0000 | [diff] [blame] | 189 | Bitmask |= (1 << MipsRegisterInfo:: |
| 190 | getRegisterNumbering(RI.getFrameRegister(MF))); |
Bruno Cardoso Lopes | dc0c04c | 2007-08-28 05:06:17 +0000 | [diff] [blame] | 191 | |
| 192 | if (MF.getFrameInfo()->hasCalls()) |
Bruno Cardoso Lopes | 0a60400 | 2007-10-09 03:01:19 +0000 | [diff] [blame] | 193 | Bitmask |= (1 << MipsRegisterInfo:: |
| 194 | getRegisterNumbering(RI.getRARegister())); |
Bruno Cardoso Lopes | dc0c04c | 2007-08-28 05:06:17 +0000 | [diff] [blame] | 195 | |
| 196 | return Bitmask; |
| 197 | } |
| 198 | |
| 199 | // Print a 32 bit hex number with all numbers. |
| 200 | void MipsAsmPrinter:: |
| 201 | printHex32(unsigned int Value) |
| 202 | { |
| 203 | O << "0x" << std::hex; |
| 204 | for (int i = 7; i >= 0; i--) |
| 205 | O << std::hex << ( (Value & (0xF << (i*4))) >> (i*4) ); |
| 206 | O << std::dec; |
Bruno Cardoso Lopes | a4e8200 | 2007-07-11 23:24:41 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Bruno Cardoso Lopes | 43d526d | 2008-07-14 14:42:54 +0000 | [diff] [blame] | 209 | //===----------------------------------------------------------------------===// |
| 210 | // Frame and Set directives |
| 211 | //===----------------------------------------------------------------------===// |
| 212 | |
| 213 | /// Frame Directive |
| 214 | void MipsAsmPrinter:: |
| 215 | emitFrameDirective(MachineFunction &MF) |
| 216 | { |
| 217 | const TargetRegisterInfo &RI = *TM.getRegisterInfo(); |
| 218 | |
| 219 | unsigned stackReg = RI.getFrameRegister(MF); |
| 220 | unsigned returnReg = RI.getRARegister(); |
| 221 | unsigned stackSize = MF.getFrameInfo()->getStackSize(); |
| 222 | |
| 223 | |
Anton Korobeynikov | 055a76b | 2008-07-19 13:16:32 +0000 | [diff] [blame] | 224 | O << "\t.frame\t" << '$' << LowercaseString(RI.get(stackReg).AsmName) |
| 225 | << ',' << stackSize << ',' |
| 226 | << '$' << LowercaseString(RI.get(returnReg).AsmName) |
| 227 | << '\n'; |
Bruno Cardoso Lopes | 43d526d | 2008-07-14 14:42:54 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | /// Emit Set directives. |
| 231 | const char * MipsAsmPrinter:: |
| 232 | emitCurrentABIString(void) |
| 233 | { |
| 234 | switch(Subtarget->getTargetABI()) { |
| 235 | case MipsSubtarget::O32: return "abi32"; |
| 236 | case MipsSubtarget::O64: return "abiO64"; |
| 237 | case MipsSubtarget::N32: return "abiN32"; |
| 238 | case MipsSubtarget::N64: return "abi64"; |
| 239 | case MipsSubtarget::EABI: return "eabi32"; // TODO: handle eabi64 |
| 240 | default: break; |
| 241 | } |
| 242 | |
| 243 | assert(0 && "Unknown Mips ABI"); |
| 244 | return NULL; |
| 245 | } |
| 246 | |
Anton Korobeynikov | ae408e6 | 2008-07-19 13:16:11 +0000 | [diff] [blame] | 247 | // Substitute old hook with new one temporary |
| 248 | std::string MipsAsmPrinter::getSectionForFunction(const Function &F) const { |
| 249 | return TAI->SectionForGlobal(&F); |
| 250 | } |
| 251 | |
Bruno Cardoso Lopes | a4e8200 | 2007-07-11 23:24:41 +0000 | [diff] [blame] | 252 | /// Emit the directives used by GAS on the start of functions |
| 253 | void MipsAsmPrinter:: |
| 254 | emitFunctionStart(MachineFunction &MF) |
| 255 | { |
| 256 | // Print out the label for the function. |
| 257 | const Function *F = MF.getFunction(); |
Anton Korobeynikov | ae408e6 | 2008-07-19 13:16:11 +0000 | [diff] [blame] | 258 | SwitchToTextSection(TAI->SectionForGlobal(F).c_str()); |
Bruno Cardoso Lopes | a4e8200 | 2007-07-11 23:24:41 +0000 | [diff] [blame] | 259 | |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 260 | // 2 bits aligned |
| 261 | EmitAlignment(2, F); |
Bruno Cardoso Lopes | a4e8200 | 2007-07-11 23:24:41 +0000 | [diff] [blame] | 262 | |
Anton Korobeynikov | 055a76b | 2008-07-19 13:16:32 +0000 | [diff] [blame] | 263 | O << "\t.globl\t" << CurrentFnName << '\n'; |
| 264 | O << "\t.ent\t" << CurrentFnName << '\n'; |
Bruno Cardoso Lopes | 43d526d | 2008-07-14 14:42:54 +0000 | [diff] [blame] | 265 | |
| 266 | if ((TAI->hasDotTypeDotSizeDirective()) && Subtarget->isLinux()) |
| 267 | O << "\t.type\t" << CurrentFnName << ", @function\n"; |
| 268 | |
Bruno Cardoso Lopes | a4e8200 | 2007-07-11 23:24:41 +0000 | [diff] [blame] | 269 | O << CurrentFnName << ":\n"; |
| 270 | |
| 271 | emitFrameDirective(MF); |
| 272 | emitMaskDirective(MF); |
Bruno Cardoso Lopes | dc0c04c | 2007-08-28 05:06:17 +0000 | [diff] [blame] | 273 | emitFMaskDirective(MF); |
Bruno Cardoso Lopes | 0a60400 | 2007-10-09 03:01:19 +0000 | [diff] [blame] | 274 | |
Anton Korobeynikov | 055a76b | 2008-07-19 13:16:32 +0000 | [diff] [blame] | 275 | O << '\n'; |
Bruno Cardoso Lopes | a4e8200 | 2007-07-11 23:24:41 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | /// Emit the directives used by GAS on the end of functions |
| 279 | void MipsAsmPrinter:: |
Bruno Cardoso Lopes | 0a60400 | 2007-10-09 03:01:19 +0000 | [diff] [blame] | 280 | emitFunctionEnd(MachineFunction &MF) |
| 281 | { |
Bruno Cardoso Lopes | 43d526d | 2008-07-14 14:42:54 +0000 | [diff] [blame] | 282 | // There are instruction for this macros, but they must |
| 283 | // always be at the function end, and we can't emit and |
| 284 | // break with BB logic. |
| 285 | O << "\t.set\tmacro\n"; |
| 286 | O << "\t.set\treorder\n"; |
Bruno Cardoso Lopes | 0a60400 | 2007-10-09 03:01:19 +0000 | [diff] [blame] | 287 | |
Anton Korobeynikov | 055a76b | 2008-07-19 13:16:32 +0000 | [diff] [blame] | 288 | O << "\t.end\t" << CurrentFnName << '\n'; |
Bruno Cardoso Lopes | 43d526d | 2008-07-14 14:42:54 +0000 | [diff] [blame] | 289 | if (TAI->hasDotTypeDotSizeDirective() && !Subtarget->isLinux()) |
Anton Korobeynikov | 055a76b | 2008-07-19 13:16:32 +0000 | [diff] [blame] | 290 | O << "\t.size\t" << CurrentFnName << ", .-" << CurrentFnName << '\n'; |
Bruno Cardoso Lopes | a4e8200 | 2007-07-11 23:24:41 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 293 | /// runOnMachineFunction - This uses the printMachineInstruction() |
| 294 | /// method to print assembly for each instruction. |
| 295 | bool MipsAsmPrinter:: |
| 296 | runOnMachineFunction(MachineFunction &MF) |
| 297 | { |
| 298 | SetupMachineFunction(MF); |
| 299 | |
| 300 | // Print out constants referenced by the function |
| 301 | EmitConstantPool(MF.getConstantPool()); |
| 302 | |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 303 | // Print out jump tables referenced by the function |
| 304 | EmitJumpTableInfo(MF.getJumpTableInfo(), MF); |
| 305 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 306 | O << "\n\n"; |
| 307 | |
| 308 | // What's my mangled name? |
| 309 | CurrentFnName = Mang->getValueName(MF.getFunction()); |
| 310 | |
Bruno Cardoso Lopes | a4e8200 | 2007-07-11 23:24:41 +0000 | [diff] [blame] | 311 | // Emit the function start directives |
| 312 | emitFunctionStart(MF); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 313 | |
| 314 | // Print out code for the function. |
| 315 | for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); |
| 316 | I != E; ++I) { |
| 317 | |
| 318 | // Print a label for the basic block. |
| 319 | if (I != MF.begin()) { |
Evan Cheng | fb8075d | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 320 | printBasicBlockLabel(I, true, true); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 321 | O << '\n'; |
| 322 | } |
| 323 | |
| 324 | for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end(); |
| 325 | II != E; ++II) { |
| 326 | // Print the assembly for the instruction. |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 327 | printInstruction(II); |
| 328 | ++EmittedInsts; |
| 329 | } |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 330 | |
| 331 | // Each Basic Block is separated by a newline |
| 332 | O << '\n'; |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Bruno Cardoso Lopes | a4e8200 | 2007-07-11 23:24:41 +0000 | [diff] [blame] | 335 | // Emit function end directives |
Bruno Cardoso Lopes | 0a60400 | 2007-10-09 03:01:19 +0000 | [diff] [blame] | 336 | emitFunctionEnd(MF); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 337 | |
| 338 | // We didn't modify anything. |
| 339 | return false; |
| 340 | } |
| 341 | |
Bruno Cardoso Lopes | 91ef849 | 2008-08-02 19:42:36 +0000 | [diff] [blame^] | 342 | // Print out an operand for an inline asm expression. |
| 343 | bool MipsAsmPrinter:: |
| 344 | PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, |
| 345 | unsigned AsmVariant, const char *ExtraCode) |
| 346 | { |
| 347 | // Does this asm operand have a single letter operand modifier? |
| 348 | if (ExtraCode && ExtraCode[0]) |
| 349 | return true; // Unknown modifier. |
| 350 | |
| 351 | printOperand(MI, OpNo); |
| 352 | return false; |
| 353 | } |
| 354 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 355 | void MipsAsmPrinter:: |
| 356 | printOperand(const MachineInstr *MI, int opNum) |
| 357 | { |
| 358 | const MachineOperand &MO = MI->getOperand(opNum); |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 359 | const TargetRegisterInfo &RI = *TM.getRegisterInfo(); |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 360 | bool closeP = false; |
| 361 | bool isPIC = (TM.getRelocationModel() == Reloc::PIC_); |
| 362 | bool isCodeLarge = (TM.getCodeModel() == CodeModel::Large); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 363 | |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 364 | // %hi and %lo used on mips gas to load global addresses on |
| 365 | // static code. %got is used to load global addresses when |
| 366 | // using PIC_. %call16 is used to load direct call targets |
| 367 | // on PIC_ and small code size. %call_lo and %call_hi load |
| 368 | // direct call targets on PIC_ and large code size. |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 369 | if (MI->getOpcode() == Mips::LUi && !MO.isRegister() |
| 370 | && !MO.isImmediate()) { |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 371 | if ((isPIC) && (isCodeLarge)) |
| 372 | O << "%call_hi("; |
| 373 | else |
| 374 | O << "%hi("; |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 375 | closeP = true; |
| 376 | } else if ((MI->getOpcode() == Mips::ADDiu) && !MO.isRegister() |
| 377 | && !MO.isImmediate()) { |
Bruno Cardoso Lopes | 91fd532 | 2008-07-21 18:52:34 +0000 | [diff] [blame] | 378 | const MachineOperand &firstMO = MI->getOperand(opNum-1); |
| 379 | if (firstMO.getReg() == Mips::GP) |
| 380 | O << "%gp_rel("; |
| 381 | else |
| 382 | O << "%lo("; |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 383 | closeP = true; |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 384 | } else if ((isPIC) && (MI->getOpcode() == Mips::LW) |
| 385 | && (!MO.isRegister()) && (!MO.isImmediate())) { |
| 386 | const MachineOperand &firstMO = MI->getOperand(opNum-1); |
| 387 | const MachineOperand &lastMO = MI->getOperand(opNum+1); |
| 388 | if ((firstMO.isRegister()) && (lastMO.isRegister())) { |
| 389 | if ((firstMO.getReg() == Mips::T9) && (lastMO.getReg() == Mips::GP) |
| 390 | && (!isCodeLarge)) |
| 391 | O << "%call16("; |
| 392 | else if ((firstMO.getReg() != Mips::T9) && (lastMO.getReg() == Mips::GP)) |
| 393 | O << "%got("; |
| 394 | else if ((firstMO.getReg() == Mips::T9) && (lastMO.getReg() != Mips::GP) |
| 395 | && (isCodeLarge)) |
| 396 | O << "%call_lo("; |
| 397 | closeP = true; |
| 398 | } |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | switch (MO.getType()) |
| 402 | { |
| 403 | case MachineOperand::MO_Register: |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 404 | if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) |
Anton Korobeynikov | 055a76b | 2008-07-19 13:16:32 +0000 | [diff] [blame] | 405 | O << '$' << LowercaseString (RI.get(MO.getReg()).AsmName); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 406 | else |
Anton Korobeynikov | 055a76b | 2008-07-19 13:16:32 +0000 | [diff] [blame] | 407 | O << '$' << MO.getReg(); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 408 | break; |
| 409 | |
| 410 | case MachineOperand::MO_Immediate: |
| 411 | if ((MI->getOpcode() == Mips::SLTiu) || (MI->getOpcode() == Mips::ORi) || |
Bruno Cardoso Lopes | a4e8200 | 2007-07-11 23:24:41 +0000 | [diff] [blame] | 412 | (MI->getOpcode() == Mips::LUi) || (MI->getOpcode() == Mips::ANDi)) |
Chris Lattner | 9a1ceae | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 413 | O << (unsigned short int)MO.getImm(); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 414 | else |
Chris Lattner | 9a1ceae | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 415 | O << (short int)MO.getImm(); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 416 | break; |
| 417 | |
| 418 | case MachineOperand::MO_MachineBasicBlock: |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 419 | printBasicBlockLabel(MO.getMBB()); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 420 | return; |
| 421 | |
| 422 | case MachineOperand::MO_GlobalAddress: |
| 423 | O << Mang->getValueName(MO.getGlobal()); |
| 424 | break; |
| 425 | |
| 426 | case MachineOperand::MO_ExternalSymbol: |
| 427 | O << MO.getSymbolName(); |
| 428 | break; |
| 429 | |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 430 | case MachineOperand::MO_JumpTableIndex: |
| 431 | O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 432 | << '_' << MO.getIndex(); |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 433 | break; |
| 434 | |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 435 | // FIXME: Verify correct |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 436 | case MachineOperand::MO_ConstantPoolIndex: |
| 437 | O << TAI->getPrivateGlobalPrefix() << "CPI" |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 438 | << getFunctionNumber() << "_" << MO.getIndex(); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 439 | break; |
| 440 | |
| 441 | default: |
| 442 | O << "<unknown operand type>"; abort (); break; |
| 443 | } |
| 444 | |
| 445 | if (closeP) O << ")"; |
| 446 | } |
| 447 | |
| 448 | void MipsAsmPrinter:: |
| 449 | printMemOperand(const MachineInstr *MI, int opNum, const char *Modifier) |
| 450 | { |
Bruno Cardoso Lopes | b42abeb | 2007-09-24 20:15:11 +0000 | [diff] [blame] | 451 | // when using stack locations for not load/store instructions |
| 452 | // print the same way as all normal 3 operand instructions. |
| 453 | if (Modifier && !strcmp(Modifier, "stackloc")) { |
| 454 | printOperand(MI, opNum+1); |
| 455 | O << ", "; |
| 456 | printOperand(MI, opNum); |
| 457 | return; |
| 458 | } |
| 459 | |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 460 | // Load/Store memory operands -- imm($reg) |
| 461 | // If PIC target the target is loaded as the |
| 462 | // pattern lw $25,%call16($28) |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 463 | printOperand(MI, opNum); |
| 464 | O << "("; |
| 465 | printOperand(MI, opNum+1); |
| 466 | O << ")"; |
| 467 | } |
| 468 | |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 469 | void MipsAsmPrinter:: |
| 470 | printFCCOperand(const MachineInstr *MI, int opNum, const char *Modifier) |
| 471 | { |
| 472 | const MachineOperand& MO = MI->getOperand(opNum); |
| 473 | O << Mips::MipsFCCToString((Mips::CondCode)MO.getImm()); |
| 474 | } |
| 475 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 476 | bool MipsAsmPrinter:: |
| 477 | doInitialization(Module &M) |
| 478 | { |
| 479 | Mang = new Mangler(M); |
Bruno Cardoso Lopes | 43d526d | 2008-07-14 14:42:54 +0000 | [diff] [blame] | 480 | |
| 481 | // Tell the assembler which ABI we are using |
Anton Korobeynikov | 055a76b | 2008-07-19 13:16:32 +0000 | [diff] [blame] | 482 | O << "\t.section .mdebug." << emitCurrentABIString() << '\n'; |
Bruno Cardoso Lopes | 43d526d | 2008-07-14 14:42:54 +0000 | [diff] [blame] | 483 | |
| 484 | // TODO: handle O64 ABI |
| 485 | if (Subtarget->isABI_EABI()) |
| 486 | O << "\t.section .gcc_compiled_long" << |
Anton Korobeynikov | 055a76b | 2008-07-19 13:16:32 +0000 | [diff] [blame] | 487 | (Subtarget->isGP32bit() ? "32" : "64") << '\n'; |
Bruno Cardoso Lopes | 43d526d | 2008-07-14 14:42:54 +0000 | [diff] [blame] | 488 | |
| 489 | // return to previous section |
Anton Korobeynikov | 055a76b | 2008-07-19 13:16:32 +0000 | [diff] [blame] | 490 | O << "\t.previous" << '\n'; |
Bruno Cardoso Lopes | 43d526d | 2008-07-14 14:42:54 +0000 | [diff] [blame] | 491 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 492 | return false; // success |
| 493 | } |
| 494 | |
Anton Korobeynikov | ae408e6 | 2008-07-19 13:16:11 +0000 | [diff] [blame] | 495 | void MipsAsmPrinter:: |
| 496 | printModuleLevelGV(const GlobalVariable* GVar) { |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 497 | const TargetData *TD = TM.getTargetData(); |
| 498 | |
Anton Korobeynikov | ae408e6 | 2008-07-19 13:16:11 +0000 | [diff] [blame] | 499 | if (!GVar->hasInitializer()) |
| 500 | return; // External global require no code |
| 501 | |
| 502 | // Check to see if this is a special global used by LLVM, if so, emit it. |
| 503 | if (EmitSpecialLLVMGlobal(GVar)) |
| 504 | return; |
| 505 | |
| 506 | O << "\n\n"; |
| 507 | std::string SectionName = TAI->SectionForGlobal(GVar); |
| 508 | std::string name = Mang->getValueName(GVar); |
| 509 | Constant *C = GVar->getInitializer(); |
| 510 | const Type *CTy = C->getType(); |
| 511 | unsigned Size = TD->getABITypeSize(CTy); |
Bruno Cardoso Lopes | 91fd532 | 2008-07-21 18:52:34 +0000 | [diff] [blame] | 512 | const ConstantArray *CVA = dyn_cast<ConstantArray>(C); |
Anton Korobeynikov | ae408e6 | 2008-07-19 13:16:11 +0000 | [diff] [blame] | 513 | bool printSizeAndType = true; |
| 514 | |
| 515 | // A data structure or array is aligned in memory to the largest |
| 516 | // alignment boundary required by any data type inside it (this matches |
| 517 | // the Preferred Type Alignment). For integral types, the alignment is |
| 518 | // the type size. |
Anton Korobeynikov | ae408e6 | 2008-07-19 13:16:11 +0000 | [diff] [blame] | 519 | unsigned Align; |
| 520 | if (CTy->getTypeID() == Type::IntegerTyID || |
| 521 | CTy->getTypeID() == Type::VoidTyID) { |
| 522 | assert(!(Size & (Size-1)) && "Alignment is not a power of two!"); |
| 523 | Align = Log2_32(Size); |
| 524 | } else |
| 525 | Align = TD->getPreferredTypeAlignmentShift(CTy); |
| 526 | |
| 527 | // FIXME: ELF supports visibility |
| 528 | |
| 529 | SwitchToDataSection(SectionName.c_str()); |
| 530 | |
| 531 | if (C->isNullValue() && !GVar->hasSection()) { |
| 532 | if (!GVar->isThreadLocal() && |
| 533 | (GVar->hasInternalLinkage() || GVar->isWeakForLinker())) { |
| 534 | if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it. |
| 535 | |
Bruno Cardoso Lopes | 85e31e3 | 2008-07-28 19:11:24 +0000 | [diff] [blame] | 536 | if (GVar->hasInternalLinkage()) |
| 537 | O << "\t.local\t" << name << '\n'; |
| 538 | |
| 539 | O << TAI->getCOMMDirective() << name << ',' << Size; |
| 540 | if (TAI->getCOMMDirectiveTakesAlignment()) |
| 541 | O << ',' << (1 << Align); |
| 542 | |
Anton Korobeynikov | ae408e6 | 2008-07-19 13:16:11 +0000 | [diff] [blame] | 543 | O << '\n'; |
| 544 | return; |
| 545 | } |
| 546 | } |
| 547 | switch (GVar->getLinkage()) { |
| 548 | case GlobalValue::LinkOnceLinkage: |
| 549 | case GlobalValue::CommonLinkage: |
| 550 | case GlobalValue::WeakLinkage: |
| 551 | // FIXME: Verify correct for weak. |
| 552 | // Nonnull linkonce -> weak |
Anton Korobeynikov | 055a76b | 2008-07-19 13:16:32 +0000 | [diff] [blame] | 553 | O << "\t.weak " << name << '\n'; |
Anton Korobeynikov | ae408e6 | 2008-07-19 13:16:11 +0000 | [diff] [blame] | 554 | break; |
| 555 | case GlobalValue::AppendingLinkage: |
| 556 | // FIXME: appending linkage variables should go into a section of their name |
| 557 | // or something. For now, just emit them as external. |
| 558 | case GlobalValue::ExternalLinkage: |
| 559 | // If external or appending, declare as a global symbol |
Anton Korobeynikov | 055a76b | 2008-07-19 13:16:32 +0000 | [diff] [blame] | 560 | O << TAI->getGlobalDirective() << name << '\n'; |
Anton Korobeynikov | ae408e6 | 2008-07-19 13:16:11 +0000 | [diff] [blame] | 561 | // Fall Through |
| 562 | case GlobalValue::InternalLinkage: |
Bruno Cardoso Lopes | 91fd532 | 2008-07-21 18:52:34 +0000 | [diff] [blame] | 563 | if (CVA && CVA->isCString()) |
| 564 | printSizeAndType = false; |
Anton Korobeynikov | ae408e6 | 2008-07-19 13:16:11 +0000 | [diff] [blame] | 565 | break; |
| 566 | case GlobalValue::GhostLinkage: |
| 567 | cerr << "Should not have any unmaterialized functions!\n"; |
| 568 | abort(); |
| 569 | case GlobalValue::DLLImportLinkage: |
| 570 | cerr << "DLLImport linkage is not supported by this target!\n"; |
| 571 | abort(); |
| 572 | case GlobalValue::DLLExportLinkage: |
| 573 | cerr << "DLLExport linkage is not supported by this target!\n"; |
| 574 | abort(); |
| 575 | default: |
| 576 | assert(0 && "Unknown linkage type!"); |
| 577 | } |
| 578 | |
| 579 | if (Align) |
Anton Korobeynikov | 055a76b | 2008-07-19 13:16:32 +0000 | [diff] [blame] | 580 | O << "\t.align " << Align << '\n'; |
Anton Korobeynikov | ae408e6 | 2008-07-19 13:16:11 +0000 | [diff] [blame] | 581 | |
| 582 | if (TAI->hasDotTypeDotSizeDirective() && printSizeAndType) { |
| 583 | O << "\t.type " << name << ",@object\n"; |
Anton Korobeynikov | 055a76b | 2008-07-19 13:16:32 +0000 | [diff] [blame] | 584 | O << "\t.size " << name << ',' << Size << '\n'; |
Anton Korobeynikov | ae408e6 | 2008-07-19 13:16:11 +0000 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | O << name << ":\n"; |
| 588 | EmitGlobalConstant(C); |
| 589 | } |
| 590 | |
| 591 | bool MipsAsmPrinter:: |
| 592 | doFinalization(Module &M) |
| 593 | { |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 594 | // Print out module-level global variables here. |
| 595 | for (Module::const_global_iterator I = M.global_begin(), |
| 596 | E = M.global_end(); I != E; ++I) |
Anton Korobeynikov | ae408e6 | 2008-07-19 13:16:11 +0000 | [diff] [blame] | 597 | printModuleLevelGV(I); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 598 | |
Anton Korobeynikov | 055a76b | 2008-07-19 13:16:32 +0000 | [diff] [blame] | 599 | O << '\n'; |
Bruno Cardoso Lopes | d2947ee | 2008-06-04 01:45:25 +0000 | [diff] [blame] | 600 | |
Dan Gohman | b8275a3 | 2007-07-25 19:33:14 +0000 | [diff] [blame] | 601 | return AsmPrinter::doFinalization(M); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 602 | } |