1e8d8bef9SDimitry Andric //===-- CSKYMCCodeEmitter.cpp - CSKY Code Emitter interface ---------------===// 2e8d8bef9SDimitry Andric // 3e8d8bef9SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4e8d8bef9SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5e8d8bef9SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6e8d8bef9SDimitry Andric // 7e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===// 8e8d8bef9SDimitry Andric // 9e8d8bef9SDimitry Andric // This file implements the CSKYMCCodeEmitter class. 10e8d8bef9SDimitry Andric // 11e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===// 12e8d8bef9SDimitry Andric 13e8d8bef9SDimitry Andric #ifndef LLVM_LIB_TARGET_CSKY_MCTARGETDESC_CSKYMCCODEEMITTER_H 14e8d8bef9SDimitry Andric #define LLVM_LIB_TARGET_CSKY_MCTARGETDESC_CSKYMCCODEEMITTER_H 15e8d8bef9SDimitry Andric 16fe6060f1SDimitry Andric #include "MCTargetDesc/CSKYFixupKinds.h" 17349cc55cSDimitry Andric #include "MCTargetDesc/CSKYMCExpr.h" 18e8d8bef9SDimitry Andric #include "llvm/MC/MCCodeEmitter.h" 19e8d8bef9SDimitry Andric #include "llvm/MC/MCContext.h" 20e8d8bef9SDimitry Andric 21e8d8bef9SDimitry Andric namespace llvm { 22e8d8bef9SDimitry Andric 2381ad6265SDimitry Andric class MCInstrInfo; 2481ad6265SDimitry Andric 25e8d8bef9SDimitry Andric class CSKYMCCodeEmitter : public MCCodeEmitter { 26e8d8bef9SDimitry Andric MCContext &Ctx; 27e8d8bef9SDimitry Andric const MCInstrInfo &MII; 28e8d8bef9SDimitry Andric 29e8d8bef9SDimitry Andric public: CSKYMCCodeEmitter(MCContext & Ctx,const MCInstrInfo & MII)30e8d8bef9SDimitry Andric CSKYMCCodeEmitter(MCContext &Ctx, const MCInstrInfo &MII) 31e8d8bef9SDimitry Andric : Ctx(Ctx), MII(MII) {} 32e8d8bef9SDimitry Andric ~CSKYMCCodeEmitter()33e8d8bef9SDimitry Andric ~CSKYMCCodeEmitter() {} 34e8d8bef9SDimitry Andric 35*5f757f3fSDimitry Andric void encodeInstruction(const MCInst &Inst, SmallVectorImpl<char> &CB, 36e8d8bef9SDimitry Andric SmallVectorImpl<MCFixup> &Fixups, 37e8d8bef9SDimitry Andric const MCSubtargetInfo &STI) const override; 38e8d8bef9SDimitry Andric 39e8d8bef9SDimitry Andric // Generated by tablegen. 40e8d8bef9SDimitry Andric uint64_t getBinaryCodeForInstr(const MCInst &MI, 41e8d8bef9SDimitry Andric SmallVectorImpl<MCFixup> &Fixups, 42e8d8bef9SDimitry Andric const MCSubtargetInfo &STI) const; 43e8d8bef9SDimitry Andric 44e8d8bef9SDimitry Andric // Default encoding method used by tablegen. 45e8d8bef9SDimitry Andric unsigned getMachineOpValue(const MCInst &MI, const MCOperand &MO, 46e8d8bef9SDimitry Andric SmallVectorImpl<MCFixup> &Fixups, 47e8d8bef9SDimitry Andric const MCSubtargetInfo &STI) const; 48e8d8bef9SDimitry Andric 49e8d8bef9SDimitry Andric template <int shift = 0> getImmOpValue(const MCInst & MI,unsigned Idx,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI)50e8d8bef9SDimitry Andric unsigned getImmOpValue(const MCInst &MI, unsigned Idx, 51e8d8bef9SDimitry Andric SmallVectorImpl<MCFixup> &Fixups, 52e8d8bef9SDimitry Andric const MCSubtargetInfo &STI) const { 53e8d8bef9SDimitry Andric const MCOperand &MO = MI.getOperand(Idx); 54349cc55cSDimitry Andric if (MO.isImm()) 55e8d8bef9SDimitry Andric return (MO.getImm() >> shift); 56349cc55cSDimitry Andric 57349cc55cSDimitry Andric assert(MO.isExpr() && "Unexpected MO type."); 58349cc55cSDimitry Andric 59349cc55cSDimitry Andric MCFixupKind Kind = getTargetFixup(MO.getExpr()); 60349cc55cSDimitry Andric Fixups.push_back(MCFixup::create(0, MO.getExpr(), Kind, MI.getLoc())); 61349cc55cSDimitry Andric return 0; 62e8d8bef9SDimitry Andric } 63e8d8bef9SDimitry Andric 64349cc55cSDimitry Andric unsigned getRegSeqImmOpValue(const MCInst &MI, unsigned Idx, 65349cc55cSDimitry Andric SmallVectorImpl<MCFixup> &Fixups, 66349cc55cSDimitry Andric const MCSubtargetInfo &STI) const; 67349cc55cSDimitry Andric 68349cc55cSDimitry Andric unsigned getRegisterSeqOpValue(const MCInst &MI, unsigned Op, 69349cc55cSDimitry Andric SmallVectorImpl<MCFixup> &Fixups, 70349cc55cSDimitry Andric const MCSubtargetInfo &STI) const; 71349cc55cSDimitry Andric 72e8d8bef9SDimitry Andric unsigned getOImmOpValue(const MCInst &MI, unsigned Idx, 73e8d8bef9SDimitry Andric SmallVectorImpl<MCFixup> &Fixups, 74e8d8bef9SDimitry Andric const MCSubtargetInfo &STI) const; 75fe6060f1SDimitry Andric 76349cc55cSDimitry Andric unsigned getImmOpValueIDLY(const MCInst &MI, unsigned Idx, 77349cc55cSDimitry Andric SmallVectorImpl<MCFixup> &Fixups, 78349cc55cSDimitry Andric const MCSubtargetInfo &STI) const; 79349cc55cSDimitry Andric 80349cc55cSDimitry Andric unsigned getImmJMPIX(const MCInst &MI, unsigned Idx, 81349cc55cSDimitry Andric SmallVectorImpl<MCFixup> &Fixups, 82349cc55cSDimitry Andric const MCSubtargetInfo &STI) const; 83349cc55cSDimitry Andric 84349cc55cSDimitry Andric unsigned getImmOpValueMSBSize(const MCInst &MI, unsigned Idx, 85349cc55cSDimitry Andric SmallVectorImpl<MCFixup> &Fixups, 86349cc55cSDimitry Andric const MCSubtargetInfo &STI) const; 87349cc55cSDimitry Andric getImmShiftOpValue(const MCInst & MI,unsigned Idx,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI)88fe6060f1SDimitry Andric unsigned getImmShiftOpValue(const MCInst &MI, unsigned Idx, 89fe6060f1SDimitry Andric SmallVectorImpl<MCFixup> &Fixups, 90fe6060f1SDimitry Andric const MCSubtargetInfo &STI) const { 91fe6060f1SDimitry Andric const MCOperand &MO = MI.getOperand(Idx); 92fe6060f1SDimitry Andric assert(MO.isImm() && "Unexpected MO type."); 93fe6060f1SDimitry Andric return 1 << MO.getImm(); 94fe6060f1SDimitry Andric } 95fe6060f1SDimitry Andric 96fe6060f1SDimitry Andric MCFixupKind getTargetFixup(const MCExpr *Expr) const; 97fe6060f1SDimitry Andric 98fe6060f1SDimitry Andric template <llvm::CSKY::Fixups FIXUP> getBranchSymbolOpValue(const MCInst & MI,unsigned Idx,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI)99fe6060f1SDimitry Andric unsigned getBranchSymbolOpValue(const MCInst &MI, unsigned Idx, 100fe6060f1SDimitry Andric SmallVectorImpl<MCFixup> &Fixups, 101fe6060f1SDimitry Andric const MCSubtargetInfo &STI) const { 102fe6060f1SDimitry Andric const MCOperand &MO = MI.getOperand(Idx); 103fe6060f1SDimitry Andric 104fe6060f1SDimitry Andric if (MO.isImm()) 105fe6060f1SDimitry Andric return MO.getImm() >> 1; 106fe6060f1SDimitry Andric 107fe6060f1SDimitry Andric assert(MO.isExpr() && "Unexpected MO type."); 108fe6060f1SDimitry Andric 109fe6060f1SDimitry Andric MCFixupKind Kind = MCFixupKind(FIXUP); 110fe6060f1SDimitry Andric if (MO.getExpr()->getKind() == MCExpr::Target) 111fe6060f1SDimitry Andric Kind = getTargetFixup(MO.getExpr()); 112fe6060f1SDimitry Andric 113fe6060f1SDimitry Andric Fixups.push_back(MCFixup::create(0, MO.getExpr(), Kind, MI.getLoc())); 114fe6060f1SDimitry Andric return 0; 115fe6060f1SDimitry Andric } 116fe6060f1SDimitry Andric 117fe6060f1SDimitry Andric template <llvm::CSKY::Fixups FIXUP> getConstpoolSymbolOpValue(const MCInst & MI,unsigned Idx,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI)118fe6060f1SDimitry Andric unsigned getConstpoolSymbolOpValue(const MCInst &MI, unsigned Idx, 119fe6060f1SDimitry Andric SmallVectorImpl<MCFixup> &Fixups, 120fe6060f1SDimitry Andric const MCSubtargetInfo &STI) const { 121fe6060f1SDimitry Andric const MCOperand &MO = MI.getOperand(Idx); 122fe6060f1SDimitry Andric assert(MO.isExpr() && "Unexpected MO type."); 123fe6060f1SDimitry Andric 124fe6060f1SDimitry Andric MCFixupKind Kind = MCFixupKind(FIXUP); 125fe6060f1SDimitry Andric if (MO.getExpr()->getKind() == MCExpr::Target) 126fe6060f1SDimitry Andric Kind = getTargetFixup(MO.getExpr()); 127fe6060f1SDimitry Andric 128fe6060f1SDimitry Andric Fixups.push_back(MCFixup::create(0, MO.getExpr(), Kind, MI.getLoc())); 129fe6060f1SDimitry Andric return 0; 130fe6060f1SDimitry Andric } 131fe6060f1SDimitry Andric 132349cc55cSDimitry Andric template <llvm::CSKY::Fixups FIXUP> getDataSymbolOpValue(const MCInst & MI,unsigned Idx,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI)133349cc55cSDimitry Andric unsigned getDataSymbolOpValue(const MCInst &MI, unsigned Idx, 134349cc55cSDimitry Andric SmallVectorImpl<MCFixup> &Fixups, 135349cc55cSDimitry Andric const MCSubtargetInfo &STI) const { 136349cc55cSDimitry Andric const MCOperand &MO = MI.getOperand(Idx); 137349cc55cSDimitry Andric assert(MO.isExpr() && "Unexpected MO type."); 138349cc55cSDimitry Andric 139349cc55cSDimitry Andric MCFixupKind Kind = MCFixupKind(FIXUP); 140349cc55cSDimitry Andric if (MO.getExpr()->getKind() == MCExpr::Target) 141349cc55cSDimitry Andric Kind = getTargetFixup(MO.getExpr()); 142349cc55cSDimitry Andric 143349cc55cSDimitry Andric Fixups.push_back(MCFixup::create(0, MO.getExpr(), Kind, MI.getLoc())); 144349cc55cSDimitry Andric return 0; 145349cc55cSDimitry Andric } 146349cc55cSDimitry Andric getCallSymbolOpValue(const MCInst & MI,unsigned Idx,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI)147fe6060f1SDimitry Andric unsigned getCallSymbolOpValue(const MCInst &MI, unsigned Idx, 148fe6060f1SDimitry Andric SmallVectorImpl<MCFixup> &Fixups, 149fe6060f1SDimitry Andric const MCSubtargetInfo &STI) const { 150fe6060f1SDimitry Andric const MCOperand &MO = MI.getOperand(Idx); 151fe6060f1SDimitry Andric assert(MO.isExpr() && "Unexpected MO type."); 152fe6060f1SDimitry Andric 153fe6060f1SDimitry Andric MCFixupKind Kind = MCFixupKind(CSKY::fixup_csky_pcrel_imm26_scale2); 154fe6060f1SDimitry Andric if (MO.getExpr()->getKind() == MCExpr::Target) 155fe6060f1SDimitry Andric Kind = getTargetFixup(MO.getExpr()); 156fe6060f1SDimitry Andric 157fe6060f1SDimitry Andric Fixups.push_back(MCFixup::create(0, MO.getExpr(), Kind, MI.getLoc())); 158fe6060f1SDimitry Andric return 0; 159fe6060f1SDimitry Andric } 160fe6060f1SDimitry Andric getBareSymbolOpValue(const MCInst & MI,unsigned Idx,SmallVectorImpl<MCFixup> & Fixups,const MCSubtargetInfo & STI)161fe6060f1SDimitry Andric unsigned getBareSymbolOpValue(const MCInst &MI, unsigned Idx, 162fe6060f1SDimitry Andric SmallVectorImpl<MCFixup> &Fixups, 163fe6060f1SDimitry Andric const MCSubtargetInfo &STI) const { 164fe6060f1SDimitry Andric const MCOperand &MO = MI.getOperand(Idx); 165fe6060f1SDimitry Andric assert(MO.isExpr() && "Unexpected MO type."); 166fe6060f1SDimitry Andric 167fe6060f1SDimitry Andric MCFixupKind Kind = MCFixupKind(CSKY::fixup_csky_pcrel_imm18_scale2); 168fe6060f1SDimitry Andric if (MO.getExpr()->getKind() == MCExpr::Target) 169fe6060f1SDimitry Andric Kind = getTargetFixup(MO.getExpr()); 170fe6060f1SDimitry Andric 171fe6060f1SDimitry Andric Fixups.push_back(MCFixup::create(0, MO.getExpr(), Kind, MI.getLoc())); 172fe6060f1SDimitry Andric return 0; 173fe6060f1SDimitry Andric } 17481ad6265SDimitry Andric 175*5f757f3fSDimitry Andric void expandJBTF(const MCInst &MI, SmallVectorImpl<char> &CB, 17681ad6265SDimitry Andric SmallVectorImpl<MCFixup> &Fixups, 17781ad6265SDimitry Andric const MCSubtargetInfo &STI) const; 178*5f757f3fSDimitry Andric void expandNEG(const MCInst &MI, SmallVectorImpl<char> &CB, 17981ad6265SDimitry Andric SmallVectorImpl<MCFixup> &Fixups, 18081ad6265SDimitry Andric const MCSubtargetInfo &STI) const; 181*5f757f3fSDimitry Andric void expandRSUBI(const MCInst &MI, SmallVectorImpl<char> &CB, 18281ad6265SDimitry Andric SmallVectorImpl<MCFixup> &Fixups, 18381ad6265SDimitry Andric const MCSubtargetInfo &STI) const; 184e8d8bef9SDimitry Andric }; 185e8d8bef9SDimitry Andric 186e8d8bef9SDimitry Andric } // namespace llvm 187e8d8bef9SDimitry Andric 188e8d8bef9SDimitry Andric #endif // LLVM_LIB_TARGET_CSKY_MCTARGETDESC_CSKYMCCODEEMITTER_H 189