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