Skip to content

Commit 559bea7

Browse files
committed
encode2: Minor optimization of enc_mem
1 parent e19a52e commit 559bea7

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

encode2.c

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,22 @@ enc_mem_common(uint8_t* buf, unsigned ripoff, FeMem op0, uint64_t op1,
6767
withsib = true;
6868
}
6969

70-
if (UNLIKELY(op0.base.idx == op_reg_idx(FE_NOREG))) {
71-
*buf++ = (reg << 3) | 4;
72-
*buf++ = sib | 5;
73-
enc_imm(buf, off, 4);
74-
return 6;
75-
} else if (UNLIKELY(op0.base.idx == FE_IP.idx)) {
76-
if (withsib)
70+
if (UNLIKELY(op0.base.idx >= 0x20)) {
71+
if (UNLIKELY(op0.base.idx >= op_reg_idx(FE_NOREG))) {
72+
*buf++ = (reg << 3) | 4;
73+
*buf++ = sib | 5;
74+
enc_imm(buf, off, 4);
75+
return 6;
76+
} else if (LIKELY(op0.base.idx == FE_IP.idx)) {
77+
if (withsib)
78+
return 0;
79+
*buf++ = (reg << 3) | 5;
80+
// Adjust offset, caller doesn't know instruction length.
81+
enc_imm(buf, off - ripoff - 5, 4);
82+
return 5;
83+
} else {
7784
return 0;
78-
// Adjust offset, caller doesn't know instruction length.
79-
off -= ripoff + 5;
80-
*buf++ = (reg << 3) | 5;
81-
enc_imm(buf, off, 4);
82-
return 5;
85+
}
8386
}
8487

8588
rm = op_reg_idx(op0.base) & 7;
@@ -116,13 +119,15 @@ enc_mem_common(uint8_t* buf, unsigned ripoff, FeMem op0, uint64_t op1,
116119
static int
117120
enc_mem(uint8_t* buf, unsigned ripoff, FeMem op0, uint64_t op1, bool forcesib,
118121
unsigned disp8scale) {
119-
if ((op_reg_idx(op0.idx) != op_reg_idx(FE_NOREG)) != !!op0.scale)
120-
return 0;
121122
unsigned sibidx = forcesib ? 4 : 8;
122-
if (op_reg_idx(op0.idx) != op_reg_idx(FE_NOREG)) {
123+
if (op_reg_idx(op0.idx) < op_reg_idx(FE_NOREG)) {
124+
if (!op0.scale)
125+
return 0;
123126
if (op_reg_idx(op0.idx) == 4)
124127
return 0;
125128
sibidx = op_reg_idx(op0.idx) & 7;
129+
} else if (op0.scale) {
130+
return 0;
126131
}
127132
return enc_mem_common(buf, ripoff, op0, op1, sibidx, disp8scale);
128133
}

0 commit comments

Comments
 (0)