OSDN Git Service

MC: Change MCAsmBackend::writeNopData() to take a raw_ostream instead of an MCObjectW...
[android-x86/external-llvm.git] / lib / Target / RISCV / MCTargetDesc / RISCVAsmBackend.cpp
1 //===-- RISCVAsmBackend.cpp - RISCV Assembler Backend ---------------------===//
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 #include "MCTargetDesc/RISCVFixupKinds.h"
11 #include "MCTargetDesc/RISCVMCTargetDesc.h"
12 #include "llvm/ADT/APInt.h"
13 #include "llvm/MC/MCAsmBackend.h"
14 #include "llvm/MC/MCAssembler.h"
15 #include "llvm/MC/MCContext.h"
16 #include "llvm/MC/MCDirectives.h"
17 #include "llvm/MC/MCELFObjectWriter.h"
18 #include "llvm/MC/MCExpr.h"
19 #include "llvm/MC/MCFixupKindInfo.h"
20 #include "llvm/MC/MCObjectWriter.h"
21 #include "llvm/MC/MCSubtargetInfo.h"
22 #include "llvm/MC/MCSymbol.h"
23 #include "llvm/Support/ErrorHandling.h"
24 #include "llvm/Support/raw_ostream.h"
25
26 using namespace llvm;
27
28 namespace {
29 class RISCVAsmBackend : public MCAsmBackend {
30   const MCSubtargetInfo &STI;
31   uint8_t OSABI;
32   bool Is64Bit;
33
34 public:
35   RISCVAsmBackend(const MCSubtargetInfo &STI, uint8_t OSABI, bool Is64Bit)
36       : MCAsmBackend(support::little), STI(STI), OSABI(OSABI),
37         Is64Bit(Is64Bit) {}
38   ~RISCVAsmBackend() override {}
39
40   void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
41                   const MCValue &Target, MutableArrayRef<char> Data,
42                   uint64_t Value, bool IsResolved) const override;
43
44   std::unique_ptr<MCObjectWriter>
45   createObjectWriter(raw_pwrite_stream &OS) const override;
46
47   // If linker relaxation is enabled, always emit relocations even if the fixup
48   // can be resolved. This is necessary for correctness as offsets may change
49   // during relaxation.
50   bool shouldForceRelocation(const MCAssembler &Asm, const MCFixup &Fixup,
51                              const MCValue &Target) override {
52     return STI.getFeatureBits()[RISCV::FeatureRelax];
53   }
54
55   bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
56                             const MCRelaxableFragment *DF,
57                             const MCAsmLayout &Layout) const override {
58     llvm_unreachable("Handled by fixupNeedsRelaxationAdvanced");
59   }
60
61   bool fixupNeedsRelaxationAdvanced(const MCFixup &Fixup, bool Resolved,
62                                     uint64_t Value,
63                                     const MCRelaxableFragment *DF,
64                                     const MCAsmLayout &Layout,
65                                     const bool WasForced) const override;
66
67   unsigned getNumFixupKinds() const override {
68     return RISCV::NumTargetFixupKinds;
69   }
70
71   const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override {
72     const static MCFixupKindInfo Infos[RISCV::NumTargetFixupKinds] = {
73       // This table *must* be in the order that the fixup_* kinds are defined in
74       // RISCVFixupKinds.h.
75       //
76       // name                      offset bits  flags
77       { "fixup_riscv_hi20",         12,     20,  0 },
78       { "fixup_riscv_lo12_i",       20,     12,  0 },
79       { "fixup_riscv_lo12_s",        0,     32,  0 },
80       { "fixup_riscv_pcrel_hi20",   12,     20,  MCFixupKindInfo::FKF_IsPCRel },
81       { "fixup_riscv_pcrel_lo12_i", 20,     12,  MCFixupKindInfo::FKF_IsPCRel },
82       { "fixup_riscv_pcrel_lo12_s",  0,     32,  MCFixupKindInfo::FKF_IsPCRel },
83       { "fixup_riscv_jal",          12,     20,  MCFixupKindInfo::FKF_IsPCRel },
84       { "fixup_riscv_branch",        0,     32,  MCFixupKindInfo::FKF_IsPCRel },
85       { "fixup_riscv_rvc_jump",      2,     11,  MCFixupKindInfo::FKF_IsPCRel },
86       { "fixup_riscv_rvc_branch",    0,     16,  MCFixupKindInfo::FKF_IsPCRel }
87     };
88
89     if (Kind < FirstTargetFixupKind)
90       return MCAsmBackend::getFixupKindInfo(Kind);
91
92     assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
93            "Invalid kind!");
94     return Infos[Kind - FirstTargetFixupKind];
95   }
96
97   bool mayNeedRelaxation(const MCInst &Inst) const override;
98   unsigned getRelaxedOpcode(unsigned Op) const;
99
100   void relaxInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
101                         MCInst &Res) const override;
102
103
104   bool writeNopData(raw_ostream &OS, uint64_t Count) const override;
105 };
106
107
108 bool RISCVAsmBackend::fixupNeedsRelaxationAdvanced(const MCFixup &Fixup,
109                                                    bool Resolved,
110                                                    uint64_t Value,
111                                                    const MCRelaxableFragment *DF,
112                                                    const MCAsmLayout &Layout,
113                                                    const bool WasForced) const {
114   // Return true if the symbol is actually unresolved.
115   // Resolved could be always false when shouldForceRelocation return true.
116   // We use !WasForced to indicate that the symbol is unresolved and not forced
117   // by shouldForceRelocation.
118   if (!Resolved && !WasForced)
119     return true;
120
121   int64_t Offset = int64_t(Value);
122   switch ((unsigned)Fixup.getKind()) {
123   default:
124     return false;
125   case RISCV::fixup_riscv_rvc_branch:
126     // For compressed branch instructions the immediate must be
127     // in the range [-256, 254].
128     return Offset > 254 || Offset < -256;
129   case RISCV::fixup_riscv_rvc_jump:
130     // For compressed jump instructions the immediate must be
131     // in the range [-2048, 2046].
132     return Offset > 2046 || Offset < -2048;
133   }
134 }
135
136 void RISCVAsmBackend::relaxInstruction(const MCInst &Inst,
137                                        const MCSubtargetInfo &STI,
138                                        MCInst &Res) const {
139   // TODO: replace this with call to auto generated uncompressinstr() function.
140   switch (Inst.getOpcode()) {
141   default:
142     llvm_unreachable("Opcode not expected!");
143   case RISCV::C_BEQZ:
144     // c.beqz $rs1, $imm -> beq $rs1, X0, $imm.
145     Res.setOpcode(RISCV::BEQ);
146     Res.addOperand(Inst.getOperand(0));
147     Res.addOperand(MCOperand::createReg(RISCV::X0));
148     Res.addOperand(Inst.getOperand(1));
149     break;
150   case RISCV::C_BNEZ:
151     // c.bnez $rs1, $imm -> bne $rs1, X0, $imm.
152     Res.setOpcode(RISCV::BNE);
153     Res.addOperand(Inst.getOperand(0));
154     Res.addOperand(MCOperand::createReg(RISCV::X0));
155     Res.addOperand(Inst.getOperand(1));
156     break;
157   case RISCV::C_J:
158     // c.j $imm -> jal X0, $imm.
159     Res.setOpcode(RISCV::JAL);
160     Res.addOperand(MCOperand::createReg(RISCV::X0));
161     Res.addOperand(Inst.getOperand(0));
162     break;
163   case RISCV::C_JAL:
164     // c.jal $imm -> jal X1, $imm.
165     Res.setOpcode(RISCV::JAL);
166     Res.addOperand(MCOperand::createReg(RISCV::X1));
167     Res.addOperand(Inst.getOperand(0));
168     break;
169   }
170 }
171
172 // Given a compressed control flow instruction this function returns
173 // the expanded instruction.
174 unsigned RISCVAsmBackend::getRelaxedOpcode(unsigned Op) const {
175   switch (Op) {
176   default:
177     return Op;
178   case RISCV::C_BEQZ:
179     return RISCV::BEQ;
180   case RISCV::C_BNEZ:
181     return RISCV::BNE;
182   case RISCV::C_J:
183   case RISCV::C_JAL: // fall through.
184     return RISCV::JAL;
185   }
186 }
187
188 bool RISCVAsmBackend::mayNeedRelaxation(const MCInst &Inst) const {
189   return getRelaxedOpcode(Inst.getOpcode()) != Inst.getOpcode();
190 }
191
192 bool RISCVAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count) const {
193   bool HasStdExtC = STI.getFeatureBits()[RISCV::FeatureStdExtC];
194   unsigned MinNopLen = HasStdExtC ? 2 : 4;
195
196   if ((Count % MinNopLen) != 0)
197     return false;
198
199   // The canonical nop on RISC-V is addi x0, x0, 0.
200   uint64_t Nop32Count = Count / 4;
201   for (uint64_t i = Nop32Count; i != 0; --i)
202     OS.write("\x13\0\0\0", 4);
203
204   // The canonical nop on RVC is c.nop.
205   if (HasStdExtC) {
206     uint64_t Nop16Count = (Count - Nop32Count * 4) / 2;
207     for (uint64_t i = Nop16Count; i != 0; --i)
208       OS.write("\x01\0", 2);
209   }
210
211   return true;
212 }
213
214 static uint64_t adjustFixupValue(const MCFixup &Fixup, uint64_t Value,
215                                  MCContext &Ctx) {
216   unsigned Kind = Fixup.getKind();
217   switch (Kind) {
218   default:
219     llvm_unreachable("Unknown fixup kind!");
220   case FK_Data_1:
221   case FK_Data_2:
222   case FK_Data_4:
223   case FK_Data_8:
224     return Value;
225   case RISCV::fixup_riscv_lo12_i:
226   case RISCV::fixup_riscv_pcrel_lo12_i:
227     return Value & 0xfff;
228   case RISCV::fixup_riscv_lo12_s:
229   case RISCV::fixup_riscv_pcrel_lo12_s:
230     return (((Value >> 5) & 0x7f) << 25) | ((Value & 0x1f) << 7);
231   case RISCV::fixup_riscv_hi20:
232   case RISCV::fixup_riscv_pcrel_hi20:
233     // Add 1 if bit 11 is 1, to compensate for low 12 bits being negative.
234     return ((Value + 0x800) >> 12) & 0xfffff;
235   case RISCV::fixup_riscv_jal: {
236     if (!isInt<21>(Value))
237       Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
238     if (Value & 0x1)
239       Ctx.reportError(Fixup.getLoc(), "fixup value must be 2-byte aligned");
240     // Need to produce imm[19|10:1|11|19:12] from the 21-bit Value.
241     unsigned Sbit = (Value >> 20) & 0x1;
242     unsigned Hi8 = (Value >> 12) & 0xff;
243     unsigned Mid1 = (Value >> 11) & 0x1;
244     unsigned Lo10 = (Value >> 1) & 0x3ff;
245     // Inst{31} = Sbit;
246     // Inst{30-21} = Lo10;
247     // Inst{20} = Mid1;
248     // Inst{19-12} = Hi8;
249     Value = (Sbit << 19) | (Lo10 << 9) | (Mid1 << 8) | Hi8;
250     return Value;
251   }
252   case RISCV::fixup_riscv_branch: {
253     if (!isInt<13>(Value))
254       Ctx.reportError(Fixup.getLoc(), "fixup value out of range");
255     if (Value & 0x1)
256       Ctx.reportError(Fixup.getLoc(), "fixup value must be 2-byte aligned");
257     // Need to extract imm[12], imm[10:5], imm[4:1], imm[11] from the 13-bit
258     // Value.
259     unsigned Sbit = (Value >> 12) & 0x1;
260     unsigned Hi1 = (Value >> 11) & 0x1;
261     unsigned Mid6 = (Value >> 5) & 0x3f;
262     unsigned Lo4 = (Value >> 1) & 0xf;
263     // Inst{31} = Sbit;
264     // Inst{30-25} = Mid6;
265     // Inst{11-8} = Lo4;
266     // Inst{7} = Hi1;
267     Value = (Sbit << 31) | (Mid6 << 25) | (Lo4 << 8) | (Hi1 << 7);
268     return Value;
269   }
270   case RISCV::fixup_riscv_rvc_jump: {
271     // Need to produce offset[11|4|9:8|10|6|7|3:1|5] from the 11-bit Value.
272     unsigned Bit11  = (Value >> 11) & 0x1;
273     unsigned Bit4   = (Value >> 4) & 0x1;
274     unsigned Bit9_8 = (Value >> 8) & 0x3;
275     unsigned Bit10  = (Value >> 10) & 0x1;
276     unsigned Bit6   = (Value >> 6) & 0x1;
277     unsigned Bit7   = (Value >> 7) & 0x1;
278     unsigned Bit3_1 = (Value >> 1) & 0x7;
279     unsigned Bit5   = (Value >> 5) & 0x1;
280     Value = (Bit11 << 10) | (Bit4 << 9) | (Bit9_8 << 7) | (Bit10 << 6) |
281             (Bit6 << 5) | (Bit7 << 4) | (Bit3_1 << 1) | Bit5;
282     return Value;
283   }
284   case RISCV::fixup_riscv_rvc_branch: {
285     // Need to produce offset[8|4:3], [reg 3 bit], offset[7:6|2:1|5]
286     unsigned Bit8   = (Value >> 8) & 0x1;
287     unsigned Bit7_6 = (Value >> 6) & 0x3;
288     unsigned Bit5   = (Value >> 5) & 0x1;
289     unsigned Bit4_3 = (Value >> 3) & 0x3;
290     unsigned Bit2_1 = (Value >> 1) & 0x3;
291     Value = (Bit8 << 12) | (Bit4_3 << 10) | (Bit7_6 << 5) | (Bit2_1 << 3) |
292             (Bit5 << 2);
293     return Value;
294   }
295
296   }
297 }
298
299 static unsigned getSize(unsigned Kind) {
300   switch (Kind) {
301   default:
302     return 4;
303   case RISCV::fixup_riscv_rvc_jump:
304   case RISCV::fixup_riscv_rvc_branch:
305     return 2;
306   }
307 }
308
309 void RISCVAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
310                                  const MCValue &Target,
311                                  MutableArrayRef<char> Data, uint64_t Value,
312                                  bool IsResolved) const {
313   MCContext &Ctx = Asm.getContext();
314   MCFixupKindInfo Info = getFixupKindInfo(Fixup.getKind());
315   if (!Value)
316     return; // Doesn't change encoding.
317   // Apply any target-specific value adjustments.
318   Value = adjustFixupValue(Fixup, Value, Ctx);
319
320   // Shift the value into position.
321   Value <<= Info.TargetOffset;
322
323   unsigned Offset = Fixup.getOffset();
324   unsigned FullSize = getSize(Fixup.getKind());
325
326 #ifndef NDEBUG
327   unsigned NumBytes = (Info.TargetSize + 7) / 8;
328   assert(Offset + NumBytes <= Data.size() && "Invalid fixup offset!");
329 #endif
330
331   // For each byte of the fragment that the fixup touches, mask in the
332   // bits from the fixup value.
333   for (unsigned i = 0; i != FullSize; ++i) {
334     Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff);
335   }
336 }
337
338 std::unique_ptr<MCObjectWriter>
339 RISCVAsmBackend::createObjectWriter(raw_pwrite_stream &OS) const {
340   return createRISCVELFObjectWriter(OS, OSABI, Is64Bit);
341 }
342
343 } // end anonymous namespace
344
345 MCAsmBackend *llvm::createRISCVAsmBackend(const Target &T,
346                                           const MCSubtargetInfo &STI,
347                                           const MCRegisterInfo &MRI,
348                                           const MCTargetOptions &Options) {
349   const Triple &TT = STI.getTargetTriple();
350   uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(TT.getOS());
351   return new RISCVAsmBackend(STI, OSABI, TT.isArch64Bit());
352 }