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