Skip to content

Commit 582622d

Browse files
committed
[F3850] Fix operand range check
1 parent a5b31c7 commit 582622d

File tree

3 files changed

+18
-19
lines changed

3 files changed

+18
-19
lines changed

src/asm_f3850.cpp

+4-17
Original file line numberDiff line numberDiff line change
@@ -77,22 +77,7 @@ Error AsmF3850::parseOperand(StrScanner &scan, Operand &op) const {
7777
op.val = parseInteger(p.skipSpaces(), op);
7878
if (op.hasError())
7979
return op.getError();
80-
const auto v = op.val.getSigned();
81-
if (v == 1) {
82-
op.mode = M_C1;
83-
} else if (v == 4) {
84-
op.mode = M_C4;
85-
} else if (op.val.overflow(7)) {
86-
op.mode = M_IM3;
87-
} else if (op.val.overflow(15)) {
88-
op.mode = M_IM4;
89-
} else if (!op.val.overflowUint8()) {
90-
op.mode = M_IM8;
91-
} else if (!op.val.overflowUint16()) {
92-
op.mode = M_ADDR;
93-
} else {
94-
op.setError(OVERFLOW_RANGE);
95-
}
80+
op.mode = M_ADDR;
9681
scan = p;
9782
return OK;
9883
}
@@ -141,8 +126,10 @@ void AsmF3850::encodeOperand(AsmInsn &insn, const Operand &op, AddrMode mode) co
141126
insn.embed(v & 0xF);
142127
break;
143128
case M_IOA:
144-
if (op.isOK() && !op.val.overflow(3, 0))
129+
if (op.isOK() && !op.val.overflow(3))
145130
insn.setErrorIf(op, OPERAND_NOT_ALLOWED);
131+
if (op.val.overflow(255))
132+
insn.setErrorIf(op, OVERFLOW_RANGE);
146133
/* Fall-through */
147134
case M_IM8:
148135
if (op.val.overflowUint8())

src/table_f3850.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ bool acceptMode(AddrMode opr, AddrMode table) {
215215
return true;
216216
if (opr == M_J)
217217
return table == M_REG;
218-
if (opr == M_C1 || opr == M_C4 || opr == M_IM3 || opr == M_IM4 || opr == M_IM8 || opr == M_ADDR)
218+
if (opr == M_ADDR)
219219
return table == M_C1 || table == M_C4 || table == M_IM3 || table == M_IM4 ||
220220
table == M_IM8 || table == M_ADDR || table == M_REL || table == M_REG ||
221221
table == M_IOS || table == M_IOA;

test/test_asm_f3850.cpp

+13-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,11 @@ void test_accumlator() {
6161
TEST("LIS 15", 0x7F);
6262
ERRT("LIS 16", OVERFLOW_RANGE, "16", 0x70);
6363
TEST("LI 0", 0x20, 0x00);
64+
TEST("LI -1", 0x20, 0xFF);
65+
TEST("LI -128", 0x20, 0x80);
6466
TEST("LI H'FF'", 0x20, 0xFF);
67+
ERRT("LI 256", OVERFLOW_RANGE, "256", 0x20, 0x00);
68+
ERRT("LI -129", OVERFLOW_RANGE, "-129", 0x20, 0x7F);
6569
TEST("NI 1", 0x21, 0x01);
6670
TEST("NI -D'2'", 0x21, 0xFE);
6771
TEST("OI $FE", 0x22, 0xFE);
@@ -209,6 +213,8 @@ void test_branch() {
209213
ATEST(0x1000, "BT 5, H'1002'", 0x85, 0x01);
210214
ATEST(0x1000, "BT 6, H'1002'", 0x86, 0x01);
211215
ATEST(0x1000, "BT 7, H'1002'", 0x87, 0x01);
216+
AERRT(0x1000, "BT 8, H'1002'", OVERFLOW_RANGE, "8, H'1002'", 0x80, 0x01);
217+
AERRT(0x1000, "BT -1, H'1002'", OVERFLOW_RANGE, "-1, H'1002'", 0x87, 0x01);
212218
ATEST(0x1000, "BR7 H'1002'", 0x8F, 0x01);
213219

214220
ATEST(0x1000, "BR H'1002'", 0x90, 0x01);
@@ -228,6 +234,8 @@ void test_branch() {
228234
ATEST(0x1000, "BF 14, H'1002'", 0x9E, 0x01);
229235
ATEST(0x1000, "BF 15, H'1002'", 0x9F, 0x01);
230236
ATEST(0x1000, "BF 15, *+H'80'", 0x9F, 0x7F);
237+
AERRT(0x1000, "BF 16, H'1002'", OVERFLOW_RANGE, "16, H'1002'", 0x90, 0x01);
238+
AERRT(0x1000, "BF -1, H'1002'", OVERFLOW_RANGE, "-1, H'1002'", 0x9F, 0x01);
231239
}
232240

233241
void test_io() {
@@ -243,6 +251,8 @@ void test_io() {
243251
ERRT("IN 3", OPERAND_NOT_ALLOWED, "3", 0x26, 0x03);
244252
TEST("IN 4", 0x26, 0x04);
245253
TEST("IN H'FF'", 0x26, 0xFF);
254+
ERRT("IN 256", OVERFLOW_RANGE, "256", 0x26, 0x00);
255+
ERRT("IN -1", OVERFLOW_RANGE, "-1", 0x26, 0xFF);
246256
TEST("OUTS 0", 0xB0);
247257
TEST("OUTS 1", 0xB1);
248258
ERRT("OUTS 2", OPERAND_NOT_ALLOWED, "2", 0xB2);
@@ -254,7 +264,9 @@ void test_io() {
254264
ERRT("OUT 2", OPERAND_NOT_ALLOWED, "2", 0x27, 0x02);
255265
ERRT("OUT 3", OPERAND_NOT_ALLOWED, "3", 0x27, 0x03);
256266
TEST("OUT 4", 0x27, 0x04);
257-
TEST("OUT H'AB'", 0x27, 0xAB);
267+
TEST("OUT 255", 0x27, 0xFF);
268+
ERRT("OUT 256", OVERFLOW_RANGE, "256", 0x27, 0x00);
269+
ERRT("OUT -1", OVERFLOW_RANGE, "-1", 0x27, 0xFF);
258270
}
259271

260272
void test_control() {

0 commit comments

Comments
 (0)