R600/SI: Use a custom encoding method for simm16 in SOPP branch instructions
[oota-llvm.git] / lib / Target / R600 / MCTargetDesc / AMDGPUAsmBackend.cpp
1 //===-- AMDGPUAsmBackend.cpp - AMDGPU 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 /// \file
9 //===----------------------------------------------------------------------===//
10
11 #include "MCTargetDesc/AMDGPUMCTargetDesc.h"
12 #include "MCTargetDesc/AMDGPUFixupKinds.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/MC/MCAsmBackend.h"
15 #include "llvm/MC/MCAssembler.h"
16 #include "llvm/MC/MCFixupKindInfo.h"
17 #include "llvm/MC/MCObjectWriter.h"
18 #include "llvm/MC/MCValue.h"
19 #include "llvm/Support/TargetRegistry.h"
20
21 using namespace llvm;
22
23 namespace {
24
25 class AMDGPUMCObjectWriter : public MCObjectWriter {
26 public:
27   AMDGPUMCObjectWriter(raw_ostream &OS) : MCObjectWriter(OS, true) { }
28   void ExecutePostLayoutBinding(MCAssembler &Asm,
29                                 const MCAsmLayout &Layout) override {
30     //XXX: Implement if necessary.
31   }
32   void RecordRelocation(const MCAssembler &Asm, const MCAsmLayout &Layout,
33                         const MCFragment *Fragment, const MCFixup &Fixup,
34                         MCValue Target, bool &IsPCRel,
35                         uint64_t &FixedValue) override {
36     assert(!"Not implemented");
37   }
38
39   void WriteObject(MCAssembler &Asm, const MCAsmLayout &Layout) override;
40
41 };
42
43 class AMDGPUAsmBackend : public MCAsmBackend {
44 public:
45   AMDGPUAsmBackend(const Target &T)
46     : MCAsmBackend() {}
47
48   unsigned getNumFixupKinds() const override { return 0; };
49   void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
50                   uint64_t Value, bool IsPCRel) const override;
51   bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
52                             const MCRelaxableFragment *DF,
53                             const MCAsmLayout &Layout) const override {
54     return false;
55   }
56   void relaxInstruction(const MCInst &Inst, MCInst &Res) const override {
57     assert(!"Not implemented");
58   }
59   bool mayNeedRelaxation(const MCInst &Inst) const override { return false; }
60   bool writeNopData(uint64_t Count, MCObjectWriter *OW) const override {
61     return true;
62   }
63
64   const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override;
65 };
66
67 } //End anonymous namespace
68
69 void AMDGPUMCObjectWriter::WriteObject(MCAssembler &Asm,
70                                        const MCAsmLayout &Layout) {
71   for (MCAssembler::iterator I = Asm.begin(), E = Asm.end(); I != E; ++I) {
72     Asm.writeSectionData(I, Layout);
73   }
74 }
75
76 void AMDGPUAsmBackend::applyFixup(const MCFixup &Fixup, char *Data,
77                                   unsigned DataSize, uint64_t Value,
78                                   bool IsPCRel) const {
79
80   uint16_t *Dst = (uint16_t*)(Data + Fixup.getOffset());
81   assert(Fixup.getKind() == FK_PCRel_4);
82   *Dst = (Value - 4) / 4;
83 }
84
85 const MCFixupKindInfo &AMDGPUAsmBackend::getFixupKindInfo(
86                                                        MCFixupKind Kind) const {
87   const static MCFixupKindInfo Infos[AMDGPU::NumTargetFixupKinds] = {
88     // name                   offset bits  flags
89     { "fixup_si_sopp_br",     0,     16,   MCFixupKindInfo::FKF_IsPCRel }
90   };
91
92   if (Kind < FirstTargetFixupKind)
93     return MCAsmBackend::getFixupKindInfo(Kind);
94
95   return Infos[Kind - FirstTargetFixupKind];
96 }
97
98 //===----------------------------------------------------------------------===//
99 // ELFAMDGPUAsmBackend class
100 //===----------------------------------------------------------------------===//
101
102 namespace {
103
104 class ELFAMDGPUAsmBackend : public AMDGPUAsmBackend {
105 public:
106   ELFAMDGPUAsmBackend(const Target &T) : AMDGPUAsmBackend(T) { }
107
108   MCObjectWriter *createObjectWriter(raw_ostream &OS) const override {
109     return createAMDGPUELFObjectWriter(OS);
110   }
111 };
112
113 } // end anonymous namespace
114
115 MCAsmBackend *llvm::createAMDGPUAsmBackend(const Target &T,
116                                            const MCRegisterInfo &MRI,
117                                            StringRef TT,
118                                            StringRef CPU) {
119   return new ELFAMDGPUAsmBackend(T);
120 }