1010//
1111// ===----------------------------------------------------------------------===//
1212
13- #include " CSKYMCCodeEmitter.h"
1413#include " CSKYMCAsmInfo.h"
14+ #include " MCTargetDesc/CSKYFixupKinds.h"
15+ #include " MCTargetDesc/CSKYMCAsmInfo.h"
1516#include " MCTargetDesc/CSKYMCTargetDesc.h"
1617#include " llvm/ADT/Statistic.h"
18+ #include " llvm/MC/MCCodeEmitter.h"
19+ #include " llvm/MC/MCContext.h"
1720#include " llvm/MC/MCInstBuilder.h"
1821#include " llvm/MC/MCInstrInfo.h"
1922#include " llvm/MC/MCRegisterInfo.h"
@@ -27,6 +30,186 @@ using namespace llvm;
2730
2831STATISTIC (MCNumEmitted, " Number of MC instructions emitted" );
2932
33+ static void addFixup (SmallVectorImpl<MCFixup> &Fixups, uint32_t Offset,
34+ const MCExpr *Value, uint16_t Kind) {
35+ bool PCRel = false ;
36+ switch (Kind) {
37+ case CSKY::Fixups::fixup_csky_pcrel_imm16_scale2:
38+ case CSKY::Fixups::fixup_csky_pcrel_uimm16_scale4:
39+ case CSKY::Fixups::fixup_csky_pcrel_imm26_scale2:
40+ case CSKY::Fixups::fixup_csky_pcrel_imm18_scale2:
41+ case CSKY::Fixups::fixup_csky_gotpc:
42+ case CSKY::Fixups::fixup_csky_pcrel_imm10_scale2:
43+ case CSKY::Fixups::fixup_csky_pcrel_uimm7_scale4:
44+ case CSKY::Fixups::fixup_csky_pcrel_uimm8_scale4:
45+ PCRel = true ;
46+ }
47+ Fixups.push_back (MCFixup::create (Offset, Value, Kind, PCRel));
48+ }
49+
50+ namespace {
51+ class CSKYMCCodeEmitter : public MCCodeEmitter {
52+ MCContext &Ctx;
53+ const MCInstrInfo &MII;
54+
55+ public:
56+ CSKYMCCodeEmitter (MCContext &Ctx, const MCInstrInfo &MII)
57+ : Ctx(Ctx), MII(MII) {}
58+
59+ ~CSKYMCCodeEmitter () {}
60+
61+ void encodeInstruction (const MCInst &Inst, SmallVectorImpl<char > &CB,
62+ SmallVectorImpl<MCFixup> &Fixups,
63+ const MCSubtargetInfo &STI) const override ;
64+
65+ // Generated by tablegen.
66+ uint64_t getBinaryCodeForInstr (const MCInst &MI,
67+ SmallVectorImpl<MCFixup> &Fixups,
68+ const MCSubtargetInfo &STI) const ;
69+
70+ // Default encoding method used by tablegen.
71+ unsigned getMachineOpValue (const MCInst &MI, const MCOperand &MO,
72+ SmallVectorImpl<MCFixup> &Fixups,
73+ const MCSubtargetInfo &STI) const ;
74+
75+ template <int shift = 0 >
76+ unsigned getImmOpValue (const MCInst &MI, unsigned Idx,
77+ SmallVectorImpl<MCFixup> &Fixups,
78+ const MCSubtargetInfo &STI) const {
79+ const MCOperand &MO = MI.getOperand (Idx);
80+ if (MO.isImm ())
81+ return (MO.getImm () >> shift);
82+
83+ assert (MO.isExpr () && " Unexpected MO type." );
84+
85+ MCFixupKind Kind = getTargetFixup (MO.getExpr ());
86+ addFixup (Fixups, 0 , MO.getExpr (), Kind);
87+ return 0 ;
88+ }
89+
90+ unsigned getRegSeqImmOpValue (const MCInst &MI, unsigned Idx,
91+ SmallVectorImpl<MCFixup> &Fixups,
92+ const MCSubtargetInfo &STI) const ;
93+
94+ unsigned getRegisterSeqOpValue (const MCInst &MI, unsigned Op,
95+ SmallVectorImpl<MCFixup> &Fixups,
96+ const MCSubtargetInfo &STI) const ;
97+
98+ unsigned getOImmOpValue (const MCInst &MI, unsigned Idx,
99+ SmallVectorImpl<MCFixup> &Fixups,
100+ const MCSubtargetInfo &STI) const ;
101+
102+ unsigned getImmOpValueIDLY (const MCInst &MI, unsigned Idx,
103+ SmallVectorImpl<MCFixup> &Fixups,
104+ const MCSubtargetInfo &STI) const ;
105+
106+ unsigned getImmJMPIX (const MCInst &MI, unsigned Idx,
107+ SmallVectorImpl<MCFixup> &Fixups,
108+ const MCSubtargetInfo &STI) const ;
109+
110+ unsigned getImmOpValueMSBSize (const MCInst &MI, unsigned Idx,
111+ SmallVectorImpl<MCFixup> &Fixups,
112+ const MCSubtargetInfo &STI) const ;
113+
114+ unsigned getImmShiftOpValue (const MCInst &MI, unsigned Idx,
115+ SmallVectorImpl<MCFixup> &Fixups,
116+ const MCSubtargetInfo &STI) const {
117+ const MCOperand &MO = MI.getOperand (Idx);
118+ assert (MO.isImm () && " Unexpected MO type." );
119+ return 1 << MO.getImm ();
120+ }
121+
122+ MCFixupKind getTargetFixup (const MCExpr *Expr) const ;
123+
124+ template <llvm::CSKY::Fixups FIXUP>
125+ unsigned getBranchSymbolOpValue (const MCInst &MI, unsigned Idx,
126+ SmallVectorImpl<MCFixup> &Fixups,
127+ const MCSubtargetInfo &STI) const {
128+ const MCOperand &MO = MI.getOperand (Idx);
129+
130+ if (MO.isImm ())
131+ return MO.getImm () >> 1 ;
132+
133+ assert (MO.isExpr () && " Unexpected MO type." );
134+
135+ MCFixupKind Kind = MCFixupKind (FIXUP);
136+ if (MO.getExpr ()->getKind () == MCExpr::Specifier)
137+ Kind = getTargetFixup (MO.getExpr ());
138+
139+ addFixup (Fixups, 0 , MO.getExpr (), Kind);
140+ return 0 ;
141+ }
142+
143+ template <llvm::CSKY::Fixups FIXUP>
144+ unsigned getConstpoolSymbolOpValue (const MCInst &MI, unsigned Idx,
145+ SmallVectorImpl<MCFixup> &Fixups,
146+ const MCSubtargetInfo &STI) const {
147+ const MCOperand &MO = MI.getOperand (Idx);
148+ assert (MO.isExpr () && " Unexpected MO type." );
149+
150+ MCFixupKind Kind = MCFixupKind (FIXUP);
151+ if (MO.getExpr ()->getKind () == MCExpr::Specifier)
152+ Kind = getTargetFixup (MO.getExpr ());
153+
154+ addFixup (Fixups, 0 , MO.getExpr (), Kind);
155+ return 0 ;
156+ }
157+
158+ template <llvm::CSKY::Fixups FIXUP>
159+ unsigned getDataSymbolOpValue (const MCInst &MI, unsigned Idx,
160+ SmallVectorImpl<MCFixup> &Fixups,
161+ const MCSubtargetInfo &STI) const {
162+ const MCOperand &MO = MI.getOperand (Idx);
163+ assert (MO.isExpr () && " Unexpected MO type." );
164+
165+ MCFixupKind Kind = MCFixupKind (FIXUP);
166+ if (MO.getExpr ()->getKind () == MCExpr::Specifier)
167+ Kind = getTargetFixup (MO.getExpr ());
168+
169+ addFixup (Fixups, 0 , MO.getExpr (), Kind);
170+ return 0 ;
171+ }
172+
173+ unsigned getCallSymbolOpValue (const MCInst &MI, unsigned Idx,
174+ SmallVectorImpl<MCFixup> &Fixups,
175+ const MCSubtargetInfo &STI) const {
176+ const MCOperand &MO = MI.getOperand (Idx);
177+ assert (MO.isExpr () && " Unexpected MO type." );
178+
179+ MCFixupKind Kind = MCFixupKind (CSKY::fixup_csky_pcrel_imm26_scale2);
180+ if (MO.getExpr ()->getKind () == MCExpr::Specifier)
181+ Kind = getTargetFixup (MO.getExpr ());
182+
183+ addFixup (Fixups, 0 , MO.getExpr (), Kind);
184+ return 0 ;
185+ }
186+
187+ unsigned getBareSymbolOpValue (const MCInst &MI, unsigned Idx,
188+ SmallVectorImpl<MCFixup> &Fixups,
189+ const MCSubtargetInfo &STI) const {
190+ const MCOperand &MO = MI.getOperand (Idx);
191+ assert (MO.isExpr () && " Unexpected MO type." );
192+
193+ MCFixupKind Kind = MCFixupKind (CSKY::fixup_csky_pcrel_imm18_scale2);
194+ if (MO.getExpr ()->getKind () == MCExpr::Specifier)
195+ Kind = getTargetFixup (MO.getExpr ());
196+
197+ addFixup (Fixups, 0 , MO.getExpr (), Kind);
198+ return 0 ;
199+ }
200+
201+ void expandJBTF (const MCInst &MI, SmallVectorImpl<char > &CB,
202+ SmallVectorImpl<MCFixup> &Fixups,
203+ const MCSubtargetInfo &STI) const ;
204+ void expandNEG (const MCInst &MI, SmallVectorImpl<char > &CB,
205+ SmallVectorImpl<MCFixup> &Fixups,
206+ const MCSubtargetInfo &STI) const ;
207+ void expandRSUBI (const MCInst &MI, SmallVectorImpl<char > &CB,
208+ SmallVectorImpl<MCFixup> &Fixups,
209+ const MCSubtargetInfo &STI) const ;
210+ };
211+ } // namespace
212+
30213unsigned CSKYMCCodeEmitter::getOImmOpValue (const MCInst &MI, unsigned Idx,
31214 SmallVectorImpl<MCFixup> &Fixups,
32215 const MCSubtargetInfo &STI) const {
0 commit comments