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