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" |
| 18 | #include "MipsInstrInfo.h" |
| 19 | #include "MipsTargetMachine.h" |
Bruno Cardoso Lopes | a4e8200 | 2007-07-11 23:24:41 +0000 | [diff] [blame] | 20 | #include "MipsMachineFunction.h" |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 21 | #include "llvm/Constants.h" |
| 22 | #include "llvm/DerivedTypes.h" |
| 23 | #include "llvm/Module.h" |
| 24 | #include "llvm/CodeGen/AsmPrinter.h" |
| 25 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 26 | #include "llvm/CodeGen/MachineConstantPool.h" |
Bruno Cardoso Lopes | a4e8200 | 2007-07-11 23:24:41 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 28 | #include "llvm/CodeGen/MachineInstr.h" |
| 29 | #include "llvm/Target/TargetAsmInfo.h" |
| 30 | #include "llvm/Target/TargetData.h" |
| 31 | #include "llvm/Target/TargetMachine.h" |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 32 | #include "llvm/Target/TargetOptions.h" |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Mangler.h" |
| 34 | #include "llvm/ADT/Statistic.h" |
| 35 | #include "llvm/ADT/StringExtras.h" |
Bruno Cardoso Lopes | a4e8200 | 2007-07-11 23:24:41 +0000 | [diff] [blame] | 36 | #include "llvm/Support/Debug.h" |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 37 | #include "llvm/Support/CommandLine.h" |
| 38 | #include "llvm/Support/MathExtras.h" |
| 39 | #include <cctype> |
| 40 | |
| 41 | using namespace llvm; |
| 42 | |
| 43 | STATISTIC(EmittedInsts, "Number of machine instrs printed"); |
| 44 | |
| 45 | namespace { |
| 46 | struct VISIBILITY_HIDDEN MipsAsmPrinter : public AsmPrinter { |
| 47 | MipsAsmPrinter(std::ostream &O, MipsTargetMachine &TM, |
| 48 | const TargetAsmInfo *T): |
| 49 | AsmPrinter(O, TM, T) {} |
| 50 | |
| 51 | virtual const char *getPassName() const { |
| 52 | return "Mips Assembly Printer"; |
| 53 | } |
| 54 | |
Bruno Cardoso Lopes | a4e8200 | 2007-07-11 23:24:41 +0000 | [diff] [blame] | 55 | enum SetDirectiveFlags { |
| 56 | REORDER, // enables instruction reordering. |
| 57 | NOREORDER, // disables instruction reordering. |
| 58 | MACRO, // enables GAS macros. |
| 59 | NOMACRO // disables GAS macros. |
| 60 | }; |
| 61 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 62 | void printOperand(const MachineInstr *MI, int opNum); |
| 63 | void printMemOperand(const MachineInstr *MI, int opNum, |
| 64 | const char *Modifier = 0); |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 65 | void printFCCOperand(const MachineInstr *MI, int opNum, |
| 66 | const char *Modifier = 0); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 67 | |
Bruno Cardoso Lopes | dc0c04c | 2007-08-28 05:06:17 +0000 | [diff] [blame] | 68 | unsigned int getSavedRegsBitmask(bool isFloat, MachineFunction &MF); |
Bruno Cardoso Lopes | a4e8200 | 2007-07-11 23:24:41 +0000 | [diff] [blame] | 69 | void printHex32(unsigned int Value); |
Bruno Cardoso Lopes | dc0c04c | 2007-08-28 05:06:17 +0000 | [diff] [blame] | 70 | |
Bruno Cardoso Lopes | a4e8200 | 2007-07-11 23:24:41 +0000 | [diff] [blame] | 71 | void emitFunctionStart(MachineFunction &MF); |
Bruno Cardoso Lopes | 0a60400 | 2007-10-09 03:01:19 +0000 | [diff] [blame] | 72 | void emitFunctionEnd(MachineFunction &MF); |
Bruno Cardoso Lopes | a4e8200 | 2007-07-11 23:24:41 +0000 | [diff] [blame] | 73 | void emitFrameDirective(MachineFunction &MF); |
| 74 | void emitMaskDirective(MachineFunction &MF); |
Bruno Cardoso Lopes | dc0c04c | 2007-08-28 05:06:17 +0000 | [diff] [blame] | 75 | void emitFMaskDirective(MachineFunction &MF); |
Bruno Cardoso Lopes | a4e8200 | 2007-07-11 23:24:41 +0000 | [diff] [blame] | 76 | void emitSetDirective(SetDirectiveFlags Flag); |
| 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 | |
| 128 | /// Mask directive for GPR |
| 129 | void MipsAsmPrinter:: |
| 130 | emitMaskDirective(MachineFunction &MF) |
| 131 | { |
| 132 | MipsFunctionInfo *MipsFI = MF.getInfo<MipsFunctionInfo>(); |
| 133 | |
| 134 | int StackSize = MF.getFrameInfo()->getStackSize(); |
| 135 | int Offset = (!MipsFI->getTopSavedRegOffset()) ? 0 : |
| 136 | (-(StackSize-MipsFI->getTopSavedRegOffset())); |
| 137 | |
| 138 | #ifndef NDEBUG |
| 139 | DOUT << "--> emitMaskDirective" << "\n"; |
| 140 | DOUT << "StackSize : " << StackSize << "\n"; |
| 141 | DOUT << "getTopSavedReg : " << MipsFI->getTopSavedRegOffset() << "\n"; |
| 142 | DOUT << "Offset : " << Offset << "\n\n"; |
| 143 | #endif |
| 144 | |
| 145 | unsigned int Bitmask = getSavedRegsBitmask(false, MF); |
Bruno Cardoso Lopes | b42abeb | 2007-09-24 20:15:11 +0000 | [diff] [blame] | 146 | O << "\t.mask \t"; |
Bruno Cardoso Lopes | dc0c04c | 2007-08-28 05:06:17 +0000 | [diff] [blame] | 147 | printHex32(Bitmask); |
| 148 | O << "," << Offset << "\n"; |
| 149 | } |
| 150 | |
| 151 | /// TODO: Mask Directive for Float Point |
| 152 | void MipsAsmPrinter:: |
| 153 | emitFMaskDirective(MachineFunction &MF) |
| 154 | { |
| 155 | unsigned int Bitmask = getSavedRegsBitmask(true, MF); |
| 156 | |
| 157 | O << "\t.fmask\t"; |
| 158 | printHex32(Bitmask); |
| 159 | O << ",0" << "\n"; |
| 160 | } |
| 161 | |
| 162 | /// Frame Directive |
Bruno Cardoso Lopes | a4e8200 | 2007-07-11 23:24:41 +0000 | [diff] [blame] | 163 | void MipsAsmPrinter:: |
| 164 | emitFrameDirective(MachineFunction &MF) |
| 165 | { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 166 | const TargetRegisterInfo &RI = *TM.getRegisterInfo(); |
Bruno Cardoso Lopes | a4e8200 | 2007-07-11 23:24:41 +0000 | [diff] [blame] | 167 | |
| 168 | unsigned stackReg = RI.getFrameRegister(MF); |
| 169 | unsigned returnReg = RI.getRARegister(); |
| 170 | unsigned stackSize = MF.getFrameInfo()->getStackSize(); |
| 171 | |
| 172 | |
Bill Wendling | 74ab84c | 2008-02-26 21:11:01 +0000 | [diff] [blame] | 173 | O << "\t.frame\t" << "$" << LowercaseString(RI.get(stackReg).AsmName) |
Bruno Cardoso Lopes | a4e8200 | 2007-07-11 23:24:41 +0000 | [diff] [blame] | 174 | << "," << stackSize << "," |
Bill Wendling | 74ab84c | 2008-02-26 21:11:01 +0000 | [diff] [blame] | 175 | << "$" << LowercaseString(RI.get(returnReg).AsmName) |
Bruno Cardoso Lopes | a4e8200 | 2007-07-11 23:24:41 +0000 | [diff] [blame] | 176 | << "\n"; |
| 177 | } |
| 178 | |
Bruno Cardoso Lopes | a4e8200 | 2007-07-11 23:24:41 +0000 | [diff] [blame] | 179 | /// Emit Set directives. |
| 180 | void MipsAsmPrinter:: |
Bruno Cardoso Lopes | dc0c04c | 2007-08-28 05:06:17 +0000 | [diff] [blame] | 181 | emitSetDirective(SetDirectiveFlags Flag) |
| 182 | { |
Bruno Cardoso Lopes | a4e8200 | 2007-07-11 23:24:41 +0000 | [diff] [blame] | 183 | O << "\t.set\t"; |
| 184 | switch(Flag) { |
| 185 | case REORDER: O << "reorder" << "\n"; break; |
| 186 | case NOREORDER: O << "noreorder" << "\n"; break; |
| 187 | case MACRO: O << "macro" << "\n"; break; |
| 188 | case NOMACRO: O << "nomacro" << "\n"; break; |
| 189 | default: break; |
| 190 | } |
Bruno Cardoso Lopes | dc0c04c | 2007-08-28 05:06:17 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | // Create a bitmask with all callee saved registers for CPU |
| 194 | // or Float Point registers. For CPU registers consider RA, |
| 195 | // GP and FP for saving if necessary. |
| 196 | unsigned int MipsAsmPrinter:: |
| 197 | getSavedRegsBitmask(bool isFloat, MachineFunction &MF) |
| 198 | { |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 199 | const TargetRegisterInfo &RI = *TM.getRegisterInfo(); |
Bruno Cardoso Lopes | dc0c04c | 2007-08-28 05:06:17 +0000 | [diff] [blame] | 200 | |
| 201 | // Float Point Registers, TODO |
| 202 | if (isFloat) |
| 203 | return 0; |
| 204 | |
| 205 | // CPU Registers |
| 206 | unsigned int Bitmask = 0; |
| 207 | |
| 208 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 209 | const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo(); |
| 210 | for (unsigned i = 0, e = CSI.size(); i != e; ++i) |
| 211 | Bitmask |= (1 << MipsRegisterInfo::getRegisterNumbering(CSI[i].getReg())); |
| 212 | |
| 213 | if (RI.hasFP(MF)) |
Bruno Cardoso Lopes | 0a60400 | 2007-10-09 03:01:19 +0000 | [diff] [blame] | 214 | Bitmask |= (1 << MipsRegisterInfo:: |
| 215 | getRegisterNumbering(RI.getFrameRegister(MF))); |
Bruno Cardoso Lopes | dc0c04c | 2007-08-28 05:06:17 +0000 | [diff] [blame] | 216 | |
| 217 | if (MF.getFrameInfo()->hasCalls()) |
Bruno Cardoso Lopes | 0a60400 | 2007-10-09 03:01:19 +0000 | [diff] [blame] | 218 | Bitmask |= (1 << MipsRegisterInfo:: |
| 219 | getRegisterNumbering(RI.getRARegister())); |
Bruno Cardoso Lopes | dc0c04c | 2007-08-28 05:06:17 +0000 | [diff] [blame] | 220 | |
| 221 | return Bitmask; |
| 222 | } |
| 223 | |
| 224 | // Print a 32 bit hex number with all numbers. |
| 225 | void MipsAsmPrinter:: |
| 226 | printHex32(unsigned int Value) |
| 227 | { |
| 228 | O << "0x" << std::hex; |
| 229 | for (int i = 7; i >= 0; i--) |
| 230 | O << std::hex << ( (Value & (0xF << (i*4))) >> (i*4) ); |
| 231 | O << std::dec; |
Bruno Cardoso Lopes | a4e8200 | 2007-07-11 23:24:41 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | /// Emit the directives used by GAS on the start of functions |
| 235 | void MipsAsmPrinter:: |
| 236 | emitFunctionStart(MachineFunction &MF) |
| 237 | { |
| 238 | // Print out the label for the function. |
| 239 | const Function *F = MF.getFunction(); |
| 240 | SwitchToTextSection(getSectionForFunction(*F).c_str(), F); |
| 241 | |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 242 | // 2 bits aligned |
| 243 | EmitAlignment(2, F); |
Bruno Cardoso Lopes | a4e8200 | 2007-07-11 23:24:41 +0000 | [diff] [blame] | 244 | |
| 245 | O << "\t.globl\t" << CurrentFnName << "\n"; |
| 246 | O << "\t.ent\t" << CurrentFnName << "\n"; |
| 247 | O << "\t.type\t" << CurrentFnName << ", @function\n"; |
| 248 | O << CurrentFnName << ":\n"; |
| 249 | |
| 250 | emitFrameDirective(MF); |
| 251 | emitMaskDirective(MF); |
Bruno Cardoso Lopes | dc0c04c | 2007-08-28 05:06:17 +0000 | [diff] [blame] | 252 | emitFMaskDirective(MF); |
Bruno Cardoso Lopes | 0a60400 | 2007-10-09 03:01:19 +0000 | [diff] [blame] | 253 | |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 254 | if (TM.getRelocationModel() == Reloc::Static) { |
Bruno Cardoso Lopes | 0a60400 | 2007-10-09 03:01:19 +0000 | [diff] [blame] | 255 | emitSetDirective(NOREORDER); |
| 256 | emitSetDirective(NOMACRO); |
| 257 | } |
| 258 | |
Bruno Cardoso Lopes | 250a171 | 2007-08-18 02:05:24 +0000 | [diff] [blame] | 259 | O << "\n"; |
Bruno Cardoso Lopes | a4e8200 | 2007-07-11 23:24:41 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | /// Emit the directives used by GAS on the end of functions |
| 263 | void MipsAsmPrinter:: |
Bruno Cardoso Lopes | 0a60400 | 2007-10-09 03:01:19 +0000 | [diff] [blame] | 264 | emitFunctionEnd(MachineFunction &MF) |
| 265 | { |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 266 | if (TM.getRelocationModel() == Reloc::Static) { |
Bruno Cardoso Lopes | 0a60400 | 2007-10-09 03:01:19 +0000 | [diff] [blame] | 267 | emitSetDirective(MACRO); |
| 268 | emitSetDirective(REORDER); |
| 269 | } |
| 270 | |
Bruno Cardoso Lopes | a4e8200 | 2007-07-11 23:24:41 +0000 | [diff] [blame] | 271 | O << "\t.end\t" << CurrentFnName << "\n"; |
| 272 | } |
| 273 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 274 | /// runOnMachineFunction - This uses the printMachineInstruction() |
| 275 | /// method to print assembly for each instruction. |
| 276 | bool MipsAsmPrinter:: |
| 277 | runOnMachineFunction(MachineFunction &MF) |
| 278 | { |
| 279 | SetupMachineFunction(MF); |
| 280 | |
| 281 | // Print out constants referenced by the function |
| 282 | EmitConstantPool(MF.getConstantPool()); |
| 283 | |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 284 | // Print out jump tables referenced by the function |
| 285 | EmitJumpTableInfo(MF.getJumpTableInfo(), MF); |
| 286 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 287 | O << "\n\n"; |
| 288 | |
| 289 | // What's my mangled name? |
| 290 | CurrentFnName = Mang->getValueName(MF.getFunction()); |
| 291 | |
Bruno Cardoso Lopes | a4e8200 | 2007-07-11 23:24:41 +0000 | [diff] [blame] | 292 | // Emit the function start directives |
| 293 | emitFunctionStart(MF); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 294 | |
| 295 | // Print out code for the function. |
| 296 | for (MachineFunction::const_iterator I = MF.begin(), E = MF.end(); |
| 297 | I != E; ++I) { |
| 298 | |
| 299 | // Print a label for the basic block. |
| 300 | if (I != MF.begin()) { |
Evan Cheng | fb8075d | 2008-02-28 00:43:03 +0000 | [diff] [blame] | 301 | printBasicBlockLabel(I, true, true); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 302 | O << '\n'; |
| 303 | } |
| 304 | |
| 305 | for (MachineBasicBlock::const_iterator II = I->begin(), E = I->end(); |
| 306 | II != E; ++II) { |
| 307 | // Print the assembly for the instruction. |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 308 | printInstruction(II); |
| 309 | ++EmittedInsts; |
| 310 | } |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 311 | |
| 312 | // Each Basic Block is separated by a newline |
| 313 | O << '\n'; |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Bruno Cardoso Lopes | a4e8200 | 2007-07-11 23:24:41 +0000 | [diff] [blame] | 316 | // Emit function end directives |
Bruno Cardoso Lopes | 0a60400 | 2007-10-09 03:01:19 +0000 | [diff] [blame] | 317 | emitFunctionEnd(MF); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 318 | |
| 319 | // We didn't modify anything. |
| 320 | return false; |
| 321 | } |
| 322 | |
| 323 | void MipsAsmPrinter:: |
| 324 | printOperand(const MachineInstr *MI, int opNum) |
| 325 | { |
| 326 | const MachineOperand &MO = MI->getOperand(opNum); |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 327 | const TargetRegisterInfo &RI = *TM.getRegisterInfo(); |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 328 | bool closeP = false; |
| 329 | bool isPIC = (TM.getRelocationModel() == Reloc::PIC_); |
| 330 | bool isCodeLarge = (TM.getCodeModel() == CodeModel::Large); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 331 | |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 332 | // %hi and %lo used on mips gas to load global addresses on |
| 333 | // static code. %got is used to load global addresses when |
| 334 | // using PIC_. %call16 is used to load direct call targets |
| 335 | // on PIC_ and small code size. %call_lo and %call_hi load |
| 336 | // direct call targets on PIC_ and large code size. |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 337 | if (MI->getOpcode() == Mips::LUi && !MO.isRegister() |
| 338 | && !MO.isImmediate()) { |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 339 | if ((isPIC) && (isCodeLarge)) |
| 340 | O << "%call_hi("; |
| 341 | else |
| 342 | O << "%hi("; |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 343 | closeP = true; |
| 344 | } else if ((MI->getOpcode() == Mips::ADDiu) && !MO.isRegister() |
| 345 | && !MO.isImmediate()) { |
| 346 | O << "%lo("; |
| 347 | closeP = true; |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 348 | } else if ((isPIC) && (MI->getOpcode() == Mips::LW) |
| 349 | && (!MO.isRegister()) && (!MO.isImmediate())) { |
| 350 | const MachineOperand &firstMO = MI->getOperand(opNum-1); |
| 351 | const MachineOperand &lastMO = MI->getOperand(opNum+1); |
| 352 | if ((firstMO.isRegister()) && (lastMO.isRegister())) { |
| 353 | if ((firstMO.getReg() == Mips::T9) && (lastMO.getReg() == Mips::GP) |
| 354 | && (!isCodeLarge)) |
| 355 | O << "%call16("; |
| 356 | else if ((firstMO.getReg() != Mips::T9) && (lastMO.getReg() == Mips::GP)) |
| 357 | O << "%got("; |
| 358 | else if ((firstMO.getReg() == Mips::T9) && (lastMO.getReg() != Mips::GP) |
| 359 | && (isCodeLarge)) |
| 360 | O << "%call_lo("; |
| 361 | closeP = true; |
| 362 | } |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | switch (MO.getType()) |
| 366 | { |
| 367 | case MachineOperand::MO_Register: |
Dan Gohman | 6f0d024 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 368 | if (TargetRegisterInfo::isPhysicalRegister(MO.getReg())) |
Bill Wendling | 74ab84c | 2008-02-26 21:11:01 +0000 | [diff] [blame] | 369 | O << "$" << LowercaseString (RI.get(MO.getReg()).AsmName); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 370 | else |
| 371 | O << "$" << MO.getReg(); |
| 372 | break; |
| 373 | |
| 374 | case MachineOperand::MO_Immediate: |
| 375 | if ((MI->getOpcode() == Mips::SLTiu) || (MI->getOpcode() == Mips::ORi) || |
Bruno Cardoso Lopes | a4e8200 | 2007-07-11 23:24:41 +0000 | [diff] [blame] | 376 | (MI->getOpcode() == Mips::LUi) || (MI->getOpcode() == Mips::ANDi)) |
Chris Lattner | 9a1ceae | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 377 | O << (unsigned short int)MO.getImm(); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 378 | else |
Chris Lattner | 9a1ceae | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 379 | O << (short int)MO.getImm(); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 380 | break; |
| 381 | |
| 382 | case MachineOperand::MO_MachineBasicBlock: |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 383 | printBasicBlockLabel(MO.getMBB()); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 384 | return; |
| 385 | |
| 386 | case MachineOperand::MO_GlobalAddress: |
| 387 | O << Mang->getValueName(MO.getGlobal()); |
| 388 | break; |
| 389 | |
| 390 | case MachineOperand::MO_ExternalSymbol: |
| 391 | O << MO.getSymbolName(); |
| 392 | break; |
| 393 | |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 394 | case MachineOperand::MO_JumpTableIndex: |
| 395 | O << TAI->getPrivateGlobalPrefix() << "JTI" << getFunctionNumber() |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 396 | << '_' << MO.getIndex(); |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 397 | break; |
| 398 | |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 399 | // FIXME: Verify correct |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 400 | case MachineOperand::MO_ConstantPoolIndex: |
| 401 | O << TAI->getPrivateGlobalPrefix() << "CPI" |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 402 | << getFunctionNumber() << "_" << MO.getIndex(); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 403 | break; |
| 404 | |
| 405 | default: |
| 406 | O << "<unknown operand type>"; abort (); break; |
| 407 | } |
| 408 | |
| 409 | if (closeP) O << ")"; |
| 410 | } |
| 411 | |
| 412 | void MipsAsmPrinter:: |
| 413 | printMemOperand(const MachineInstr *MI, int opNum, const char *Modifier) |
| 414 | { |
Bruno Cardoso Lopes | b42abeb | 2007-09-24 20:15:11 +0000 | [diff] [blame] | 415 | // when using stack locations for not load/store instructions |
| 416 | // print the same way as all normal 3 operand instructions. |
| 417 | if (Modifier && !strcmp(Modifier, "stackloc")) { |
| 418 | printOperand(MI, opNum+1); |
| 419 | O << ", "; |
| 420 | printOperand(MI, opNum); |
| 421 | return; |
| 422 | } |
| 423 | |
Bruno Cardoso Lopes | c7db561 | 2007-11-05 03:02:32 +0000 | [diff] [blame] | 424 | // Load/Store memory operands -- imm($reg) |
| 425 | // If PIC target the target is loaded as the |
| 426 | // pattern lw $25,%call16($28) |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 427 | printOperand(MI, opNum); |
| 428 | O << "("; |
| 429 | printOperand(MI, opNum+1); |
| 430 | O << ")"; |
| 431 | } |
| 432 | |
Bruno Cardoso Lopes | 225ca9c | 2008-07-05 19:05:21 +0000 | [diff] [blame] | 433 | void MipsAsmPrinter:: |
| 434 | printFCCOperand(const MachineInstr *MI, int opNum, const char *Modifier) |
| 435 | { |
| 436 | const MachineOperand& MO = MI->getOperand(opNum); |
| 437 | O << Mips::MipsFCCToString((Mips::CondCode)MO.getImm()); |
| 438 | } |
| 439 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 440 | bool MipsAsmPrinter:: |
| 441 | doInitialization(Module &M) |
| 442 | { |
| 443 | Mang = new Mangler(M); |
| 444 | return false; // success |
| 445 | } |
| 446 | |
| 447 | bool MipsAsmPrinter:: |
| 448 | doFinalization(Module &M) |
| 449 | { |
| 450 | const TargetData *TD = TM.getTargetData(); |
| 451 | |
| 452 | // Print out module-level global variables here. |
| 453 | for (Module::const_global_iterator I = M.global_begin(), |
| 454 | E = M.global_end(); I != E; ++I) |
| 455 | |
| 456 | // External global require no code |
| 457 | if (I->hasInitializer()) { |
| 458 | |
| 459 | // Check to see if this is a special global |
| 460 | // used by LLVM, if so, emit it. |
| 461 | if (EmitSpecialLLVMGlobal(I)) |
| 462 | continue; |
| 463 | |
| 464 | O << "\n\n"; |
| 465 | std::string name = Mang->getValueName(I); |
| 466 | Constant *C = I->getInitializer(); |
Duncan Sands | ca0ed74 | 2007-11-05 00:04:43 +0000 | [diff] [blame] | 467 | unsigned Size = TD->getABITypeSize(C->getType()); |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 468 | unsigned Align = TD->getPreferredAlignmentLog(I); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 469 | |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 470 | // Is this correct ? |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 471 | if (C->isNullValue() && (I->hasLinkOnceLinkage() || |
Dale Johannesen | aafce77 | 2008-05-14 20:12:51 +0000 | [diff] [blame] | 472 | I->hasInternalLinkage() || I->hasWeakLinkage() || |
| 473 | I->hasCommonLinkage())) |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 474 | { |
| 475 | if (Size == 0) Size = 1; // .comm Foo, 0 is undefined, avoid it. |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 476 | |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 477 | if (!NoZerosInBSS && TAI->getBSSSection()) |
| 478 | SwitchToDataSection(TAI->getBSSSection(), I); |
| 479 | else |
| 480 | SwitchToDataSection(TAI->getDataSection(), I); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 481 | |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 482 | if (I->hasInternalLinkage()) { |
| 483 | if (TAI->getLCOMMDirective()) |
| 484 | O << TAI->getLCOMMDirective() << name << "," << Size; |
| 485 | else |
| 486 | O << "\t.local\t" << name << "\n"; |
| 487 | } else { |
| 488 | O << TAI->getCOMMDirective() << name << "," << Size; |
| 489 | // The .comm alignment in bytes. |
| 490 | if (TAI->getCOMMDirectiveTakesAlignment()) |
| 491 | O << "," << (1 << Align); |
| 492 | } |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 493 | |
| 494 | } else { |
| 495 | |
| 496 | switch (I->getLinkage()) |
| 497 | { |
| 498 | case GlobalValue::LinkOnceLinkage: |
Dale Johannesen | aafce77 | 2008-05-14 20:12:51 +0000 | [diff] [blame] | 499 | case GlobalValue::CommonLinkage: |
| 500 | case GlobalValue::WeakLinkage: |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 501 | // FIXME: Verify correct for weak. |
| 502 | // Nonnull linkonce -> weak |
| 503 | O << "\t.weak " << name << "\n"; |
| 504 | SwitchToDataSection("", I); |
| 505 | O << "\t.section\t\".llvm.linkonce.d." << name |
| 506 | << "\",\"aw\",@progbits\n"; |
| 507 | break; |
| 508 | case GlobalValue::AppendingLinkage: |
| 509 | // FIXME: appending linkage variables |
| 510 | // should go into a section of their name or |
| 511 | // something. For now, just emit them as external. |
| 512 | case GlobalValue::ExternalLinkage: |
| 513 | // If external or appending, declare as a global symbol |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 514 | O << TAI->getGlobalDirective() << name << "\n"; |
| 515 | // Fall Through |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 516 | case GlobalValue::InternalLinkage: |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 517 | // FIXME: special handling for ".ctors" & ".dtors" sections |
| 518 | if (I->hasSection() && (I->getSection() == ".ctors" || |
| 519 | I->getSection() == ".dtors")) { |
| 520 | std::string SectionName = ".section " + I->getSection(); |
| 521 | SectionName += ",\"aw\",%progbits"; |
| 522 | SwitchToDataSection(SectionName.c_str()); |
| 523 | } else { |
| 524 | if (C->isNullValue() && !NoZerosInBSS && TAI->getBSSSection()) |
| 525 | SwitchToDataSection(TAI->getBSSSection(), I); |
| 526 | else if (!I->isConstant()) |
| 527 | SwitchToDataSection(TAI->getDataSection(), I); |
| 528 | else { |
| 529 | // Read-only data. |
| 530 | if (TAI->getReadOnlySection()) |
| 531 | SwitchToDataSection(TAI->getReadOnlySection(), I); |
| 532 | else |
| 533 | SwitchToDataSection(TAI->getDataSection(), I); |
| 534 | } |
| 535 | } |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 536 | break; |
| 537 | case GlobalValue::GhostLinkage: |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 538 | cerr << "Should not have any unmaterialized functions!\n"; |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 539 | abort(); |
| 540 | case GlobalValue::DLLImportLinkage: |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 541 | cerr << "DLLImport linkage is not supported by this target!\n"; |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 542 | abort(); |
| 543 | case GlobalValue::DLLExportLinkage: |
Bruno Cardoso Lopes | 753a987 | 2007-11-12 19:49:57 +0000 | [diff] [blame] | 544 | cerr << "DLLExport linkage is not supported by this target!\n"; |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 545 | abort(); |
| 546 | default: |
| 547 | assert(0 && "Unknown linkage type!"); |
| 548 | } |
Bruno Cardoso Lopes | dc0c04c | 2007-08-28 05:06:17 +0000 | [diff] [blame] | 549 | |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 550 | O << "\t.align " << Align << "\n"; |
| 551 | O << "\t.type " << name << ",@object\n"; |
| 552 | O << "\t.size " << name << "," << Size << "\n"; |
| 553 | O << name << ":\n"; |
| 554 | EmitGlobalConstant(C); |
| 555 | } |
| 556 | } |
| 557 | |
Bruno Cardoso Lopes | d2947ee | 2008-06-04 01:45:25 +0000 | [diff] [blame] | 558 | O << "\n"; |
| 559 | |
Dan Gohman | b8275a3 | 2007-07-25 19:33:14 +0000 | [diff] [blame] | 560 | return AsmPrinter::doFinalization(M); |
Bruno Cardoso Lopes | 972f589 | 2007-06-06 07:42:06 +0000 | [diff] [blame] | 561 | } |