Skip to content

Commit 3322c22

Browse files
atrosinenkoakiramenai
authored andcommitted
[EraVM][MC] Reuse analyzeMCOperandsStack for adjusting opcode on encoding
1 parent 7542703 commit 3322c22

File tree

3 files changed

+40
-15
lines changed

3 files changed

+40
-15
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,19 @@ void EraVM::analyzeMCOperandsStack(const MCInst &MI, unsigned Idx, bool IsSrc,
231231
Addend &= 0xFFFF;
232232
}
233233

234+
EraVM::MemOperandKind EraVM::getStackOperandKind(const MCInst &MI, unsigned Idx,
235+
bool IsSrc) {
236+
// TODO After refactoring, make analyzeMCOperandsStack() call this function.
237+
// For now, calling this way to not reimplement handling of (@SYM, 0, 0) here.
238+
unsigned Reg = 0;
239+
MemOperandKind Kind = MemOperandKind::OperandInvalid;
240+
const MCSymbol *Symbol = nullptr;
241+
int Addend = 0;
242+
243+
analyzeMCOperandsStack(MI, Idx, IsSrc, Reg, Kind, Symbol, Addend);
244+
return Kind;
245+
}
246+
234247
static MCOperand createStackOperandMarker(EraVM::MemOperandKind Kind) {
235248
switch (Kind) {
236249
default:

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

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,9 @@ class EraVMMCCodeEmitter : public MCCodeEmitter {
9191
SmallVectorImpl<MCFixup> &Fixups) const;
9292
};
9393

94-
static int modeEncodingByMarker(const MCOperand &Op) {
95-
assert((Op.isImm() || Op.isReg()) && "Unexpected marker operand");
96-
97-
if (Op.isImm())
98-
return EraVM::ModeStackAbs;
99-
100-
unsigned MarkerReg = Op.getReg();
101-
assert(MarkerReg == EraVM::R0 || MarkerReg == EraVM::SP);
102-
return (MarkerReg == EraVM::SP) ? EraVM::ModeSpRel : EraVM::ModeSpMod;
94+
static int getStackModeEncoding(const MCInst &MI, unsigned Idx, bool IsSrc) {
95+
EraVM::MemOperandKind Kind = EraVM::getStackOperandKind(MI, Idx, IsSrc);
96+
return EraVM::getModeEncodingForOperandKind(Kind);
10397
}
10498

10599
uint64_t EraVMMCCodeEmitter::adjustForStackOperands(
@@ -117,15 +111,13 @@ uint64_t EraVMMCCodeEmitter::adjustForStackOperands(
117111
assert(Info && "Incorrect EncodedOpcode produced by the encoder");
118112

119113
if (OldSrcMode == EraVM::ModeStackAbs) {
120-
const MCOperand &Op =
121-
MI.getOperand(Info->getMCOperandIndexOfStackSrc(Desc));
122-
int NewSrcMode = modeEncodingByMarker(Op);
114+
unsigned SrcIdx = Info->getMCOperandIndexOfStackSrc(Desc);
115+
int NewSrcMode = getStackModeEncoding(MI, SrcIdx, /*IsSrc=*/true);
123116
EncodedOpcode += (NewSrcMode - (int)OldSrcMode) * (int)Info->SrcMultiplier;
124117
}
125118
if (OldDstMode == EraVM::ModeStackAbs) {
126-
const MCOperand &Op =
127-
MI.getOperand(Info->getMCOperandIndexOfStackDst(Desc));
128-
int NewDstMode = modeEncodingByMarker(Op);
119+
unsigned DstIdx = Info->getMCOperandIndexOfStackDst(Desc);
120+
int NewDstMode = getStackModeEncoding(MI, DstIdx, /*IsSrc=*/false);
129121
EncodedOpcode += (NewDstMode - (int)OldDstMode) * (int)Info->DstMultiplier;
130122
}
131123

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ void analyzeMCOperandsStack(const MCInst &MI, unsigned Idx, bool IsSrc,
140140
unsigned &Reg, MemOperandKind &Kind,
141141
const MCSymbol *&Symbol, int &Addend);
142142

143+
// Returns the same Kind as analyzeMCOperandsStack returns, without other info.
144+
MemOperandKind getStackOperandKind(const MCInst &MI, unsigned Idx, bool IsSrc);
145+
143146
void appendMCOperands(MCContext &Ctx, MCInst &MI, MemOperandKind Kind,
144147
unsigned Reg, const MCSymbol *Symbol, int Addend);
145148

@@ -156,6 +159,23 @@ enum EncodedOperandMode {
156159
NumSrcModes = 6,
157160
};
158161

162+
static inline EncodedOperandMode
163+
getModeEncodingForOperandKind(MemOperandKind Kind) {
164+
switch (Kind) {
165+
case OperandInvalid:
166+
return ModeNotApplicable;
167+
case OperandCode:
168+
return ModeCode;
169+
case OperandStackAbsolute:
170+
return ModeStackAbs;
171+
case OperandStackSPRelative:
172+
return ModeSpRel;
173+
case OperandStackSPDecrement:
174+
case OperandStackSPIncrement:
175+
return ModeSpMod;
176+
}
177+
}
178+
159179
const uint64_t EncodedOpcodeMask = UINT64_C(0x7ff);
160180

161181
const EraVMOpcodeInfo *findOpcodeInfo(unsigned Opcode);

0 commit comments

Comments
 (0)