1 //===-- CSKYMCCodeEmitter.cpp - CSKY Code Emitter interface ---------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements the CSKYMCCodeEmitter class.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_LIB_TARGET_CSKY_MCTARGETDESC_CSKYMCCODEEMITTER_H
14 #define LLVM_LIB_TARGET_CSKY_MCTARGETDESC_CSKYMCCODEEMITTER_H
16 #include "CSKYMCExpr.h"
17 #include "MCTargetDesc/CSKYFixupKinds.h"
18 #include "llvm/MC/MCCodeEmitter.h"
19 #include "llvm/MC/MCContext.h"
23 class CSKYMCCodeEmitter
: public MCCodeEmitter
{
25 const MCInstrInfo
&MII
;
28 CSKYMCCodeEmitter(MCContext
&Ctx
, const MCInstrInfo
&MII
)
29 : Ctx(Ctx
), MII(MII
) {}
31 ~CSKYMCCodeEmitter() {}
33 void encodeInstruction(const MCInst
&Inst
, raw_ostream
&OS
,
34 SmallVectorImpl
<MCFixup
> &Fixups
,
35 const MCSubtargetInfo
&STI
) const override
;
37 // Generated by tablegen.
38 uint64_t getBinaryCodeForInstr(const MCInst
&MI
,
39 SmallVectorImpl
<MCFixup
> &Fixups
,
40 const MCSubtargetInfo
&STI
) const;
42 // Default encoding method used by tablegen.
43 unsigned getMachineOpValue(const MCInst
&MI
, const MCOperand
&MO
,
44 SmallVectorImpl
<MCFixup
> &Fixups
,
45 const MCSubtargetInfo
&STI
) const;
47 template <int shift
= 0>
48 unsigned getImmOpValue(const MCInst
&MI
, unsigned Idx
,
49 SmallVectorImpl
<MCFixup
> &Fixups
,
50 const MCSubtargetInfo
&STI
) const {
51 const MCOperand
&MO
= MI
.getOperand(Idx
);
52 assert(MO
.isImm() && "Unexpected MO type.");
53 return (MO
.getImm() >> shift
);
56 unsigned getOImmOpValue(const MCInst
&MI
, unsigned Idx
,
57 SmallVectorImpl
<MCFixup
> &Fixups
,
58 const MCSubtargetInfo
&STI
) const;
60 unsigned getImmShiftOpValue(const MCInst
&MI
, unsigned Idx
,
61 SmallVectorImpl
<MCFixup
> &Fixups
,
62 const MCSubtargetInfo
&STI
) const {
63 const MCOperand
&MO
= MI
.getOperand(Idx
);
64 assert(MO
.isImm() && "Unexpected MO type.");
65 return 1 << MO
.getImm();
68 MCFixupKind
getTargetFixup(const MCExpr
*Expr
) const;
70 template <llvm::CSKY::Fixups FIXUP
>
71 unsigned getBranchSymbolOpValue(const MCInst
&MI
, unsigned Idx
,
72 SmallVectorImpl
<MCFixup
> &Fixups
,
73 const MCSubtargetInfo
&STI
) const {
74 const MCOperand
&MO
= MI
.getOperand(Idx
);
77 return MO
.getImm() >> 1;
79 assert(MO
.isExpr() && "Unexpected MO type.");
81 MCFixupKind Kind
= MCFixupKind(FIXUP
);
82 if (MO
.getExpr()->getKind() == MCExpr::Target
)
83 Kind
= getTargetFixup(MO
.getExpr());
85 Fixups
.push_back(MCFixup::create(0, MO
.getExpr(), Kind
, MI
.getLoc()));
89 template <llvm::CSKY::Fixups FIXUP
>
90 unsigned getConstpoolSymbolOpValue(const MCInst
&MI
, unsigned Idx
,
91 SmallVectorImpl
<MCFixup
> &Fixups
,
92 const MCSubtargetInfo
&STI
) const {
93 const MCOperand
&MO
= MI
.getOperand(Idx
);
94 assert(MO
.isExpr() && "Unexpected MO type.");
96 MCFixupKind Kind
= MCFixupKind(FIXUP
);
97 if (MO
.getExpr()->getKind() == MCExpr::Target
)
98 Kind
= getTargetFixup(MO
.getExpr());
100 Fixups
.push_back(MCFixup::create(0, MO
.getExpr(), Kind
, MI
.getLoc()));
104 unsigned getCallSymbolOpValue(const MCInst
&MI
, unsigned Idx
,
105 SmallVectorImpl
<MCFixup
> &Fixups
,
106 const MCSubtargetInfo
&STI
) const {
107 const MCOperand
&MO
= MI
.getOperand(Idx
);
108 assert(MO
.isExpr() && "Unexpected MO type.");
110 MCFixupKind Kind
= MCFixupKind(CSKY::fixup_csky_pcrel_imm26_scale2
);
111 if (MO
.getExpr()->getKind() == MCExpr::Target
)
112 Kind
= getTargetFixup(MO
.getExpr());
114 Fixups
.push_back(MCFixup::create(0, MO
.getExpr(), Kind
, MI
.getLoc()));
118 unsigned getBareSymbolOpValue(const MCInst
&MI
, unsigned Idx
,
119 SmallVectorImpl
<MCFixup
> &Fixups
,
120 const MCSubtargetInfo
&STI
) const {
121 const MCOperand
&MO
= MI
.getOperand(Idx
);
122 assert(MO
.isExpr() && "Unexpected MO type.");
124 MCFixupKind Kind
= MCFixupKind(CSKY::fixup_csky_pcrel_imm18_scale2
);
125 if (MO
.getExpr()->getKind() == MCExpr::Target
)
126 Kind
= getTargetFixup(MO
.getExpr());
128 Fixups
.push_back(MCFixup::create(0, MO
.getExpr(), Kind
, MI
.getLoc()));
135 #endif // LLVM_LIB_TARGET_CSKY_MCTARGETDESC_CSKYMCCODEEMITTER_H