Jack Carter | 86ac5c1 | 2013-11-18 23:55:27 +0000 | [diff] [blame] | 1 | //===-- MipsTargetStreamer.cpp - Mips Target Streamer Methods -------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file provides Mips specific target streamer methods. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Mehdi Amini | b550cb1 | 2016-04-18 09:17:29 +0000 | [diff] [blame] | 14 | #include "MipsTargetStreamer.h" |
Rafael Espindola | 054234f | 2014-01-27 03:53:56 +0000 | [diff] [blame] | 15 | #include "InstPrinter/MipsInstPrinter.h" |
Daniel Sanders | 68c3747 | 2014-07-21 13:30:55 +0000 | [diff] [blame] | 16 | #include "MipsELFStreamer.h" |
Daniel Sanders | fe98b2f | 2016-05-03 13:35:44 +0000 | [diff] [blame] | 17 | #include "MipsMCExpr.h" |
Chandler Carruth | 442f784 | 2014-03-04 10:07:28 +0000 | [diff] [blame] | 18 | #include "MipsMCTargetDesc.h" |
Rafael Espindola | 972e71a | 2014-01-31 23:10:26 +0000 | [diff] [blame] | 19 | #include "MipsTargetObjectFile.h" |
Rafael Espindola | 972e71a | 2014-01-31 23:10:26 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCContext.h" |
Rafael Espindola | 972e71a | 2014-01-31 23:10:26 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCSectionELF.h" |
Rafael Espindola | cb1953f | 2014-01-26 06:57:13 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCSubtargetInfo.h" |
Rafael Espindola | 95fb9b9 | 2015-06-02 20:38:46 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCSymbolELF.h" |
Daniel Sanders | c07f06a | 2016-05-04 13:21:06 +0000 | [diff] [blame] | 24 | #include "llvm/Support/CommandLine.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 25 | #include "llvm/Support/ELF.h" |
Jack Carter | 86ac5c1 | 2013-11-18 23:55:27 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ErrorHandling.h" |
| 27 | #include "llvm/Support/FormattedStream.h" |
| 28 | |
| 29 | using namespace llvm; |
| 30 | |
Daniel Sanders | c07f06a | 2016-05-04 13:21:06 +0000 | [diff] [blame] | 31 | namespace { |
| 32 | static cl::opt<bool> RoundSectionSizes( |
| 33 | "mips-round-section-sizes", cl::init(false), |
| 34 | cl::desc("Round section sizes up to the section alignment"), cl::Hidden); |
| 35 | } // end anonymous namespace |
| 36 | |
Vladimir Medic | fb8a2a9 | 2014-07-08 08:59:22 +0000 | [diff] [blame] | 37 | MipsTargetStreamer::MipsTargetStreamer(MCStreamer &S) |
Daniel Sanders | cdb45fa | 2014-08-14 09:18:14 +0000 | [diff] [blame] | 38 | : MCTargetStreamer(S), ModuleDirectiveAllowed(true) { |
Daniel Sanders | d97a634 | 2014-08-13 10:07:34 +0000 | [diff] [blame] | 39 | GPRInfoSet = FPRInfoSet = FrameInfoSet = false; |
| 40 | } |
Rafael Espindola | 60890b8 | 2014-06-23 19:43:40 +0000 | [diff] [blame] | 41 | void MipsTargetStreamer::emitDirectiveSetMicroMips() {} |
| 42 | void MipsTargetStreamer::emitDirectiveSetNoMicroMips() {} |
Daniel Sanders | cda908a | 2016-05-16 09:10:13 +0000 | [diff] [blame] | 43 | void MipsTargetStreamer::setUsesMicroMips() {} |
Rafael Espindola | 60890b8 | 2014-06-23 19:43:40 +0000 | [diff] [blame] | 44 | void MipsTargetStreamer::emitDirectiveSetMips16() {} |
Daniel Sanders | cdb45fa | 2014-08-14 09:18:14 +0000 | [diff] [blame] | 45 | void MipsTargetStreamer::emitDirectiveSetNoMips16() { forbidModuleDirective(); } |
| 46 | void MipsTargetStreamer::emitDirectiveSetReorder() { forbidModuleDirective(); } |
Rafael Espindola | 60890b8 | 2014-06-23 19:43:40 +0000 | [diff] [blame] | 47 | void MipsTargetStreamer::emitDirectiveSetNoReorder() {} |
Daniel Sanders | cdb45fa | 2014-08-14 09:18:14 +0000 | [diff] [blame] | 48 | void MipsTargetStreamer::emitDirectiveSetMacro() { forbidModuleDirective(); } |
| 49 | void MipsTargetStreamer::emitDirectiveSetNoMacro() { forbidModuleDirective(); } |
| 50 | void MipsTargetStreamer::emitDirectiveSetMsa() { forbidModuleDirective(); } |
| 51 | void MipsTargetStreamer::emitDirectiveSetNoMsa() { forbidModuleDirective(); } |
| 52 | void MipsTargetStreamer::emitDirectiveSetAt() { forbidModuleDirective(); } |
Toma Tabacu | 16a7449 | 2015-02-13 10:30:57 +0000 | [diff] [blame] | 53 | void MipsTargetStreamer::emitDirectiveSetAtWithArg(unsigned RegNo) { |
| 54 | forbidModuleDirective(); |
| 55 | } |
Daniel Sanders | cdb45fa | 2014-08-14 09:18:14 +0000 | [diff] [blame] | 56 | void MipsTargetStreamer::emitDirectiveSetNoAt() { forbidModuleDirective(); } |
Rafael Espindola | 60890b8 | 2014-06-23 19:43:40 +0000 | [diff] [blame] | 57 | void MipsTargetStreamer::emitDirectiveEnd(StringRef Name) {} |
| 58 | void MipsTargetStreamer::emitDirectiveEnt(const MCSymbol &Symbol) {} |
| 59 | void MipsTargetStreamer::emitDirectiveAbiCalls() {} |
| 60 | void MipsTargetStreamer::emitDirectiveNaN2008() {} |
| 61 | void MipsTargetStreamer::emitDirectiveNaNLegacy() {} |
| 62 | void MipsTargetStreamer::emitDirectiveOptionPic0() {} |
| 63 | void MipsTargetStreamer::emitDirectiveOptionPic2() {} |
Toma Tabacu | 9ca5096 | 2015-04-16 09:53:47 +0000 | [diff] [blame] | 64 | void MipsTargetStreamer::emitDirectiveInsn() { forbidModuleDirective(); } |
Rafael Espindola | 60890b8 | 2014-06-23 19:43:40 +0000 | [diff] [blame] | 65 | void MipsTargetStreamer::emitFrame(unsigned StackReg, unsigned StackSize, |
| 66 | unsigned ReturnReg) {} |
| 67 | void MipsTargetStreamer::emitMask(unsigned CPUBitmask, int CPUTopSavedRegOff) {} |
| 68 | void MipsTargetStreamer::emitFMask(unsigned FPUBitmask, int FPUTopSavedRegOff) { |
| 69 | } |
Toma Tabacu | 85618b3 | 2014-08-19 14:22:52 +0000 | [diff] [blame] | 70 | void MipsTargetStreamer::emitDirectiveSetArch(StringRef Arch) { |
| 71 | forbidModuleDirective(); |
| 72 | } |
Toma Tabacu | 4e0cf8e | 2015-03-06 12:15:12 +0000 | [diff] [blame] | 73 | void MipsTargetStreamer::emitDirectiveSetMips0() { forbidModuleDirective(); } |
Daniel Sanders | cdb45fa | 2014-08-14 09:18:14 +0000 | [diff] [blame] | 74 | void MipsTargetStreamer::emitDirectiveSetMips1() { forbidModuleDirective(); } |
| 75 | void MipsTargetStreamer::emitDirectiveSetMips2() { forbidModuleDirective(); } |
| 76 | void MipsTargetStreamer::emitDirectiveSetMips3() { forbidModuleDirective(); } |
| 77 | void MipsTargetStreamer::emitDirectiveSetMips4() { forbidModuleDirective(); } |
| 78 | void MipsTargetStreamer::emitDirectiveSetMips5() { forbidModuleDirective(); } |
| 79 | void MipsTargetStreamer::emitDirectiveSetMips32() { forbidModuleDirective(); } |
| 80 | void MipsTargetStreamer::emitDirectiveSetMips32R2() { forbidModuleDirective(); } |
Daniel Sanders | 1779314 | 2015-02-18 16:24:50 +0000 | [diff] [blame] | 81 | void MipsTargetStreamer::emitDirectiveSetMips32R3() { forbidModuleDirective(); } |
| 82 | void MipsTargetStreamer::emitDirectiveSetMips32R5() { forbidModuleDirective(); } |
Daniel Sanders | cdb45fa | 2014-08-14 09:18:14 +0000 | [diff] [blame] | 83 | void MipsTargetStreamer::emitDirectiveSetMips32R6() { forbidModuleDirective(); } |
| 84 | void MipsTargetStreamer::emitDirectiveSetMips64() { forbidModuleDirective(); } |
| 85 | void MipsTargetStreamer::emitDirectiveSetMips64R2() { forbidModuleDirective(); } |
Daniel Sanders | 1779314 | 2015-02-18 16:24:50 +0000 | [diff] [blame] | 86 | void MipsTargetStreamer::emitDirectiveSetMips64R3() { forbidModuleDirective(); } |
| 87 | void MipsTargetStreamer::emitDirectiveSetMips64R5() { forbidModuleDirective(); } |
Daniel Sanders | cdb45fa | 2014-08-14 09:18:14 +0000 | [diff] [blame] | 88 | void MipsTargetStreamer::emitDirectiveSetMips64R6() { forbidModuleDirective(); } |
Toma Tabacu | 4e0cf8e | 2015-03-06 12:15:12 +0000 | [diff] [blame] | 89 | void MipsTargetStreamer::emitDirectiveSetPop() { forbidModuleDirective(); } |
| 90 | void MipsTargetStreamer::emitDirectiveSetPush() { forbidModuleDirective(); } |
Toma Tabacu | 2969650 | 2015-06-02 09:48:04 +0000 | [diff] [blame] | 91 | void MipsTargetStreamer::emitDirectiveSetSoftFloat() { |
| 92 | forbidModuleDirective(); |
| 93 | } |
| 94 | void MipsTargetStreamer::emitDirectiveSetHardFloat() { |
| 95 | forbidModuleDirective(); |
| 96 | } |
Daniel Sanders | cdb45fa | 2014-08-14 09:18:14 +0000 | [diff] [blame] | 97 | void MipsTargetStreamer::emitDirectiveSetDsp() { forbidModuleDirective(); } |
Toma Tabacu | 351b2fe | 2014-09-17 09:01:54 +0000 | [diff] [blame] | 98 | void MipsTargetStreamer::emitDirectiveSetNoDsp() { forbidModuleDirective(); } |
Toma Tabacu | c4c202a | 2014-10-01 14:53:19 +0000 | [diff] [blame] | 99 | void MipsTargetStreamer::emitDirectiveCpLoad(unsigned RegNo) {} |
Daniel Sanders | df8510d | 2016-05-11 12:48:19 +0000 | [diff] [blame] | 100 | bool MipsTargetStreamer::emitDirectiveCpRestore( |
Benjamin Kramer | d3f4c05 | 2016-06-12 16:13:55 +0000 | [diff] [blame] | 101 | int Offset, function_ref<unsigned()> GetATReg, SMLoc IDLoc, |
Daniel Sanders | df8510d | 2016-05-11 12:48:19 +0000 | [diff] [blame] | 102 | const MCSubtargetInfo *STI) { |
Daniel Sanders | e2982ad | 2015-09-17 16:08:39 +0000 | [diff] [blame] | 103 | forbidModuleDirective(); |
Daniel Sanders | df8510d | 2016-05-11 12:48:19 +0000 | [diff] [blame] | 104 | return true; |
Daniel Sanders | e2982ad | 2015-09-17 16:08:39 +0000 | [diff] [blame] | 105 | } |
Rafael Espindola | 60890b8 | 2014-06-23 19:43:40 +0000 | [diff] [blame] | 106 | void MipsTargetStreamer::emitDirectiveCpsetup(unsigned RegNo, int RegOrOffset, |
| 107 | const MCSymbol &Sym, bool IsReg) { |
| 108 | } |
Daniel Sanders | f173dda | 2015-09-22 10:50:09 +0000 | [diff] [blame] | 109 | void MipsTargetStreamer::emitDirectiveCpreturn(unsigned SaveLocation, |
| 110 | bool SaveLocationIsRegister) {} |
Toma Tabacu | bfcbfd5 | 2015-06-23 12:34:19 +0000 | [diff] [blame] | 111 | |
Toma Tabacu | a64e540 | 2015-06-25 12:44:38 +0000 | [diff] [blame] | 112 | void MipsTargetStreamer::emitDirectiveModuleFP() {} |
Toma Tabacu | bfcbfd5 | 2015-06-23 12:34:19 +0000 | [diff] [blame] | 113 | |
Toma Tabacu | 3c49958 | 2015-06-25 10:56:57 +0000 | [diff] [blame] | 114 | void MipsTargetStreamer::emitDirectiveModuleOddSPReg() { |
| 115 | if (!ABIFlagsSection.OddSPReg && !ABIFlagsSection.Is32BitABI) |
Daniel Sanders | 7e52742 | 2014-07-10 13:38:23 +0000 | [diff] [blame] | 116 | report_fatal_error("+nooddspreg is only valid for O32"); |
| 117 | } |
Toma Tabacu | 0f09313 | 2015-06-30 13:46:03 +0000 | [diff] [blame] | 118 | void MipsTargetStreamer::emitDirectiveModuleSoftFloat() {} |
| 119 | void MipsTargetStreamer::emitDirectiveModuleHardFloat() {} |
Toma Tabacu | 4e0cf8e | 2015-03-06 12:15:12 +0000 | [diff] [blame] | 120 | void MipsTargetStreamer::emitDirectiveSetFp( |
| 121 | MipsABIFlagsSection::FpABIKind Value) { |
| 122 | forbidModuleDirective(); |
| 123 | } |
Toma Tabacu | 32c72aa | 2015-06-30 09:36:50 +0000 | [diff] [blame] | 124 | void MipsTargetStreamer::emitDirectiveSetOddSPReg() { forbidModuleDirective(); } |
| 125 | void MipsTargetStreamer::emitDirectiveSetNoOddSPReg() { |
| 126 | forbidModuleDirective(); |
| 127 | } |
Rafael Espindola | 24ea09e | 2014-01-26 06:06:37 +0000 | [diff] [blame] | 128 | |
Daniel Sanders | a736b37 | 2016-04-29 13:33:12 +0000 | [diff] [blame] | 129 | void MipsTargetStreamer::emitR(unsigned Opcode, unsigned Reg0, SMLoc IDLoc, |
| 130 | const MCSubtargetInfo *STI) { |
| 131 | MCInst TmpInst; |
| 132 | TmpInst.setOpcode(Opcode); |
| 133 | TmpInst.addOperand(MCOperand::createReg(Reg0)); |
| 134 | TmpInst.setLoc(IDLoc); |
| 135 | getStreamer().EmitInstruction(TmpInst, *STI); |
| 136 | } |
| 137 | |
| 138 | void MipsTargetStreamer::emitRX(unsigned Opcode, unsigned Reg0, MCOperand Op1, |
| 139 | SMLoc IDLoc, const MCSubtargetInfo *STI) { |
| 140 | MCInst TmpInst; |
| 141 | TmpInst.setOpcode(Opcode); |
| 142 | TmpInst.addOperand(MCOperand::createReg(Reg0)); |
| 143 | TmpInst.addOperand(Op1); |
| 144 | TmpInst.setLoc(IDLoc); |
| 145 | getStreamer().EmitInstruction(TmpInst, *STI); |
| 146 | } |
| 147 | |
| 148 | void MipsTargetStreamer::emitRI(unsigned Opcode, unsigned Reg0, int32_t Imm, |
| 149 | SMLoc IDLoc, const MCSubtargetInfo *STI) { |
| 150 | emitRX(Opcode, Reg0, MCOperand::createImm(Imm), IDLoc, STI); |
| 151 | } |
| 152 | |
| 153 | void MipsTargetStreamer::emitRR(unsigned Opcode, unsigned Reg0, unsigned Reg1, |
| 154 | SMLoc IDLoc, const MCSubtargetInfo *STI) { |
| 155 | emitRX(Opcode, Reg0, MCOperand::createReg(Reg1), IDLoc, STI); |
| 156 | } |
| 157 | |
| 158 | void MipsTargetStreamer::emitII(unsigned Opcode, int16_t Imm1, int16_t Imm2, |
| 159 | SMLoc IDLoc, const MCSubtargetInfo *STI) { |
| 160 | MCInst TmpInst; |
| 161 | TmpInst.setOpcode(Opcode); |
| 162 | TmpInst.addOperand(MCOperand::createImm(Imm1)); |
| 163 | TmpInst.addOperand(MCOperand::createImm(Imm2)); |
| 164 | TmpInst.setLoc(IDLoc); |
| 165 | getStreamer().EmitInstruction(TmpInst, *STI); |
| 166 | } |
| 167 | |
| 168 | void MipsTargetStreamer::emitRRX(unsigned Opcode, unsigned Reg0, unsigned Reg1, |
| 169 | MCOperand Op2, SMLoc IDLoc, |
| 170 | const MCSubtargetInfo *STI) { |
| 171 | MCInst TmpInst; |
| 172 | TmpInst.setOpcode(Opcode); |
| 173 | TmpInst.addOperand(MCOperand::createReg(Reg0)); |
| 174 | TmpInst.addOperand(MCOperand::createReg(Reg1)); |
| 175 | TmpInst.addOperand(Op2); |
| 176 | TmpInst.setLoc(IDLoc); |
| 177 | getStreamer().EmitInstruction(TmpInst, *STI); |
| 178 | } |
| 179 | |
| 180 | void MipsTargetStreamer::emitRRR(unsigned Opcode, unsigned Reg0, unsigned Reg1, |
| 181 | unsigned Reg2, SMLoc IDLoc, |
| 182 | const MCSubtargetInfo *STI) { |
| 183 | emitRRX(Opcode, Reg0, Reg1, MCOperand::createReg(Reg2), IDLoc, STI); |
| 184 | } |
| 185 | |
| 186 | void MipsTargetStreamer::emitRRI(unsigned Opcode, unsigned Reg0, unsigned Reg1, |
| 187 | int16_t Imm, SMLoc IDLoc, |
| 188 | const MCSubtargetInfo *STI) { |
| 189 | emitRRX(Opcode, Reg0, Reg1, MCOperand::createImm(Imm), IDLoc, STI); |
| 190 | } |
| 191 | |
| 192 | void MipsTargetStreamer::emitAddu(unsigned DstReg, unsigned SrcReg, |
| 193 | unsigned TrgReg, bool Is64Bit, |
| 194 | const MCSubtargetInfo *STI) { |
| 195 | emitRRR(Is64Bit ? Mips::DADDu : Mips::ADDu, DstReg, SrcReg, TrgReg, SMLoc(), |
| 196 | STI); |
| 197 | } |
| 198 | |
| 199 | void MipsTargetStreamer::emitDSLL(unsigned DstReg, unsigned SrcReg, |
| 200 | int16_t ShiftAmount, SMLoc IDLoc, |
| 201 | const MCSubtargetInfo *STI) { |
| 202 | if (ShiftAmount >= 32) { |
| 203 | emitRRI(Mips::DSLL32, DstReg, SrcReg, ShiftAmount - 32, IDLoc, STI); |
| 204 | return; |
| 205 | } |
| 206 | |
| 207 | emitRRI(Mips::DSLL, DstReg, SrcReg, ShiftAmount, IDLoc, STI); |
| 208 | } |
| 209 | |
| 210 | void MipsTargetStreamer::emitEmptyDelaySlot(bool hasShortDelaySlot, SMLoc IDLoc, |
| 211 | const MCSubtargetInfo *STI) { |
| 212 | if (hasShortDelaySlot) |
| 213 | emitRR(Mips::MOVE16_MM, Mips::ZERO, Mips::ZERO, IDLoc, STI); |
| 214 | else |
| 215 | emitRRI(Mips::SLL, Mips::ZERO, Mips::ZERO, 0, IDLoc, STI); |
| 216 | } |
| 217 | |
| 218 | void MipsTargetStreamer::emitNop(SMLoc IDLoc, const MCSubtargetInfo *STI) { |
| 219 | emitRRI(Mips::SLL, Mips::ZERO, Mips::ZERO, 0, IDLoc, STI); |
| 220 | } |
| 221 | |
Daniel Sanders | 7225cd5 | 2016-04-29 16:16:49 +0000 | [diff] [blame] | 222 | /// Emit the $gp restore operation for .cprestore. |
| 223 | void MipsTargetStreamer::emitGPRestore(int Offset, SMLoc IDLoc, |
| 224 | const MCSubtargetInfo *STI) { |
| 225 | emitLoadWithImmOffset(Mips::LW, Mips::GP, Mips::SP, Offset, Mips::GP, IDLoc, |
| 226 | STI); |
| 227 | } |
| 228 | |
| 229 | /// Emit a store instruction with an immediate offset. |
Daniel Sanders | fba875f | 2016-04-29 13:43:45 +0000 | [diff] [blame] | 230 | void MipsTargetStreamer::emitStoreWithImmOffset( |
| 231 | unsigned Opcode, unsigned SrcReg, unsigned BaseReg, int64_t Offset, |
Benjamin Kramer | d3f4c05 | 2016-06-12 16:13:55 +0000 | [diff] [blame] | 232 | function_ref<unsigned()> GetATReg, SMLoc IDLoc, |
Daniel Sanders | 241c679 | 2016-05-12 14:01:50 +0000 | [diff] [blame] | 233 | const MCSubtargetInfo *STI) { |
Daniel Sanders | 7225cd5 | 2016-04-29 16:16:49 +0000 | [diff] [blame] | 234 | if (isInt<16>(Offset)) { |
| 235 | emitRRI(Opcode, SrcReg, BaseReg, Offset, IDLoc, STI); |
| 236 | return; |
| 237 | } |
| 238 | |
Daniel Sanders | fba875f | 2016-04-29 13:43:45 +0000 | [diff] [blame] | 239 | // sw $8, offset($8) => lui $at, %hi(offset) |
| 240 | // add $at, $at, $8 |
| 241 | // sw $8, %lo(offset)($at) |
| 242 | |
Daniel Sanders | 241c679 | 2016-05-12 14:01:50 +0000 | [diff] [blame] | 243 | unsigned ATReg = GetATReg(); |
| 244 | if (!ATReg) |
| 245 | return; |
| 246 | |
Daniel Sanders | fba875f | 2016-04-29 13:43:45 +0000 | [diff] [blame] | 247 | unsigned LoOffset = Offset & 0x0000ffff; |
| 248 | unsigned HiOffset = (Offset & 0xffff0000) >> 16; |
| 249 | |
| 250 | // If msb of LoOffset is 1(negative number) we must increment HiOffset |
| 251 | // to account for the sign-extension of the low part. |
| 252 | if (LoOffset & 0x8000) |
| 253 | HiOffset++; |
| 254 | |
| 255 | // Generate the base address in ATReg. |
| 256 | emitRI(Mips::LUi, ATReg, HiOffset, IDLoc, STI); |
| 257 | if (BaseReg != Mips::ZERO) |
| 258 | emitRRR(Mips::ADDu, ATReg, ATReg, BaseReg, IDLoc, STI); |
| 259 | // Emit the store with the adjusted base and offset. |
| 260 | emitRRI(Opcode, SrcReg, ATReg, LoOffset, IDLoc, STI); |
| 261 | } |
| 262 | |
| 263 | /// Emit a store instruction with an symbol offset. Symbols are assumed to be |
| 264 | /// out of range for a simm16 will be expanded to appropriate instructions. |
| 265 | void MipsTargetStreamer::emitStoreWithSymOffset( |
| 266 | unsigned Opcode, unsigned SrcReg, unsigned BaseReg, MCOperand &HiOperand, |
| 267 | MCOperand &LoOperand, unsigned ATReg, SMLoc IDLoc, |
| 268 | const MCSubtargetInfo *STI) { |
| 269 | // sw $8, sym => lui $at, %hi(sym) |
| 270 | // sw $8, %lo(sym)($at) |
| 271 | |
| 272 | // Generate the base address in ATReg. |
| 273 | emitRX(Mips::LUi, ATReg, HiOperand, IDLoc, STI); |
| 274 | if (BaseReg != Mips::ZERO) |
| 275 | emitRRR(Mips::ADDu, ATReg, ATReg, BaseReg, IDLoc, STI); |
| 276 | // Emit the store with the adjusted base and offset. |
| 277 | emitRRX(Opcode, SrcReg, ATReg, LoOperand, IDLoc, STI); |
| 278 | } |
| 279 | |
Daniel Sanders | 7225cd5 | 2016-04-29 16:16:49 +0000 | [diff] [blame] | 280 | /// Emit a load instruction with an immediate offset. DstReg and TmpReg are |
| 281 | /// permitted to be the same register iff DstReg is distinct from BaseReg and |
| 282 | /// DstReg is a GPR. It is the callers responsibility to identify such cases |
| 283 | /// and pass the appropriate register in TmpReg. |
Daniel Sanders | fba875f | 2016-04-29 13:43:45 +0000 | [diff] [blame] | 284 | void MipsTargetStreamer::emitLoadWithImmOffset(unsigned Opcode, unsigned DstReg, |
| 285 | unsigned BaseReg, int64_t Offset, |
| 286 | unsigned TmpReg, SMLoc IDLoc, |
| 287 | const MCSubtargetInfo *STI) { |
Daniel Sanders | 7225cd5 | 2016-04-29 16:16:49 +0000 | [diff] [blame] | 288 | if (isInt<16>(Offset)) { |
| 289 | emitRRI(Opcode, DstReg, BaseReg, Offset, IDLoc, STI); |
| 290 | return; |
| 291 | } |
| 292 | |
Daniel Sanders | fba875f | 2016-04-29 13:43:45 +0000 | [diff] [blame] | 293 | // 1) lw $8, offset($9) => lui $8, %hi(offset) |
| 294 | // add $8, $8, $9 |
| 295 | // lw $8, %lo(offset)($9) |
| 296 | // 2) lw $8, offset($8) => lui $at, %hi(offset) |
| 297 | // add $at, $at, $8 |
| 298 | // lw $8, %lo(offset)($at) |
| 299 | |
| 300 | unsigned LoOffset = Offset & 0x0000ffff; |
| 301 | unsigned HiOffset = (Offset & 0xffff0000) >> 16; |
| 302 | |
| 303 | // If msb of LoOffset is 1(negative number) we must increment HiOffset |
| 304 | // to account for the sign-extension of the low part. |
| 305 | if (LoOffset & 0x8000) |
| 306 | HiOffset++; |
| 307 | |
| 308 | // Generate the base address in TmpReg. |
| 309 | emitRI(Mips::LUi, TmpReg, HiOffset, IDLoc, STI); |
| 310 | if (BaseReg != Mips::ZERO) |
| 311 | emitRRR(Mips::ADDu, TmpReg, TmpReg, BaseReg, IDLoc, STI); |
| 312 | // Emit the load with the adjusted base and offset. |
| 313 | emitRRI(Opcode, DstReg, TmpReg, LoOffset, IDLoc, STI); |
| 314 | } |
| 315 | |
| 316 | /// Emit a load instruction with an symbol offset. Symbols are assumed to be |
| 317 | /// out of range for a simm16 will be expanded to appropriate instructions. |
| 318 | /// DstReg and TmpReg are permitted to be the same register iff DstReg is a |
| 319 | /// GPR. It is the callers responsibility to identify such cases and pass the |
| 320 | /// appropriate register in TmpReg. |
| 321 | void MipsTargetStreamer::emitLoadWithSymOffset(unsigned Opcode, unsigned DstReg, |
| 322 | unsigned BaseReg, |
| 323 | MCOperand &HiOperand, |
| 324 | MCOperand &LoOperand, |
| 325 | unsigned TmpReg, SMLoc IDLoc, |
| 326 | const MCSubtargetInfo *STI) { |
| 327 | // 1) lw $8, sym => lui $8, %hi(sym) |
| 328 | // lw $8, %lo(sym)($8) |
| 329 | // 2) ldc1 $f0, sym => lui $at, %hi(sym) |
| 330 | // ldc1 $f0, %lo(sym)($at) |
| 331 | |
| 332 | // Generate the base address in TmpReg. |
| 333 | emitRX(Mips::LUi, TmpReg, HiOperand, IDLoc, STI); |
| 334 | if (BaseReg != Mips::ZERO) |
| 335 | emitRRR(Mips::ADDu, TmpReg, TmpReg, BaseReg, IDLoc, STI); |
| 336 | // Emit the load with the adjusted base and offset. |
| 337 | emitRRX(Opcode, DstReg, TmpReg, LoOperand, IDLoc, STI); |
| 338 | } |
| 339 | |
Rafael Espindola | 24ea09e | 2014-01-26 06:06:37 +0000 | [diff] [blame] | 340 | MipsTargetAsmStreamer::MipsTargetAsmStreamer(MCStreamer &S, |
| 341 | formatted_raw_ostream &OS) |
| 342 | : MipsTargetStreamer(S), OS(OS) {} |
Jack Carter | 6ef6cc5 | 2013-11-19 20:53:28 +0000 | [diff] [blame] | 343 | |
Rafael Espindola | 6d5f7ce | 2014-01-14 04:25:13 +0000 | [diff] [blame] | 344 | void MipsTargetAsmStreamer::emitDirectiveSetMicroMips() { |
| 345 | OS << "\t.set\tmicromips\n"; |
Daniel Sanders | cdb45fa | 2014-08-14 09:18:14 +0000 | [diff] [blame] | 346 | forbidModuleDirective(); |
Jack Carter | 6ef6cc5 | 2013-11-19 20:53:28 +0000 | [diff] [blame] | 347 | } |
Rafael Espindola | 6d5f7ce | 2014-01-14 04:25:13 +0000 | [diff] [blame] | 348 | |
| 349 | void MipsTargetAsmStreamer::emitDirectiveSetNoMicroMips() { |
| 350 | OS << "\t.set\tnomicromips\n"; |
Daniel Sanders | cdb45fa | 2014-08-14 09:18:14 +0000 | [diff] [blame] | 351 | forbidModuleDirective(); |
Rafael Espindola | 6d5f7ce | 2014-01-14 04:25:13 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Rafael Espindola | 6633d57 | 2014-01-14 18:57:12 +0000 | [diff] [blame] | 354 | void MipsTargetAsmStreamer::emitDirectiveSetMips16() { |
| 355 | OS << "\t.set\tmips16\n"; |
Daniel Sanders | cdb45fa | 2014-08-14 09:18:14 +0000 | [diff] [blame] | 356 | forbidModuleDirective(); |
Rafael Espindola | 6633d57 | 2014-01-14 18:57:12 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | void MipsTargetAsmStreamer::emitDirectiveSetNoMips16() { |
| 360 | OS << "\t.set\tnomips16\n"; |
Toma Tabacu | 88f05ce | 2014-08-13 12:48:12 +0000 | [diff] [blame] | 361 | MipsTargetStreamer::emitDirectiveSetNoMips16(); |
Rafael Espindola | 6633d57 | 2014-01-14 18:57:12 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Rafael Espindola | eb0a8af | 2014-01-26 05:06:48 +0000 | [diff] [blame] | 364 | void MipsTargetAsmStreamer::emitDirectiveSetReorder() { |
| 365 | OS << "\t.set\treorder\n"; |
Toma Tabacu | 88f05ce | 2014-08-13 12:48:12 +0000 | [diff] [blame] | 366 | MipsTargetStreamer::emitDirectiveSetReorder(); |
Rafael Espindola | eb0a8af | 2014-01-26 05:06:48 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | void MipsTargetAsmStreamer::emitDirectiveSetNoReorder() { |
| 370 | OS << "\t.set\tnoreorder\n"; |
Daniel Sanders | cdb45fa | 2014-08-14 09:18:14 +0000 | [diff] [blame] | 371 | forbidModuleDirective(); |
Rafael Espindola | eb0a8af | 2014-01-26 05:06:48 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | void MipsTargetAsmStreamer::emitDirectiveSetMacro() { |
| 375 | OS << "\t.set\tmacro\n"; |
Toma Tabacu | 88f05ce | 2014-08-13 12:48:12 +0000 | [diff] [blame] | 376 | MipsTargetStreamer::emitDirectiveSetMacro(); |
Rafael Espindola | eb0a8af | 2014-01-26 05:06:48 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | void MipsTargetAsmStreamer::emitDirectiveSetNoMacro() { |
| 380 | OS << "\t.set\tnomacro\n"; |
Toma Tabacu | 88f05ce | 2014-08-13 12:48:12 +0000 | [diff] [blame] | 381 | MipsTargetStreamer::emitDirectiveSetNoMacro(); |
Rafael Espindola | eb0a8af | 2014-01-26 05:06:48 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Daniel Sanders | 4493443 | 2014-08-07 12:03:36 +0000 | [diff] [blame] | 384 | void MipsTargetAsmStreamer::emitDirectiveSetMsa() { |
| 385 | OS << "\t.set\tmsa\n"; |
| 386 | MipsTargetStreamer::emitDirectiveSetMsa(); |
| 387 | } |
| 388 | |
| 389 | void MipsTargetAsmStreamer::emitDirectiveSetNoMsa() { |
| 390 | OS << "\t.set\tnomsa\n"; |
| 391 | MipsTargetStreamer::emitDirectiveSetNoMsa(); |
| 392 | } |
| 393 | |
Rafael Espindola | eb0a8af | 2014-01-26 05:06:48 +0000 | [diff] [blame] | 394 | void MipsTargetAsmStreamer::emitDirectiveSetAt() { |
| 395 | OS << "\t.set\tat\n"; |
Toma Tabacu | 88f05ce | 2014-08-13 12:48:12 +0000 | [diff] [blame] | 396 | MipsTargetStreamer::emitDirectiveSetAt(); |
Rafael Espindola | eb0a8af | 2014-01-26 05:06:48 +0000 | [diff] [blame] | 397 | } |
| 398 | |
Toma Tabacu | 16a7449 | 2015-02-13 10:30:57 +0000 | [diff] [blame] | 399 | void MipsTargetAsmStreamer::emitDirectiveSetAtWithArg(unsigned RegNo) { |
| 400 | OS << "\t.set\tat=$" << Twine(RegNo) << "\n"; |
| 401 | MipsTargetStreamer::emitDirectiveSetAtWithArg(RegNo); |
| 402 | } |
| 403 | |
Rafael Espindola | eb0a8af | 2014-01-26 05:06:48 +0000 | [diff] [blame] | 404 | void MipsTargetAsmStreamer::emitDirectiveSetNoAt() { |
| 405 | OS << "\t.set\tnoat\n"; |
Toma Tabacu | 88f05ce | 2014-08-13 12:48:12 +0000 | [diff] [blame] | 406 | MipsTargetStreamer::emitDirectiveSetNoAt(); |
Rafael Espindola | eb0a8af | 2014-01-26 05:06:48 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | void MipsTargetAsmStreamer::emitDirectiveEnd(StringRef Name) { |
| 410 | OS << "\t.end\t" << Name << '\n'; |
| 411 | } |
| 412 | |
Rafael Espindola | 6633d57 | 2014-01-14 18:57:12 +0000 | [diff] [blame] | 413 | void MipsTargetAsmStreamer::emitDirectiveEnt(const MCSymbol &Symbol) { |
| 414 | OS << "\t.ent\t" << Symbol.getName() << '\n'; |
| 415 | } |
| 416 | |
Jack Carter | 0cd3c19 | 2014-01-06 23:27:31 +0000 | [diff] [blame] | 417 | void MipsTargetAsmStreamer::emitDirectiveAbiCalls() { OS << "\t.abicalls\n"; } |
Matheus Almeida | 0051f2d | 2014-04-16 15:48:55 +0000 | [diff] [blame] | 418 | |
| 419 | void MipsTargetAsmStreamer::emitDirectiveNaN2008() { OS << "\t.nan\t2008\n"; } |
| 420 | |
| 421 | void MipsTargetAsmStreamer::emitDirectiveNaNLegacy() { |
| 422 | OS << "\t.nan\tlegacy\n"; |
| 423 | } |
| 424 | |
Jack Carter | 0cd3c19 | 2014-01-06 23:27:31 +0000 | [diff] [blame] | 425 | void MipsTargetAsmStreamer::emitDirectiveOptionPic0() { |
| 426 | OS << "\t.option\tpic0\n"; |
| 427 | } |
| 428 | |
Matheus Almeida | f79b281 | 2014-03-26 13:40:29 +0000 | [diff] [blame] | 429 | void MipsTargetAsmStreamer::emitDirectiveOptionPic2() { |
| 430 | OS << "\t.option\tpic2\n"; |
| 431 | } |
| 432 | |
Toma Tabacu | 9ca5096 | 2015-04-16 09:53:47 +0000 | [diff] [blame] | 433 | void MipsTargetAsmStreamer::emitDirectiveInsn() { |
| 434 | MipsTargetStreamer::emitDirectiveInsn(); |
| 435 | OS << "\t.insn\n"; |
| 436 | } |
| 437 | |
Rafael Espindola | 054234f | 2014-01-27 03:53:56 +0000 | [diff] [blame] | 438 | void MipsTargetAsmStreamer::emitFrame(unsigned StackReg, unsigned StackSize, |
| 439 | unsigned ReturnReg) { |
| 440 | OS << "\t.frame\t$" |
| 441 | << StringRef(MipsInstPrinter::getRegisterName(StackReg)).lower() << "," |
| 442 | << StackSize << ",$" |
Rafael Espindola | 25fa291 | 2014-01-27 04:33:11 +0000 | [diff] [blame] | 443 | << StringRef(MipsInstPrinter::getRegisterName(ReturnReg)).lower() << '\n'; |
| 444 | } |
| 445 | |
Toma Tabacu | 85618b3 | 2014-08-19 14:22:52 +0000 | [diff] [blame] | 446 | void MipsTargetAsmStreamer::emitDirectiveSetArch(StringRef Arch) { |
| 447 | OS << "\t.set arch=" << Arch << "\n"; |
| 448 | MipsTargetStreamer::emitDirectiveSetArch(Arch); |
| 449 | } |
| 450 | |
Toma Tabacu | 4e0cf8e | 2015-03-06 12:15:12 +0000 | [diff] [blame] | 451 | void MipsTargetAsmStreamer::emitDirectiveSetMips0() { |
| 452 | OS << "\t.set\tmips0\n"; |
| 453 | MipsTargetStreamer::emitDirectiveSetMips0(); |
| 454 | } |
Toma Tabacu | 2664779 | 2014-09-09 12:52:14 +0000 | [diff] [blame] | 455 | |
Daniel Sanders | f0df221 | 2014-08-04 12:20:00 +0000 | [diff] [blame] | 456 | void MipsTargetAsmStreamer::emitDirectiveSetMips1() { |
| 457 | OS << "\t.set\tmips1\n"; |
Toma Tabacu | 88f05ce | 2014-08-13 12:48:12 +0000 | [diff] [blame] | 458 | MipsTargetStreamer::emitDirectiveSetMips1(); |
Daniel Sanders | f0df221 | 2014-08-04 12:20:00 +0000 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | void MipsTargetAsmStreamer::emitDirectiveSetMips2() { |
| 462 | OS << "\t.set\tmips2\n"; |
Toma Tabacu | 88f05ce | 2014-08-13 12:48:12 +0000 | [diff] [blame] | 463 | MipsTargetStreamer::emitDirectiveSetMips2(); |
Daniel Sanders | f0df221 | 2014-08-04 12:20:00 +0000 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | void MipsTargetAsmStreamer::emitDirectiveSetMips3() { |
| 467 | OS << "\t.set\tmips3\n"; |
Toma Tabacu | 88f05ce | 2014-08-13 12:48:12 +0000 | [diff] [blame] | 468 | MipsTargetStreamer::emitDirectiveSetMips3(); |
Daniel Sanders | f0df221 | 2014-08-04 12:20:00 +0000 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | void MipsTargetAsmStreamer::emitDirectiveSetMips4() { |
| 472 | OS << "\t.set\tmips4\n"; |
Toma Tabacu | 88f05ce | 2014-08-13 12:48:12 +0000 | [diff] [blame] | 473 | MipsTargetStreamer::emitDirectiveSetMips4(); |
Daniel Sanders | f0df221 | 2014-08-04 12:20:00 +0000 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | void MipsTargetAsmStreamer::emitDirectiveSetMips5() { |
| 477 | OS << "\t.set\tmips5\n"; |
Toma Tabacu | 88f05ce | 2014-08-13 12:48:12 +0000 | [diff] [blame] | 478 | MipsTargetStreamer::emitDirectiveSetMips5(); |
Daniel Sanders | f0df221 | 2014-08-04 12:20:00 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | void MipsTargetAsmStreamer::emitDirectiveSetMips32() { |
| 482 | OS << "\t.set\tmips32\n"; |
Toma Tabacu | 88f05ce | 2014-08-13 12:48:12 +0000 | [diff] [blame] | 483 | MipsTargetStreamer::emitDirectiveSetMips32(); |
Daniel Sanders | f0df221 | 2014-08-04 12:20:00 +0000 | [diff] [blame] | 484 | } |
| 485 | |
Vladimir Medic | 615b26e | 2014-03-04 09:54:09 +0000 | [diff] [blame] | 486 | void MipsTargetAsmStreamer::emitDirectiveSetMips32R2() { |
| 487 | OS << "\t.set\tmips32r2\n"; |
Toma Tabacu | 88f05ce | 2014-08-13 12:48:12 +0000 | [diff] [blame] | 488 | MipsTargetStreamer::emitDirectiveSetMips32R2(); |
Vladimir Medic | 615b26e | 2014-03-04 09:54:09 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Daniel Sanders | 1779314 | 2015-02-18 16:24:50 +0000 | [diff] [blame] | 491 | void MipsTargetAsmStreamer::emitDirectiveSetMips32R3() { |
| 492 | OS << "\t.set\tmips32r3\n"; |
| 493 | MipsTargetStreamer::emitDirectiveSetMips32R3(); |
| 494 | } |
| 495 | |
| 496 | void MipsTargetAsmStreamer::emitDirectiveSetMips32R5() { |
| 497 | OS << "\t.set\tmips32r5\n"; |
| 498 | MipsTargetStreamer::emitDirectiveSetMips32R5(); |
| 499 | } |
| 500 | |
Daniel Sanders | f0df221 | 2014-08-04 12:20:00 +0000 | [diff] [blame] | 501 | void MipsTargetAsmStreamer::emitDirectiveSetMips32R6() { |
| 502 | OS << "\t.set\tmips32r6\n"; |
Toma Tabacu | 88f05ce | 2014-08-13 12:48:12 +0000 | [diff] [blame] | 503 | MipsTargetStreamer::emitDirectiveSetMips32R6(); |
Daniel Sanders | f0df221 | 2014-08-04 12:20:00 +0000 | [diff] [blame] | 504 | } |
| 505 | |
Matheus Almeida | 3b9c63d | 2014-03-26 15:14:32 +0000 | [diff] [blame] | 506 | void MipsTargetAsmStreamer::emitDirectiveSetMips64() { |
| 507 | OS << "\t.set\tmips64\n"; |
Toma Tabacu | 88f05ce | 2014-08-13 12:48:12 +0000 | [diff] [blame] | 508 | MipsTargetStreamer::emitDirectiveSetMips64(); |
Matheus Almeida | 3b9c63d | 2014-03-26 15:14:32 +0000 | [diff] [blame] | 509 | } |
| 510 | |
Matheus Almeida | a2cd009 | 2014-03-26 14:52:22 +0000 | [diff] [blame] | 511 | void MipsTargetAsmStreamer::emitDirectiveSetMips64R2() { |
| 512 | OS << "\t.set\tmips64r2\n"; |
Toma Tabacu | 88f05ce | 2014-08-13 12:48:12 +0000 | [diff] [blame] | 513 | MipsTargetStreamer::emitDirectiveSetMips64R2(); |
Matheus Almeida | a2cd009 | 2014-03-26 14:52:22 +0000 | [diff] [blame] | 514 | } |
| 515 | |
Daniel Sanders | 1779314 | 2015-02-18 16:24:50 +0000 | [diff] [blame] | 516 | void MipsTargetAsmStreamer::emitDirectiveSetMips64R3() { |
| 517 | OS << "\t.set\tmips64r3\n"; |
| 518 | MipsTargetStreamer::emitDirectiveSetMips64R3(); |
| 519 | } |
| 520 | |
| 521 | void MipsTargetAsmStreamer::emitDirectiveSetMips64R5() { |
| 522 | OS << "\t.set\tmips64r5\n"; |
| 523 | MipsTargetStreamer::emitDirectiveSetMips64R5(); |
| 524 | } |
| 525 | |
Daniel Sanders | f0df221 | 2014-08-04 12:20:00 +0000 | [diff] [blame] | 526 | void MipsTargetAsmStreamer::emitDirectiveSetMips64R6() { |
| 527 | OS << "\t.set\tmips64r6\n"; |
Toma Tabacu | 88f05ce | 2014-08-13 12:48:12 +0000 | [diff] [blame] | 528 | MipsTargetStreamer::emitDirectiveSetMips64R6(); |
Daniel Sanders | f0df221 | 2014-08-04 12:20:00 +0000 | [diff] [blame] | 529 | } |
| 530 | |
Vladimir Medic | 27c398e | 2014-03-05 11:05:09 +0000 | [diff] [blame] | 531 | void MipsTargetAsmStreamer::emitDirectiveSetDsp() { |
| 532 | OS << "\t.set\tdsp\n"; |
Toma Tabacu | 88f05ce | 2014-08-13 12:48:12 +0000 | [diff] [blame] | 533 | MipsTargetStreamer::emitDirectiveSetDsp(); |
Vladimir Medic | 27c398e | 2014-03-05 11:05:09 +0000 | [diff] [blame] | 534 | } |
Toma Tabacu | 9db22db | 2014-09-09 10:15:38 +0000 | [diff] [blame] | 535 | |
Toma Tabacu | 351b2fe | 2014-09-17 09:01:54 +0000 | [diff] [blame] | 536 | void MipsTargetAsmStreamer::emitDirectiveSetNoDsp() { |
| 537 | OS << "\t.set\tnodsp\n"; |
| 538 | MipsTargetStreamer::emitDirectiveSetNoDsp(); |
| 539 | } |
| 540 | |
Toma Tabacu | 4e0cf8e | 2015-03-06 12:15:12 +0000 | [diff] [blame] | 541 | void MipsTargetAsmStreamer::emitDirectiveSetPop() { |
| 542 | OS << "\t.set\tpop\n"; |
| 543 | MipsTargetStreamer::emitDirectiveSetPop(); |
| 544 | } |
Toma Tabacu | 9db22db | 2014-09-09 10:15:38 +0000 | [diff] [blame] | 545 | |
Toma Tabacu | 4e0cf8e | 2015-03-06 12:15:12 +0000 | [diff] [blame] | 546 | void MipsTargetAsmStreamer::emitDirectiveSetPush() { |
| 547 | OS << "\t.set\tpush\n"; |
| 548 | MipsTargetStreamer::emitDirectiveSetPush(); |
| 549 | } |
Toma Tabacu | 9db22db | 2014-09-09 10:15:38 +0000 | [diff] [blame] | 550 | |
Toma Tabacu | 2969650 | 2015-06-02 09:48:04 +0000 | [diff] [blame] | 551 | void MipsTargetAsmStreamer::emitDirectiveSetSoftFloat() { |
| 552 | OS << "\t.set\tsoftfloat\n"; |
| 553 | MipsTargetStreamer::emitDirectiveSetSoftFloat(); |
| 554 | } |
| 555 | |
| 556 | void MipsTargetAsmStreamer::emitDirectiveSetHardFloat() { |
| 557 | OS << "\t.set\thardfloat\n"; |
| 558 | MipsTargetStreamer::emitDirectiveSetHardFloat(); |
| 559 | } |
| 560 | |
Rafael Espindola | 25fa291 | 2014-01-27 04:33:11 +0000 | [diff] [blame] | 561 | // Print a 32 bit hex number with all numbers. |
| 562 | static void printHex32(unsigned Value, raw_ostream &OS) { |
| 563 | OS << "0x"; |
| 564 | for (int i = 7; i >= 0; i--) |
Vladimir Medic | fb8a2a9 | 2014-07-08 08:59:22 +0000 | [diff] [blame] | 565 | OS.write_hex((Value & (0xF << (i * 4))) >> (i * 4)); |
Rafael Espindola | 25fa291 | 2014-01-27 04:33:11 +0000 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | void MipsTargetAsmStreamer::emitMask(unsigned CPUBitmask, |
| 569 | int CPUTopSavedRegOff) { |
| 570 | OS << "\t.mask \t"; |
| 571 | printHex32(CPUBitmask, OS); |
| 572 | OS << ',' << CPUTopSavedRegOff << '\n'; |
| 573 | } |
| 574 | |
| 575 | void MipsTargetAsmStreamer::emitFMask(unsigned FPUBitmask, |
| 576 | int FPUTopSavedRegOff) { |
| 577 | OS << "\t.fmask\t"; |
| 578 | printHex32(FPUBitmask, OS); |
| 579 | OS << "," << FPUTopSavedRegOff << '\n'; |
Rafael Espindola | 054234f | 2014-01-27 03:53:56 +0000 | [diff] [blame] | 580 | } |
| 581 | |
Toma Tabacu | c4c202a | 2014-10-01 14:53:19 +0000 | [diff] [blame] | 582 | void MipsTargetAsmStreamer::emitDirectiveCpLoad(unsigned RegNo) { |
Matheus Almeida | 525bc4f | 2014-04-30 11:28:42 +0000 | [diff] [blame] | 583 | OS << "\t.cpload\t$" |
| 584 | << StringRef(MipsInstPrinter::getRegisterName(RegNo)).lower() << "\n"; |
Daniel Sanders | cdb45fa | 2014-08-14 09:18:14 +0000 | [diff] [blame] | 585 | forbidModuleDirective(); |
Matheus Almeida | 525bc4f | 2014-04-30 11:28:42 +0000 | [diff] [blame] | 586 | } |
| 587 | |
Daniel Sanders | df8510d | 2016-05-11 12:48:19 +0000 | [diff] [blame] | 588 | bool MipsTargetAsmStreamer::emitDirectiveCpRestore( |
Benjamin Kramer | d3f4c05 | 2016-06-12 16:13:55 +0000 | [diff] [blame] | 589 | int Offset, function_ref<unsigned()> GetATReg, SMLoc IDLoc, |
Daniel Sanders | df8510d | 2016-05-11 12:48:19 +0000 | [diff] [blame] | 590 | const MCSubtargetInfo *STI) { |
| 591 | MipsTargetStreamer::emitDirectiveCpRestore(Offset, GetATReg, IDLoc, STI); |
Daniel Sanders | e2982ad | 2015-09-17 16:08:39 +0000 | [diff] [blame] | 592 | OS << "\t.cprestore\t" << Offset << "\n"; |
Daniel Sanders | df8510d | 2016-05-11 12:48:19 +0000 | [diff] [blame] | 593 | return true; |
Daniel Sanders | e2982ad | 2015-09-17 16:08:39 +0000 | [diff] [blame] | 594 | } |
| 595 | |
Matheus Almeida | d92a3fa | 2014-05-01 10:24:46 +0000 | [diff] [blame] | 596 | void MipsTargetAsmStreamer::emitDirectiveCpsetup(unsigned RegNo, |
| 597 | int RegOrOffset, |
| 598 | const MCSymbol &Sym, |
| 599 | bool IsReg) { |
| 600 | OS << "\t.cpsetup\t$" |
| 601 | << StringRef(MipsInstPrinter::getRegisterName(RegNo)).lower() << ", "; |
| 602 | |
| 603 | if (IsReg) |
| 604 | OS << "$" |
| 605 | << StringRef(MipsInstPrinter::getRegisterName(RegOrOffset)).lower(); |
| 606 | else |
| 607 | OS << RegOrOffset; |
| 608 | |
| 609 | OS << ", "; |
| 610 | |
Daniel Sanders | 5d79628 | 2015-09-21 09:26:55 +0000 | [diff] [blame] | 611 | OS << Sym.getName(); |
Daniel Sanders | cdb45fa | 2014-08-14 09:18:14 +0000 | [diff] [blame] | 612 | forbidModuleDirective(); |
Vladimir Medic | fb8a2a9 | 2014-07-08 08:59:22 +0000 | [diff] [blame] | 613 | } |
| 614 | |
Daniel Sanders | f173dda | 2015-09-22 10:50:09 +0000 | [diff] [blame] | 615 | void MipsTargetAsmStreamer::emitDirectiveCpreturn(unsigned SaveLocation, |
| 616 | bool SaveLocationIsRegister) { |
| 617 | OS << "\t.cpreturn"; |
| 618 | forbidModuleDirective(); |
| 619 | } |
| 620 | |
Toma Tabacu | a64e540 | 2015-06-25 12:44:38 +0000 | [diff] [blame] | 621 | void MipsTargetAsmStreamer::emitDirectiveModuleFP() { |
Vladimir Medic | fb8a2a9 | 2014-07-08 08:59:22 +0000 | [diff] [blame] | 622 | OS << "\t.module\tfp="; |
Toma Tabacu | a64e540 | 2015-06-25 12:44:38 +0000 | [diff] [blame] | 623 | OS << ABIFlagsSection.getFpABIString(ABIFlagsSection.getFpABI()) << "\n"; |
Vladimir Medic | fb8a2a9 | 2014-07-08 08:59:22 +0000 | [diff] [blame] | 624 | } |
| 625 | |
Daniel Sanders | 7e52742 | 2014-07-10 13:38:23 +0000 | [diff] [blame] | 626 | void MipsTargetAsmStreamer::emitDirectiveSetFp( |
| 627 | MipsABIFlagsSection::FpABIKind Value) { |
Toma Tabacu | 4e0cf8e | 2015-03-06 12:15:12 +0000 | [diff] [blame] | 628 | MipsTargetStreamer::emitDirectiveSetFp(Value); |
| 629 | |
Vladimir Medic | fb8a2a9 | 2014-07-08 08:59:22 +0000 | [diff] [blame] | 630 | OS << "\t.set\tfp="; |
Daniel Sanders | 7e52742 | 2014-07-10 13:38:23 +0000 | [diff] [blame] | 631 | OS << ABIFlagsSection.getFpABIString(Value) << "\n"; |
Vladimir Medic | fb8a2a9 | 2014-07-08 08:59:22 +0000 | [diff] [blame] | 632 | } |
| 633 | |
Toma Tabacu | 3c49958 | 2015-06-25 10:56:57 +0000 | [diff] [blame] | 634 | void MipsTargetAsmStreamer::emitDirectiveModuleOddSPReg() { |
| 635 | MipsTargetStreamer::emitDirectiveModuleOddSPReg(); |
Daniel Sanders | 7e52742 | 2014-07-10 13:38:23 +0000 | [diff] [blame] | 636 | |
Toma Tabacu | 3c49958 | 2015-06-25 10:56:57 +0000 | [diff] [blame] | 637 | OS << "\t.module\t" << (ABIFlagsSection.OddSPReg ? "" : "no") << "oddspreg\n"; |
Daniel Sanders | 7e52742 | 2014-07-10 13:38:23 +0000 | [diff] [blame] | 638 | } |
| 639 | |
Toma Tabacu | 32c72aa | 2015-06-30 09:36:50 +0000 | [diff] [blame] | 640 | void MipsTargetAsmStreamer::emitDirectiveSetOddSPReg() { |
| 641 | MipsTargetStreamer::emitDirectiveSetOddSPReg(); |
| 642 | OS << "\t.set\toddspreg\n"; |
| 643 | } |
| 644 | |
| 645 | void MipsTargetAsmStreamer::emitDirectiveSetNoOddSPReg() { |
| 646 | MipsTargetStreamer::emitDirectiveSetNoOddSPReg(); |
| 647 | OS << "\t.set\tnooddspreg\n"; |
| 648 | } |
| 649 | |
Toma Tabacu | 0f09313 | 2015-06-30 13:46:03 +0000 | [diff] [blame] | 650 | void MipsTargetAsmStreamer::emitDirectiveModuleSoftFloat() { |
| 651 | OS << "\t.module\tsoftfloat\n"; |
| 652 | } |
| 653 | |
| 654 | void MipsTargetAsmStreamer::emitDirectiveModuleHardFloat() { |
| 655 | OS << "\t.module\thardfloat\n"; |
| 656 | } |
| 657 | |
Jack Carter | 0cd3c19 | 2014-01-06 23:27:31 +0000 | [diff] [blame] | 658 | // This part is for ELF object output. |
Rafael Espindola | cb1953f | 2014-01-26 06:57:13 +0000 | [diff] [blame] | 659 | MipsTargetELFStreamer::MipsTargetELFStreamer(MCStreamer &S, |
| 660 | const MCSubtargetInfo &STI) |
Rafael Espindola | 972e71a | 2014-01-31 23:10:26 +0000 | [diff] [blame] | 661 | : MipsTargetStreamer(S), MicroMipsEnabled(false), STI(STI) { |
Rafael Espindola | cb1953f | 2014-01-26 06:57:13 +0000 | [diff] [blame] | 662 | MCAssembler &MCA = getStreamer().getAssembler(); |
Daniel Sanders | 8de3d3c | 2016-05-06 14:37:24 +0000 | [diff] [blame] | 663 | |
| 664 | // It's possible that MCObjectFileInfo isn't fully initialized at this point |
| 665 | // due to an initialization order problem where LLVMTargetMachine creates the |
| 666 | // target streamer before TargetLoweringObjectFile calls |
| 667 | // InitializeMCObjectFileInfo. There doesn't seem to be a single place that |
| 668 | // covers all cases so this statement covers most cases and direct object |
| 669 | // emission must call setPic() once MCObjectFileInfo has been initialized. The |
| 670 | // cases we don't handle here are covered by MipsAsmPrinter. |
Rafael Espindola | 699281c | 2016-05-18 11:58:50 +0000 | [diff] [blame] | 671 | Pic = MCA.getContext().getObjectFileInfo()->isPositionIndependent(); |
Rafael Espindola | cb1953f | 2014-01-26 06:57:13 +0000 | [diff] [blame] | 672 | |
Michael Kuperstein | db0712f | 2015-05-26 10:47:10 +0000 | [diff] [blame] | 673 | const FeatureBitset &Features = STI.getFeatureBits(); |
Eric Christopher | a576281 | 2015-01-26 17:33:46 +0000 | [diff] [blame] | 674 | |
| 675 | // Set the header flags that we can in the constructor. |
| 676 | // FIXME: This is a fairly terrible hack. We set the rest |
| 677 | // of these in the destructor. The problem here is two-fold: |
| 678 | // |
| 679 | // a: Some of the eflags can be set/reset by directives. |
| 680 | // b: There aren't any usage paths that initialize the ABI |
| 681 | // pointer until after we initialize either an assembler |
| 682 | // or the target machine. |
| 683 | // We can fix this by making the target streamer construct |
| 684 | // the ABI, but this is fraught with wide ranging dependency |
| 685 | // issues as well. |
| 686 | unsigned EFlags = MCA.getELFHeaderEFlags(); |
Rafael Espindola | cb1953f | 2014-01-26 06:57:13 +0000 | [diff] [blame] | 687 | |
| 688 | // Architecture |
Michael Kuperstein | db0712f | 2015-05-26 10:47:10 +0000 | [diff] [blame] | 689 | if (Features[Mips::FeatureMips64r6]) |
Daniel Sanders | 950f48d | 2014-07-04 15:21:53 +0000 | [diff] [blame] | 690 | EFlags |= ELF::EF_MIPS_ARCH_64R6; |
Michael Kuperstein | db0712f | 2015-05-26 10:47:10 +0000 | [diff] [blame] | 691 | else if (Features[Mips::FeatureMips64r2] || |
| 692 | Features[Mips::FeatureMips64r3] || |
| 693 | Features[Mips::FeatureMips64r5]) |
Rafael Espindola | cb1953f | 2014-01-26 06:57:13 +0000 | [diff] [blame] | 694 | EFlags |= ELF::EF_MIPS_ARCH_64R2; |
Michael Kuperstein | db0712f | 2015-05-26 10:47:10 +0000 | [diff] [blame] | 695 | else if (Features[Mips::FeatureMips64]) |
Rafael Espindola | cb1953f | 2014-01-26 06:57:13 +0000 | [diff] [blame] | 696 | EFlags |= ELF::EF_MIPS_ARCH_64; |
Michael Kuperstein | db0712f | 2015-05-26 10:47:10 +0000 | [diff] [blame] | 697 | else if (Features[Mips::FeatureMips5]) |
Daniel Sanders | 950f48d | 2014-07-04 15:21:53 +0000 | [diff] [blame] | 698 | EFlags |= ELF::EF_MIPS_ARCH_5; |
Michael Kuperstein | db0712f | 2015-05-26 10:47:10 +0000 | [diff] [blame] | 699 | else if (Features[Mips::FeatureMips4]) |
Daniel Sanders | f7b3229 | 2014-04-03 12:13:36 +0000 | [diff] [blame] | 700 | EFlags |= ELF::EF_MIPS_ARCH_4; |
Michael Kuperstein | db0712f | 2015-05-26 10:47:10 +0000 | [diff] [blame] | 701 | else if (Features[Mips::FeatureMips3]) |
Daniel Sanders | 950f48d | 2014-07-04 15:21:53 +0000 | [diff] [blame] | 702 | EFlags |= ELF::EF_MIPS_ARCH_3; |
Michael Kuperstein | db0712f | 2015-05-26 10:47:10 +0000 | [diff] [blame] | 703 | else if (Features[Mips::FeatureMips32r6]) |
Daniel Sanders | 950f48d | 2014-07-04 15:21:53 +0000 | [diff] [blame] | 704 | EFlags |= ELF::EF_MIPS_ARCH_32R6; |
Michael Kuperstein | db0712f | 2015-05-26 10:47:10 +0000 | [diff] [blame] | 705 | else if (Features[Mips::FeatureMips32r2] || |
| 706 | Features[Mips::FeatureMips32r3] || |
| 707 | Features[Mips::FeatureMips32r5]) |
Rafael Espindola | cb1953f | 2014-01-26 06:57:13 +0000 | [diff] [blame] | 708 | EFlags |= ELF::EF_MIPS_ARCH_32R2; |
Michael Kuperstein | db0712f | 2015-05-26 10:47:10 +0000 | [diff] [blame] | 709 | else if (Features[Mips::FeatureMips32]) |
Rafael Espindola | cb1953f | 2014-01-26 06:57:13 +0000 | [diff] [blame] | 710 | EFlags |= ELF::EF_MIPS_ARCH_32; |
Michael Kuperstein | db0712f | 2015-05-26 10:47:10 +0000 | [diff] [blame] | 711 | else if (Features[Mips::FeatureMips2]) |
Daniel Sanders | 950f48d | 2014-07-04 15:21:53 +0000 | [diff] [blame] | 712 | EFlags |= ELF::EF_MIPS_ARCH_2; |
| 713 | else |
| 714 | EFlags |= ELF::EF_MIPS_ARCH_1; |
Rafael Espindola | cb1953f | 2014-01-26 06:57:13 +0000 | [diff] [blame] | 715 | |
Daniel Sanders | 415c159 | 2016-05-12 11:31:19 +0000 | [diff] [blame] | 716 | // Machine |
| 717 | if (Features[Mips::FeatureCnMips]) |
| 718 | EFlags |= ELF::EF_MIPS_MACH_OCTEON; |
| 719 | |
Matheus Almeida | 0051f2d | 2014-04-16 15:48:55 +0000 | [diff] [blame] | 720 | // Other options. |
Michael Kuperstein | db0712f | 2015-05-26 10:47:10 +0000 | [diff] [blame] | 721 | if (Features[Mips::FeatureNaN2008]) |
Matheus Almeida | 0051f2d | 2014-04-16 15:48:55 +0000 | [diff] [blame] | 722 | EFlags |= ELF::EF_MIPS_NAN2008; |
| 723 | |
Daniel Sanders | 16ec6c1 | 2014-07-17 09:52:56 +0000 | [diff] [blame] | 724 | // -mabicalls and -mplt are not implemented but we should act as if they were |
| 725 | // given. |
| 726 | EFlags |= ELF::EF_MIPS_CPIC; |
Daniel Sanders | 16ec6c1 | 2014-07-17 09:52:56 +0000 | [diff] [blame] | 727 | |
Rafael Espindola | cb1953f | 2014-01-26 06:57:13 +0000 | [diff] [blame] | 728 | MCA.setELFHeaderEFlags(EFlags); |
| 729 | } |
Jack Carter | 86ac5c1 | 2013-11-18 23:55:27 +0000 | [diff] [blame] | 730 | |
Rafael Espindola | 95fb9b9 | 2015-06-02 20:38:46 +0000 | [diff] [blame] | 731 | void MipsTargetELFStreamer::emitLabel(MCSymbol *S) { |
| 732 | auto *Symbol = cast<MCSymbolELF>(S); |
Rafael Espindola | 26e917c | 2014-01-15 03:07:12 +0000 | [diff] [blame] | 733 | if (!isMicroMipsEnabled()) |
| 734 | return; |
Rafael Espindola | c73aed1 | 2015-06-03 19:03:11 +0000 | [diff] [blame] | 735 | getStreamer().getAssembler().registerSymbol(*Symbol); |
Rafael Espindola | 95fb9b9 | 2015-06-02 20:38:46 +0000 | [diff] [blame] | 736 | uint8_t Type = Symbol->getType(); |
Rafael Espindola | 26e917c | 2014-01-15 03:07:12 +0000 | [diff] [blame] | 737 | if (Type != ELF::STT_FUNC) |
| 738 | return; |
| 739 | |
Rafael Espindola | 8c006ee | 2015-06-04 05:59:23 +0000 | [diff] [blame] | 740 | Symbol->setOther(ELF::STO_MIPS_MICROMIPS); |
Rafael Espindola | 6d5f7ce | 2014-01-14 04:25:13 +0000 | [diff] [blame] | 741 | } |
| 742 | |
Rafael Espindola | 972e71a | 2014-01-31 23:10:26 +0000 | [diff] [blame] | 743 | void MipsTargetELFStreamer::finish() { |
| 744 | MCAssembler &MCA = getStreamer().getAssembler(); |
Daniel Sanders | 68c3747 | 2014-07-21 13:30:55 +0000 | [diff] [blame] | 745 | const MCObjectFileInfo &OFI = *MCA.getContext().getObjectFileInfo(); |
Rafael Espindola | 972e71a | 2014-01-31 23:10:26 +0000 | [diff] [blame] | 746 | |
Daniel Sanders | 41ffa5d | 2014-07-14 15:05:51 +0000 | [diff] [blame] | 747 | // .bss, .text and .data are always at least 16-byte aligned. |
Rafael Espindola | 967d6a6 | 2015-05-21 21:02:35 +0000 | [diff] [blame] | 748 | MCSection &TextSection = *OFI.getTextSection(); |
Rafael Espindola | bb9a71c | 2015-05-26 15:07:25 +0000 | [diff] [blame] | 749 | MCA.registerSection(TextSection); |
Rafael Espindola | 967d6a6 | 2015-05-21 21:02:35 +0000 | [diff] [blame] | 750 | MCSection &DataSection = *OFI.getDataSection(); |
Rafael Espindola | bb9a71c | 2015-05-26 15:07:25 +0000 | [diff] [blame] | 751 | MCA.registerSection(DataSection); |
Rafael Espindola | 967d6a6 | 2015-05-21 21:02:35 +0000 | [diff] [blame] | 752 | MCSection &BSSSection = *OFI.getBSSSection(); |
Rafael Espindola | bb9a71c | 2015-05-26 15:07:25 +0000 | [diff] [blame] | 753 | MCA.registerSection(BSSSection); |
Daniel Sanders | 41ffa5d | 2014-07-14 15:05:51 +0000 | [diff] [blame] | 754 | |
Rafael Espindola | 967d6a6 | 2015-05-21 21:02:35 +0000 | [diff] [blame] | 755 | TextSection.setAlignment(std::max(16u, TextSection.getAlignment())); |
| 756 | DataSection.setAlignment(std::max(16u, DataSection.getAlignment())); |
| 757 | BSSSection.setAlignment(std::max(16u, BSSSection.getAlignment())); |
Daniel Sanders | 41ffa5d | 2014-07-14 15:05:51 +0000 | [diff] [blame] | 758 | |
Daniel Sanders | c07f06a | 2016-05-04 13:21:06 +0000 | [diff] [blame] | 759 | if (RoundSectionSizes) { |
| 760 | // Make sections sizes a multiple of the alignment. This is useful for |
| 761 | // verifying the output of IAS against the output of other assemblers but |
| 762 | // it's not necessary to produce a correct object and increases section |
| 763 | // size. |
| 764 | MCStreamer &OS = getStreamer(); |
| 765 | for (MCSection &S : MCA) { |
| 766 | MCSectionELF &Section = static_cast<MCSectionELF &>(S); |
Daniel Sanders | 9db710a | 2016-04-29 12:44:07 +0000 | [diff] [blame] | 767 | |
Daniel Sanders | c07f06a | 2016-05-04 13:21:06 +0000 | [diff] [blame] | 768 | unsigned Alignment = Section.getAlignment(); |
| 769 | if (Alignment) { |
| 770 | OS.SwitchSection(&Section); |
| 771 | if (Section.UseCodeAlign()) |
| 772 | OS.EmitCodeAlignment(Alignment, Alignment); |
| 773 | else |
| 774 | OS.EmitValueToAlignment(Alignment, 0, 1, Alignment); |
| 775 | } |
Daniel Sanders | 9db710a | 2016-04-29 12:44:07 +0000 | [diff] [blame] | 776 | } |
| 777 | } |
| 778 | |
Michael Kuperstein | db0712f | 2015-05-26 10:47:10 +0000 | [diff] [blame] | 779 | const FeatureBitset &Features = STI.getFeatureBits(); |
Eric Christopher | a576281 | 2015-01-26 17:33:46 +0000 | [diff] [blame] | 780 | |
| 781 | // Update e_header flags. See the FIXME and comment above in |
| 782 | // the constructor for a full rundown on this. |
| 783 | unsigned EFlags = MCA.getELFHeaderEFlags(); |
| 784 | |
| 785 | // ABI |
| 786 | // N64 does not require any ABI bits. |
| 787 | if (getABI().IsO32()) |
| 788 | EFlags |= ELF::EF_MIPS_ABI_O32; |
| 789 | else if (getABI().IsN32()) |
| 790 | EFlags |= ELF::EF_MIPS_ABI2; |
| 791 | |
Michael Kuperstein | db0712f | 2015-05-26 10:47:10 +0000 | [diff] [blame] | 792 | if (Features[Mips::FeatureGP64Bit]) { |
Eric Christopher | a576281 | 2015-01-26 17:33:46 +0000 | [diff] [blame] | 793 | if (getABI().IsO32()) |
| 794 | EFlags |= ELF::EF_MIPS_32BITMODE; /* Compatibility Mode */ |
Michael Kuperstein | db0712f | 2015-05-26 10:47:10 +0000 | [diff] [blame] | 795 | } else if (Features[Mips::FeatureMips64r2] || Features[Mips::FeatureMips64]) |
Eric Christopher | a576281 | 2015-01-26 17:33:46 +0000 | [diff] [blame] | 796 | EFlags |= ELF::EF_MIPS_32BITMODE; |
| 797 | |
| 798 | // If we've set the cpic eflag and we're n64, go ahead and set the pic |
| 799 | // one as well. |
| 800 | if (EFlags & ELF::EF_MIPS_CPIC && getABI().IsN64()) |
| 801 | EFlags |= ELF::EF_MIPS_PIC; |
| 802 | |
| 803 | MCA.setELFHeaderEFlags(EFlags); |
| 804 | |
Daniel Sanders | 68c3747 | 2014-07-21 13:30:55 +0000 | [diff] [blame] | 805 | // Emit all the option records. |
| 806 | // At the moment we are only emitting .Mips.options (ODK_REGINFO) and |
| 807 | // .reginfo. |
| 808 | MipsELFStreamer &MEF = static_cast<MipsELFStreamer &>(Streamer); |
| 809 | MEF.EmitMipsOptionRecords(); |
Rafael Espindola | 972e71a | 2014-01-31 23:10:26 +0000 | [diff] [blame] | 810 | |
Vladimir Medic | fb8a2a9 | 2014-07-08 08:59:22 +0000 | [diff] [blame] | 811 | emitMipsAbiFlags(); |
Rafael Espindola | 972e71a | 2014-01-31 23:10:26 +0000 | [diff] [blame] | 812 | } |
| 813 | |
Rafael Espindola | 95fb9b9 | 2015-06-02 20:38:46 +0000 | [diff] [blame] | 814 | void MipsTargetELFStreamer::emitAssignment(MCSymbol *S, const MCExpr *Value) { |
| 815 | auto *Symbol = cast<MCSymbolELF>(S); |
Zoran Jovanovic | 28221d8 | 2014-03-20 09:44:49 +0000 | [diff] [blame] | 816 | // If on rhs is micromips symbol then mark Symbol as microMips. |
| 817 | if (Value->getKind() != MCExpr::SymbolRef) |
| 818 | return; |
Rafael Espindola | 95fb9b9 | 2015-06-02 20:38:46 +0000 | [diff] [blame] | 819 | const auto &RhsSym = cast<MCSymbolELF>( |
| 820 | static_cast<const MCSymbolRefExpr *>(Value)->getSymbol()); |
Toma Tabacu | 2cc44f5 | 2015-04-16 13:37:32 +0000 | [diff] [blame] | 821 | |
Rafael Espindola | 8c006ee | 2015-06-04 05:59:23 +0000 | [diff] [blame] | 822 | if (!(RhsSym.getOther() & ELF::STO_MIPS_MICROMIPS)) |
Zoran Jovanovic | 28221d8 | 2014-03-20 09:44:49 +0000 | [diff] [blame] | 823 | return; |
| 824 | |
Rafael Espindola | 8c006ee | 2015-06-04 05:59:23 +0000 | [diff] [blame] | 825 | Symbol->setOther(ELF::STO_MIPS_MICROMIPS); |
Zoran Jovanovic | 28221d8 | 2014-03-20 09:44:49 +0000 | [diff] [blame] | 826 | } |
| 827 | |
Jack Carter | 86ac5c1 | 2013-11-18 23:55:27 +0000 | [diff] [blame] | 828 | MCELFStreamer &MipsTargetELFStreamer::getStreamer() { |
Rafael Espindola | 24ea09e | 2014-01-26 06:06:37 +0000 | [diff] [blame] | 829 | return static_cast<MCELFStreamer &>(Streamer); |
Jack Carter | 86ac5c1 | 2013-11-18 23:55:27 +0000 | [diff] [blame] | 830 | } |
| 831 | |
Rafael Espindola | 6d5f7ce | 2014-01-14 04:25:13 +0000 | [diff] [blame] | 832 | void MipsTargetELFStreamer::emitDirectiveSetMicroMips() { |
| 833 | MicroMipsEnabled = true; |
Daniel Sanders | cdb45fa | 2014-08-14 09:18:14 +0000 | [diff] [blame] | 834 | forbidModuleDirective(); |
Jack Carter | 86ac5c1 | 2013-11-18 23:55:27 +0000 | [diff] [blame] | 835 | } |
Rafael Espindola | 6d5f7ce | 2014-01-14 04:25:13 +0000 | [diff] [blame] | 836 | |
| 837 | void MipsTargetELFStreamer::emitDirectiveSetNoMicroMips() { |
| 838 | MicroMipsEnabled = false; |
Daniel Sanders | cdb45fa | 2014-08-14 09:18:14 +0000 | [diff] [blame] | 839 | forbidModuleDirective(); |
Rafael Espindola | 6d5f7ce | 2014-01-14 04:25:13 +0000 | [diff] [blame] | 840 | } |
| 841 | |
Daniel Sanders | cda908a | 2016-05-16 09:10:13 +0000 | [diff] [blame] | 842 | void MipsTargetELFStreamer::setUsesMicroMips() { |
| 843 | MCAssembler &MCA = getStreamer().getAssembler(); |
| 844 | unsigned Flags = MCA.getELFHeaderEFlags(); |
| 845 | Flags |= ELF::EF_MIPS_MICROMIPS; |
| 846 | MCA.setELFHeaderEFlags(Flags); |
| 847 | } |
| 848 | |
Rafael Espindola | 6633d57 | 2014-01-14 18:57:12 +0000 | [diff] [blame] | 849 | void MipsTargetELFStreamer::emitDirectiveSetMips16() { |
Rafael Espindola | e758375 | 2014-01-24 16:13:20 +0000 | [diff] [blame] | 850 | MCAssembler &MCA = getStreamer().getAssembler(); |
| 851 | unsigned Flags = MCA.getELFHeaderEFlags(); |
| 852 | Flags |= ELF::EF_MIPS_ARCH_ASE_M16; |
| 853 | MCA.setELFHeaderEFlags(Flags); |
Daniel Sanders | cdb45fa | 2014-08-14 09:18:14 +0000 | [diff] [blame] | 854 | forbidModuleDirective(); |
Rafael Espindola | 6633d57 | 2014-01-14 18:57:12 +0000 | [diff] [blame] | 855 | } |
| 856 | |
Rafael Espindola | eb0a8af | 2014-01-26 05:06:48 +0000 | [diff] [blame] | 857 | void MipsTargetELFStreamer::emitDirectiveSetNoReorder() { |
Rafael Espindola | cb1953f | 2014-01-26 06:57:13 +0000 | [diff] [blame] | 858 | MCAssembler &MCA = getStreamer().getAssembler(); |
| 859 | unsigned Flags = MCA.getELFHeaderEFlags(); |
| 860 | Flags |= ELF::EF_MIPS_NOREORDER; |
| 861 | MCA.setELFHeaderEFlags(Flags); |
Daniel Sanders | cdb45fa | 2014-08-14 09:18:14 +0000 | [diff] [blame] | 862 | forbidModuleDirective(); |
Rafael Espindola | eb0a8af | 2014-01-26 05:06:48 +0000 | [diff] [blame] | 863 | } |
| 864 | |
Rafael Espindola | eb0a8af | 2014-01-26 05:06:48 +0000 | [diff] [blame] | 865 | void MipsTargetELFStreamer::emitDirectiveEnd(StringRef Name) { |
Daniel Sanders | d97a634 | 2014-08-13 10:07:34 +0000 | [diff] [blame] | 866 | MCAssembler &MCA = getStreamer().getAssembler(); |
| 867 | MCContext &Context = MCA.getContext(); |
| 868 | MCStreamer &OS = getStreamer(); |
| 869 | |
Scott Egerton | 219fae9 | 2016-02-17 11:15:16 +0000 | [diff] [blame] | 870 | MCSectionELF *Sec = Context.getELFSection(".pdr", ELF::SHT_PROGBITS, 0); |
Daniel Sanders | d97a634 | 2014-08-13 10:07:34 +0000 | [diff] [blame] | 871 | |
Daniel Sanders | 2b56133 | 2015-11-23 16:08:03 +0000 | [diff] [blame] | 872 | MCSymbol *Sym = Context.getOrCreateSymbol(Name); |
Daniel Sanders | d97a634 | 2014-08-13 10:07:34 +0000 | [diff] [blame] | 873 | const MCSymbolRefExpr *ExprRef = |
Daniel Sanders | 2b56133 | 2015-11-23 16:08:03 +0000 | [diff] [blame] | 874 | MCSymbolRefExpr::create(Sym, MCSymbolRefExpr::VK_None, Context); |
Daniel Sanders | d97a634 | 2014-08-13 10:07:34 +0000 | [diff] [blame] | 875 | |
Rafael Espindola | bb9a71c | 2015-05-26 15:07:25 +0000 | [diff] [blame] | 876 | MCA.registerSection(*Sec); |
Rafael Espindola | 967d6a6 | 2015-05-21 21:02:35 +0000 | [diff] [blame] | 877 | Sec->setAlignment(4); |
Daniel Sanders | d97a634 | 2014-08-13 10:07:34 +0000 | [diff] [blame] | 878 | |
| 879 | OS.PushSection(); |
| 880 | |
| 881 | OS.SwitchSection(Sec); |
| 882 | |
| 883 | OS.EmitValueImpl(ExprRef, 4); |
| 884 | |
| 885 | OS.EmitIntValue(GPRInfoSet ? GPRBitMask : 0, 4); // reg_mask |
| 886 | OS.EmitIntValue(GPRInfoSet ? GPROffset : 0, 4); // reg_offset |
| 887 | |
| 888 | OS.EmitIntValue(FPRInfoSet ? FPRBitMask : 0, 4); // fpreg_mask |
| 889 | OS.EmitIntValue(FPRInfoSet ? FPROffset : 0, 4); // fpreg_offset |
| 890 | |
| 891 | OS.EmitIntValue(FrameInfoSet ? FrameOffset : 0, 4); // frame_offset |
| 892 | OS.EmitIntValue(FrameInfoSet ? FrameReg : 0, 4); // frame_reg |
| 893 | OS.EmitIntValue(FrameInfoSet ? ReturnReg : 0, 4); // return_reg |
| 894 | |
| 895 | // The .end directive marks the end of a procedure. Invalidate |
| 896 | // the information gathered up until this point. |
| 897 | GPRInfoSet = FPRInfoSet = FrameInfoSet = false; |
| 898 | |
| 899 | OS.PopSection(); |
Daniel Sanders | 2b56133 | 2015-11-23 16:08:03 +0000 | [diff] [blame] | 900 | |
| 901 | // .end also implicitly sets the size. |
| 902 | MCSymbol *CurPCSym = Context.createTempSymbol(); |
| 903 | OS.EmitLabel(CurPCSym); |
| 904 | const MCExpr *Size = MCBinaryExpr::createSub( |
| 905 | MCSymbolRefExpr::create(CurPCSym, MCSymbolRefExpr::VK_None, Context), |
| 906 | ExprRef, Context); |
| 907 | int64_t AbsSize; |
| 908 | if (!Size->evaluateAsAbsolute(AbsSize, MCA)) |
| 909 | llvm_unreachable("Function size must be evaluatable as absolute"); |
| 910 | Size = MCConstantExpr::create(AbsSize, Context); |
| 911 | static_cast<MCSymbolELF *>(Sym)->setSize(Size); |
Rafael Espindola | eb0a8af | 2014-01-26 05:06:48 +0000 | [diff] [blame] | 912 | } |
| 913 | |
Rafael Espindola | 6633d57 | 2014-01-14 18:57:12 +0000 | [diff] [blame] | 914 | void MipsTargetELFStreamer::emitDirectiveEnt(const MCSymbol &Symbol) { |
Daniel Sanders | d97a634 | 2014-08-13 10:07:34 +0000 | [diff] [blame] | 915 | GPRInfoSet = FPRInfoSet = FrameInfoSet = false; |
Daniel Sanders | 2b56133 | 2015-11-23 16:08:03 +0000 | [diff] [blame] | 916 | |
| 917 | // .ent also acts like an implicit '.type symbol, STT_FUNC' |
| 918 | static_cast<const MCSymbolELF &>(Symbol).setType(ELF::STT_FUNC); |
Rafael Espindola | 6633d57 | 2014-01-14 18:57:12 +0000 | [diff] [blame] | 919 | } |
| 920 | |
Jack Carter | 0cd3c19 | 2014-01-06 23:27:31 +0000 | [diff] [blame] | 921 | void MipsTargetELFStreamer::emitDirectiveAbiCalls() { |
| 922 | MCAssembler &MCA = getStreamer().getAssembler(); |
| 923 | unsigned Flags = MCA.getELFHeaderEFlags(); |
Rafael Espindola | cb1953f | 2014-01-26 06:57:13 +0000 | [diff] [blame] | 924 | Flags |= ELF::EF_MIPS_CPIC | ELF::EF_MIPS_PIC; |
Jack Carter | 0cd3c19 | 2014-01-06 23:27:31 +0000 | [diff] [blame] | 925 | MCA.setELFHeaderEFlags(Flags); |
| 926 | } |
Matheus Almeida | 0051f2d | 2014-04-16 15:48:55 +0000 | [diff] [blame] | 927 | |
| 928 | void MipsTargetELFStreamer::emitDirectiveNaN2008() { |
| 929 | MCAssembler &MCA = getStreamer().getAssembler(); |
| 930 | unsigned Flags = MCA.getELFHeaderEFlags(); |
| 931 | Flags |= ELF::EF_MIPS_NAN2008; |
| 932 | MCA.setELFHeaderEFlags(Flags); |
| 933 | } |
| 934 | |
| 935 | void MipsTargetELFStreamer::emitDirectiveNaNLegacy() { |
| 936 | MCAssembler &MCA = getStreamer().getAssembler(); |
| 937 | unsigned Flags = MCA.getELFHeaderEFlags(); |
| 938 | Flags &= ~ELF::EF_MIPS_NAN2008; |
| 939 | MCA.setELFHeaderEFlags(Flags); |
| 940 | } |
| 941 | |
Jack Carter | 0cd3c19 | 2014-01-06 23:27:31 +0000 | [diff] [blame] | 942 | void MipsTargetELFStreamer::emitDirectiveOptionPic0() { |
| 943 | MCAssembler &MCA = getStreamer().getAssembler(); |
| 944 | unsigned Flags = MCA.getELFHeaderEFlags(); |
Matheus Almeida | f79b281 | 2014-03-26 13:40:29 +0000 | [diff] [blame] | 945 | // This option overrides other PIC options like -KPIC. |
| 946 | Pic = false; |
Jack Carter | 0cd3c19 | 2014-01-06 23:27:31 +0000 | [diff] [blame] | 947 | Flags &= ~ELF::EF_MIPS_PIC; |
| 948 | MCA.setELFHeaderEFlags(Flags); |
| 949 | } |
Rafael Espindola | 054234f | 2014-01-27 03:53:56 +0000 | [diff] [blame] | 950 | |
Matheus Almeida | f79b281 | 2014-03-26 13:40:29 +0000 | [diff] [blame] | 951 | void MipsTargetELFStreamer::emitDirectiveOptionPic2() { |
| 952 | MCAssembler &MCA = getStreamer().getAssembler(); |
| 953 | unsigned Flags = MCA.getELFHeaderEFlags(); |
| 954 | Pic = true; |
| 955 | // NOTE: We are following the GAS behaviour here which means the directive |
| 956 | // 'pic2' also sets the CPIC bit in the ELF header. This is different from |
| 957 | // what is stated in the SYSV ABI which consider the bits EF_MIPS_PIC and |
| 958 | // EF_MIPS_CPIC to be mutually exclusive. |
| 959 | Flags |= ELF::EF_MIPS_PIC | ELF::EF_MIPS_CPIC; |
| 960 | MCA.setELFHeaderEFlags(Flags); |
| 961 | } |
| 962 | |
Toma Tabacu | 9ca5096 | 2015-04-16 09:53:47 +0000 | [diff] [blame] | 963 | void MipsTargetELFStreamer::emitDirectiveInsn() { |
| 964 | MipsTargetStreamer::emitDirectiveInsn(); |
| 965 | MipsELFStreamer &MEF = static_cast<MipsELFStreamer &>(Streamer); |
| 966 | MEF.createPendingLabelRelocs(); |
| 967 | } |
| 968 | |
Rafael Espindola | 054234f | 2014-01-27 03:53:56 +0000 | [diff] [blame] | 969 | void MipsTargetELFStreamer::emitFrame(unsigned StackReg, unsigned StackSize, |
Daniel Sanders | d97a634 | 2014-08-13 10:07:34 +0000 | [diff] [blame] | 970 | unsigned ReturnReg_) { |
| 971 | MCContext &Context = getStreamer().getAssembler().getContext(); |
| 972 | const MCRegisterInfo *RegInfo = Context.getRegisterInfo(); |
| 973 | |
| 974 | FrameInfoSet = true; |
| 975 | FrameReg = RegInfo->getEncodingValue(StackReg); |
| 976 | FrameOffset = StackSize; |
| 977 | ReturnReg = RegInfo->getEncodingValue(ReturnReg_); |
Rafael Espindola | 054234f | 2014-01-27 03:53:56 +0000 | [diff] [blame] | 978 | } |
Rafael Espindola | 25fa291 | 2014-01-27 04:33:11 +0000 | [diff] [blame] | 979 | |
| 980 | void MipsTargetELFStreamer::emitMask(unsigned CPUBitmask, |
| 981 | int CPUTopSavedRegOff) { |
Daniel Sanders | d97a634 | 2014-08-13 10:07:34 +0000 | [diff] [blame] | 982 | GPRInfoSet = true; |
| 983 | GPRBitMask = CPUBitmask; |
| 984 | GPROffset = CPUTopSavedRegOff; |
Rafael Espindola | 25fa291 | 2014-01-27 04:33:11 +0000 | [diff] [blame] | 985 | } |
| 986 | |
| 987 | void MipsTargetELFStreamer::emitFMask(unsigned FPUBitmask, |
| 988 | int FPUTopSavedRegOff) { |
Daniel Sanders | d97a634 | 2014-08-13 10:07:34 +0000 | [diff] [blame] | 989 | FPRInfoSet = true; |
| 990 | FPRBitMask = FPUBitmask; |
| 991 | FPROffset = FPUTopSavedRegOff; |
Rafael Espindola | 25fa291 | 2014-01-27 04:33:11 +0000 | [diff] [blame] | 992 | } |
Vladimir Medic | 615b26e | 2014-03-04 09:54:09 +0000 | [diff] [blame] | 993 | |
Toma Tabacu | c4c202a | 2014-10-01 14:53:19 +0000 | [diff] [blame] | 994 | void MipsTargetELFStreamer::emitDirectiveCpLoad(unsigned RegNo) { |
Matheus Almeida | 525bc4f | 2014-04-30 11:28:42 +0000 | [diff] [blame] | 995 | // .cpload $reg |
| 996 | // This directive expands to: |
| 997 | // lui $gp, %hi(_gp_disp) |
| 998 | // addui $gp, $gp, %lo(_gp_disp) |
| 999 | // addu $gp, $gp, $reg |
| 1000 | // when support for position independent code is enabled. |
Eric Christopher | a576281 | 2015-01-26 17:33:46 +0000 | [diff] [blame] | 1001 | if (!Pic || (getABI().IsN32() || getABI().IsN64())) |
Matheus Almeida | 525bc4f | 2014-04-30 11:28:42 +0000 | [diff] [blame] | 1002 | return; |
| 1003 | |
| 1004 | // There's a GNU extension controlled by -mno-shared that allows |
| 1005 | // locally-binding symbols to be accessed using absolute addresses. |
| 1006 | // This is currently not supported. When supported -mno-shared makes |
| 1007 | // .cpload expand to: |
| 1008 | // lui $gp, %hi(__gnu_local_gp) |
| 1009 | // addiu $gp, $gp, %lo(__gnu_local_gp) |
| 1010 | |
| 1011 | StringRef SymName("_gp_disp"); |
| 1012 | MCAssembler &MCA = getStreamer().getAssembler(); |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 1013 | MCSymbol *GP_Disp = MCA.getContext().getOrCreateSymbol(SymName); |
Rafael Espindola | b5d316b | 2015-05-29 20:21:02 +0000 | [diff] [blame] | 1014 | MCA.registerSymbol(*GP_Disp); |
Matheus Almeida | 525bc4f | 2014-04-30 11:28:42 +0000 | [diff] [blame] | 1015 | |
| 1016 | MCInst TmpInst; |
| 1017 | TmpInst.setOpcode(Mips::LUi); |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 1018 | TmpInst.addOperand(MCOperand::createReg(Mips::GP)); |
Daniel Sanders | fe98b2f | 2016-05-03 13:35:44 +0000 | [diff] [blame] | 1019 | const MCExpr *HiSym = MipsMCExpr::create( |
| 1020 | MipsMCExpr::MEK_HI, |
| 1021 | MCSymbolRefExpr::create("_gp_disp", MCSymbolRefExpr::VK_None, |
| 1022 | MCA.getContext()), |
| 1023 | MCA.getContext()); |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 1024 | TmpInst.addOperand(MCOperand::createExpr(HiSym)); |
Matheus Almeida | 525bc4f | 2014-04-30 11:28:42 +0000 | [diff] [blame] | 1025 | getStreamer().EmitInstruction(TmpInst, STI); |
| 1026 | |
| 1027 | TmpInst.clear(); |
| 1028 | |
| 1029 | TmpInst.setOpcode(Mips::ADDiu); |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 1030 | TmpInst.addOperand(MCOperand::createReg(Mips::GP)); |
| 1031 | TmpInst.addOperand(MCOperand::createReg(Mips::GP)); |
Daniel Sanders | fe98b2f | 2016-05-03 13:35:44 +0000 | [diff] [blame] | 1032 | const MCExpr *LoSym = MipsMCExpr::create( |
| 1033 | MipsMCExpr::MEK_LO, |
| 1034 | MCSymbolRefExpr::create("_gp_disp", MCSymbolRefExpr::VK_None, |
| 1035 | MCA.getContext()), |
| 1036 | MCA.getContext()); |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 1037 | TmpInst.addOperand(MCOperand::createExpr(LoSym)); |
Matheus Almeida | 525bc4f | 2014-04-30 11:28:42 +0000 | [diff] [blame] | 1038 | getStreamer().EmitInstruction(TmpInst, STI); |
| 1039 | |
| 1040 | TmpInst.clear(); |
| 1041 | |
| 1042 | TmpInst.setOpcode(Mips::ADDu); |
Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 1043 | TmpInst.addOperand(MCOperand::createReg(Mips::GP)); |
| 1044 | TmpInst.addOperand(MCOperand::createReg(Mips::GP)); |
| 1045 | TmpInst.addOperand(MCOperand::createReg(RegNo)); |
Matheus Almeida | 525bc4f | 2014-04-30 11:28:42 +0000 | [diff] [blame] | 1046 | getStreamer().EmitInstruction(TmpInst, STI); |
Vladimir Medic | fb8a2a9 | 2014-07-08 08:59:22 +0000 | [diff] [blame] | 1047 | |
Daniel Sanders | cdb45fa | 2014-08-14 09:18:14 +0000 | [diff] [blame] | 1048 | forbidModuleDirective(); |
Matheus Almeida | 525bc4f | 2014-04-30 11:28:42 +0000 | [diff] [blame] | 1049 | } |
Matheus Almeida | d92a3fa | 2014-05-01 10:24:46 +0000 | [diff] [blame] | 1050 | |
Daniel Sanders | df8510d | 2016-05-11 12:48:19 +0000 | [diff] [blame] | 1051 | bool MipsTargetELFStreamer::emitDirectiveCpRestore( |
Benjamin Kramer | d3f4c05 | 2016-06-12 16:13:55 +0000 | [diff] [blame] | 1052 | int Offset, function_ref<unsigned()> GetATReg, SMLoc IDLoc, |
Daniel Sanders | df8510d | 2016-05-11 12:48:19 +0000 | [diff] [blame] | 1053 | const MCSubtargetInfo *STI) { |
| 1054 | MipsTargetStreamer::emitDirectiveCpRestore(Offset, GetATReg, IDLoc, STI); |
Daniel Sanders | e2982ad | 2015-09-17 16:08:39 +0000 | [diff] [blame] | 1055 | // .cprestore offset |
| 1056 | // When PIC mode is enabled and the O32 ABI is used, this directive expands |
| 1057 | // to: |
| 1058 | // sw $gp, offset($sp) |
| 1059 | // and adds a corresponding LW after every JAL. |
| 1060 | |
| 1061 | // Note that .cprestore is ignored if used with the N32 and N64 ABIs or if it |
| 1062 | // is used in non-PIC mode. |
| 1063 | if (!Pic || (getABI().IsN32() || getABI().IsN64())) |
Daniel Sanders | df8510d | 2016-05-11 12:48:19 +0000 | [diff] [blame] | 1064 | return true; |
| 1065 | |
Daniel Sanders | 7225cd5 | 2016-04-29 16:16:49 +0000 | [diff] [blame] | 1066 | // Store the $gp on the stack. |
Daniel Sanders | 241c679 | 2016-05-12 14:01:50 +0000 | [diff] [blame] | 1067 | emitStoreWithImmOffset(Mips::SW, Mips::GP, Mips::SP, Offset, GetATReg, IDLoc, |
Daniel Sanders | 7225cd5 | 2016-04-29 16:16:49 +0000 | [diff] [blame] | 1068 | STI); |
Daniel Sanders | df8510d | 2016-05-11 12:48:19 +0000 | [diff] [blame] | 1069 | return true; |
Daniel Sanders | e2982ad | 2015-09-17 16:08:39 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
Matheus Almeida | d92a3fa | 2014-05-01 10:24:46 +0000 | [diff] [blame] | 1072 | void MipsTargetELFStreamer::emitDirectiveCpsetup(unsigned RegNo, |
| 1073 | int RegOrOffset, |
| 1074 | const MCSymbol &Sym, |
| 1075 | bool IsReg) { |
| 1076 | // Only N32 and N64 emit anything for .cpsetup iff PIC is set. |
Eric Christopher | a576281 | 2015-01-26 17:33:46 +0000 | [diff] [blame] | 1077 | if (!Pic || !(getABI().IsN32() || getABI().IsN64())) |
Matheus Almeida | d92a3fa | 2014-05-01 10:24:46 +0000 | [diff] [blame] | 1078 | return; |
| 1079 | |
Daniel Sanders | e858136 | 2016-06-14 10:13:47 +0000 | [diff] [blame] | 1080 | forbidModuleDirective(); |
| 1081 | |
Matheus Almeida | d92a3fa | 2014-05-01 10:24:46 +0000 | [diff] [blame] | 1082 | MCAssembler &MCA = getStreamer().getAssembler(); |
| 1083 | MCInst Inst; |
| 1084 | |
| 1085 | // Either store the old $gp in a register or on the stack |
| 1086 | if (IsReg) { |
| 1087 | // move $save, $gpreg |
Daniel Sanders | e858136 | 2016-06-14 10:13:47 +0000 | [diff] [blame] | 1088 | emitRRR(Mips::OR64, RegOrOffset, Mips::GP, Mips::ZERO, SMLoc(), &STI); |
Matheus Almeida | d92a3fa | 2014-05-01 10:24:46 +0000 | [diff] [blame] | 1089 | } else { |
| 1090 | // sd $gpreg, offset($sp) |
Daniel Sanders | e858136 | 2016-06-14 10:13:47 +0000 | [diff] [blame] | 1091 | emitRRI(Mips::SD, Mips::GP, Mips::SP, RegOrOffset, SMLoc(), &STI); |
Matheus Almeida | d92a3fa | 2014-05-01 10:24:46 +0000 | [diff] [blame] | 1092 | } |
Daniel Sanders | e858136 | 2016-06-14 10:13:47 +0000 | [diff] [blame] | 1093 | |
| 1094 | if (getABI().IsN32()) { |
| 1095 | MCSymbol *GPSym = MCA.getContext().getOrCreateSymbol("__gnu_local_gp"); |
| 1096 | const MipsMCExpr *HiExpr = MipsMCExpr::create( |
| 1097 | MipsMCExpr::MEK_HI, MCSymbolRefExpr::create(GPSym, MCA.getContext()), |
| 1098 | MCA.getContext()); |
| 1099 | const MipsMCExpr *LoExpr = MipsMCExpr::create( |
| 1100 | MipsMCExpr::MEK_LO, MCSymbolRefExpr::create(GPSym, MCA.getContext()), |
| 1101 | MCA.getContext()); |
| 1102 | |
| 1103 | // lui $gp, %hi(__gnu_local_gp) |
| 1104 | emitRX(Mips::LUi, Mips::GP, MCOperand::createExpr(HiExpr), SMLoc(), &STI); |
| 1105 | |
| 1106 | // addiu $gp, $gp, %lo(__gnu_local_gp) |
| 1107 | emitRRX(Mips::ADDiu, Mips::GP, Mips::GP, MCOperand::createExpr(LoExpr), |
| 1108 | SMLoc(), &STI); |
| 1109 | |
| 1110 | return; |
| 1111 | } |
Matheus Almeida | d92a3fa | 2014-05-01 10:24:46 +0000 | [diff] [blame] | 1112 | |
Daniel Sanders | fe98b2f | 2016-05-03 13:35:44 +0000 | [diff] [blame] | 1113 | const MipsMCExpr *HiExpr = MipsMCExpr::createGpOff( |
| 1114 | MipsMCExpr::MEK_HI, MCSymbolRefExpr::create(&Sym, MCA.getContext()), |
| 1115 | MCA.getContext()); |
| 1116 | const MipsMCExpr *LoExpr = MipsMCExpr::createGpOff( |
| 1117 | MipsMCExpr::MEK_LO, MCSymbolRefExpr::create(&Sym, MCA.getContext()), |
| 1118 | MCA.getContext()); |
Toma Tabacu | 8874eac | 2015-02-18 13:46:53 +0000 | [diff] [blame] | 1119 | |
Matheus Almeida | d92a3fa | 2014-05-01 10:24:46 +0000 | [diff] [blame] | 1120 | // lui $gp, %hi(%neg(%gp_rel(funcSym))) |
Daniel Sanders | e858136 | 2016-06-14 10:13:47 +0000 | [diff] [blame] | 1121 | emitRX(Mips::LUi, Mips::GP, MCOperand::createExpr(HiExpr), SMLoc(), &STI); |
Matheus Almeida | d92a3fa | 2014-05-01 10:24:46 +0000 | [diff] [blame] | 1122 | |
| 1123 | // addiu $gp, $gp, %lo(%neg(%gp_rel(funcSym))) |
Daniel Sanders | e858136 | 2016-06-14 10:13:47 +0000 | [diff] [blame] | 1124 | emitRRX(Mips::ADDiu, Mips::GP, Mips::GP, MCOperand::createExpr(LoExpr), |
| 1125 | SMLoc(), &STI); |
Matheus Almeida | d92a3fa | 2014-05-01 10:24:46 +0000 | [diff] [blame] | 1126 | |
| 1127 | // daddu $gp, $gp, $funcreg |
Daniel Sanders | e858136 | 2016-06-14 10:13:47 +0000 | [diff] [blame] | 1128 | emitRRR(Mips::DADDu, Mips::GP, Mips::GP, RegNo, SMLoc(), &STI); |
Vladimir Medic | fb8a2a9 | 2014-07-08 08:59:22 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
Daniel Sanders | f173dda | 2015-09-22 10:50:09 +0000 | [diff] [blame] | 1131 | void MipsTargetELFStreamer::emitDirectiveCpreturn(unsigned SaveLocation, |
| 1132 | bool SaveLocationIsRegister) { |
| 1133 | // Only N32 and N64 emit anything for .cpreturn iff PIC is set. |
| 1134 | if (!Pic || !(getABI().IsN32() || getABI().IsN64())) |
| 1135 | return; |
| 1136 | |
| 1137 | MCInst Inst; |
| 1138 | // Either restore the old $gp from a register or on the stack |
| 1139 | if (SaveLocationIsRegister) { |
| 1140 | Inst.setOpcode(Mips::OR); |
| 1141 | Inst.addOperand(MCOperand::createReg(Mips::GP)); |
| 1142 | Inst.addOperand(MCOperand::createReg(SaveLocation)); |
| 1143 | Inst.addOperand(MCOperand::createReg(Mips::ZERO)); |
| 1144 | } else { |
| 1145 | Inst.setOpcode(Mips::LD); |
| 1146 | Inst.addOperand(MCOperand::createReg(Mips::GP)); |
| 1147 | Inst.addOperand(MCOperand::createReg(Mips::SP)); |
| 1148 | Inst.addOperand(MCOperand::createImm(SaveLocation)); |
| 1149 | } |
| 1150 | getStreamer().EmitInstruction(Inst, STI); |
| 1151 | |
| 1152 | forbidModuleDirective(); |
| 1153 | } |
| 1154 | |
Vladimir Medic | fb8a2a9 | 2014-07-08 08:59:22 +0000 | [diff] [blame] | 1155 | void MipsTargetELFStreamer::emitMipsAbiFlags() { |
| 1156 | MCAssembler &MCA = getStreamer().getAssembler(); |
| 1157 | MCContext &Context = MCA.getContext(); |
| 1158 | MCStreamer &OS = getStreamer(); |
Rafael Espindola | 0709a7b | 2015-05-21 19:20:38 +0000 | [diff] [blame] | 1159 | MCSectionELF *Sec = Context.getELFSection( |
Rafael Espindola | ba31e27 | 2015-01-29 17:33:21 +0000 | [diff] [blame] | 1160 | ".MIPS.abiflags", ELF::SHT_MIPS_ABIFLAGS, ELF::SHF_ALLOC, 24, ""); |
Rafael Espindola | bb9a71c | 2015-05-26 15:07:25 +0000 | [diff] [blame] | 1161 | MCA.registerSection(*Sec); |
Rafael Espindola | 967d6a6 | 2015-05-21 21:02:35 +0000 | [diff] [blame] | 1162 | Sec->setAlignment(8); |
Vladimir Medic | fb8a2a9 | 2014-07-08 08:59:22 +0000 | [diff] [blame] | 1163 | OS.SwitchSection(Sec); |
| 1164 | |
Daniel Sanders | c7dbc63 | 2014-07-08 10:11:38 +0000 | [diff] [blame] | 1165 | OS << ABIFlagsSection; |
Matheus Almeida | d92a3fa | 2014-05-01 10:24:46 +0000 | [diff] [blame] | 1166 | } |