Skip to content

Commit 7542703

Browse files
committed
[EraVM][MC] Clarify usage of MemOperandKind and EncodedOperandMode enums
Explicitly specify EncodedOperandMode type and only convert it to int when necessary. Introduce MemOperandKind::OperandInvalid to not implicitly use OperandCode as invalid stack operand kind.
1 parent aeeec90 commit 7542703

File tree

6 files changed

+23
-15
lines changed

6 files changed

+23
-15
lines changed

llvm/lib/Target/EraVM/AsmParser/EraVMAsmParser.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,15 @@ class EraVMOperand : public MCParsedAsmOperand {
152152
return false;
153153

154154
switch (Mem.Kind) {
155+
case EraVM::OperandInvalid:
155156
case EraVM::OperandCode:
156157
return false;
158+
case EraVM::OperandStackAbsolute:
159+
case EraVM::OperandStackSPRelative:
160+
return true;
157161
case EraVM::OperandStackSPDecrement:
158162
case EraVM::OperandStackSPIncrement:
159163
return IsInput == (Mem.Kind == EraVM::OperandStackSPDecrement);
160-
default:
161-
return true;
162164
}
163165
}
164166

llvm/lib/Target/EraVM/Disassembler/EraVMDisassembler.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ DecodeStatus EraVMDisassembler::getInstruction(MCInst &MI, uint64_t &Size,
133133
uint64_t NewOpcode = OldOpcode;
134134
Insn &= ~EraVM::EncodedOpcodeMask;
135135

136-
int SrcMode = EraVM::OperandCode, DstMode = EraVM::OperandCode;
136+
EraVM::EncodedOperandMode SrcMode = EraVM::ModeNotApplicable;
137+
EraVM::EncodedOperandMode DstMode = EraVM::ModeNotApplicable;
137138
const EraVMOpcodeInfo *Info =
138139
EraVM::analyzeEncodedOpcode(OldOpcode, SrcMode, DstMode);
139140
if (!Info)

llvm/lib/Target/EraVM/MCTargetDesc/EraVMInstPrinter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ void EraVMInstPrinter::printStackOperand(const MCInst *MI, unsigned OpNo,
147147
using namespace EraVM;
148148

149149
unsigned BaseReg = 0;
150-
MemOperandKind Kind = MemOperandKind::OperandCode;
150+
MemOperandKind Kind = MemOperandKind::OperandInvalid;
151151
const MCSymbol *Symbol = nullptr;
152152
int Addend = 0;
153153
analyzeMCOperandsStack(*MI, OpNo, IsInput, BaseReg, Kind, Symbol, Addend);

llvm/lib/Target/EraVM/MCTargetDesc/EraVMMCCodeEmitter.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ uint64_t EraVMMCCodeEmitter::adjustForStackOperands(
106106
const MCInst &MI, uint64_t EncodedInstr, const MCSubtargetInfo &STI) const {
107107
const MCInstrDesc &Desc = MCII.get(MI.getOpcode());
108108

109-
int OldSrcMode = -1;
110-
int OldDstMode = -1;
109+
EraVM::EncodedOperandMode OldSrcMode = EraVM::ModeNotApplicable;
110+
EraVM::EncodedOperandMode OldDstMode = EraVM::ModeNotApplicable;
111111
// To not reason about possible signed-to-unsigned widening in the code below,
112112
// let's just use int type for EncodedOpcode as it essentially holds an
113113
// 11-bit value anyway.
@@ -120,13 +120,13 @@ uint64_t EraVMMCCodeEmitter::adjustForStackOperands(
120120
const MCOperand &Op =
121121
MI.getOperand(Info->getMCOperandIndexOfStackSrc(Desc));
122122
int NewSrcMode = modeEncodingByMarker(Op);
123-
EncodedOpcode += (NewSrcMode - OldSrcMode) * (int)Info->SrcMultiplier;
123+
EncodedOpcode += (NewSrcMode - (int)OldSrcMode) * (int)Info->SrcMultiplier;
124124
}
125125
if (OldDstMode == EraVM::ModeStackAbs) {
126126
const MCOperand &Op =
127127
MI.getOperand(Info->getMCOperandIndexOfStackDst(Desc));
128128
int NewDstMode = modeEncodingByMarker(Op);
129-
EncodedOpcode += (NewDstMode - OldDstMode) * (int)Info->DstMultiplier;
129+
EncodedOpcode += (NewDstMode - (int)OldDstMode) * (int)Info->DstMultiplier;
130130
}
131131

132132
EncodedInstr &= ~EraVM::EncodedOpcodeMask;
@@ -189,7 +189,7 @@ uint64_t EraVMMCCodeEmitter::getStackOpValue(const MCInst &MI, unsigned Idx,
189189
unsigned BaseReg = 0;
190190
const MCSymbol *Symbol = nullptr;
191191
int Addend = 0;
192-
EraVM::MemOperandKind Kind = EraVM::OperandStackAbsolute;
192+
EraVM::MemOperandKind Kind = EraVM::OperandInvalid;
193193
EraVM::analyzeMCOperandsStack(MI, Idx, IsSrc, BaseReg, Kind, Symbol, Addend);
194194

195195
return getRegWithAddend(MI, BaseReg, Symbol, Addend, IsSrc, Fixups);

llvm/lib/Target/EraVM/MCTargetDesc/EraVMMCTargetDesc.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,18 +105,21 @@ const EraVMOpcodeInfo *llvm::EraVM::findOpcodeInfo(unsigned Opcode) {
105105
return It;
106106
}
107107

108-
const EraVMOpcodeInfo *EraVM::analyzeEncodedOpcode(unsigned EncodedOpcode,
109-
int &SrcMode, int &DstMode) {
108+
const EraVMOpcodeInfo *
109+
EraVM::analyzeEncodedOpcode(unsigned EncodedOpcode, EncodedOperandMode &SrcMode,
110+
EncodedOperandMode &DstMode) {
110111
const EraVMOpcodeInfo *Info = findOpcodeInfo(EncodedOpcode);
111112
int OpcodeDelta = EncodedOpcode - Info->BaseOpcode;
112113

113114
SrcMode = ModeNotApplicable;
114115
DstMode = ModeNotApplicable;
115116

116117
if (Info->SrcMultiplier)
117-
SrcMode = (OpcodeDelta / Info->SrcMultiplier) % NumSrcModes;
118+
SrcMode =
119+
EncodedOperandMode((OpcodeDelta / Info->SrcMultiplier) % NumSrcModes);
118120
if (Info->DstMultiplier)
119-
DstMode = (OpcodeDelta / Info->DstMultiplier) % NumDstModes;
121+
DstMode =
122+
EncodedOperandMode((OpcodeDelta / Info->DstMultiplier) % NumDstModes);
120123

121124
return Info;
122125
}

llvm/lib/Target/EraVM/MCTargetDesc/EraVMMCTargetDesc.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ constexpr unsigned CellBitWidth = 256;
125125
/// and dummy integer immediate for absolute addressing
126126
/// * a few special cases exist - see appendMCOperands function.
127127
enum MemOperandKind {
128+
OperandInvalid,
128129
OperandCode,
129130
OperandStackAbsolute,
130131
OperandStackSPRelative,
@@ -143,7 +144,7 @@ void appendMCOperands(MCContext &Ctx, MCInst &MI, MemOperandKind Kind,
143144
unsigned Reg, const MCSymbol *Symbol, int Addend);
144145

145146
// encode_src_mode / encode_dst_mode
146-
enum OperandModes {
147+
enum EncodedOperandMode {
147148
ModeNotApplicable = -1, // no such operand
148149
ModeReg = 0, // SrcReg, DstReg
149150
ModeSpMod = 1, // SrcSpRelativePop, DstSpRelativePush
@@ -159,7 +160,8 @@ const uint64_t EncodedOpcodeMask = UINT64_C(0x7ff);
159160

160161
const EraVMOpcodeInfo *findOpcodeInfo(unsigned Opcode);
161162
const EraVMOpcodeInfo *analyzeEncodedOpcode(unsigned EncodedOpcode,
162-
int &SrcMode, int &DstMode);
163+
EncodedOperandMode &SrcMode,
164+
EncodedOperandMode &DstMode);
163165

164166
} // namespace EraVM
165167
} // namespace llvm

0 commit comments

Comments
 (0)