Skip to content

Commit

Permalink
[F3850] Fix operand range check
Browse files Browse the repository at this point in the history
  • Loading branch information
tgtakaoka committed Feb 19, 2025
1 parent a5b31c7 commit 582622d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
21 changes: 4 additions & 17 deletions src/asm_f3850.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,7 @@ Error AsmF3850::parseOperand(StrScanner &scan, Operand &op) const {
op.val = parseInteger(p.skipSpaces(), op);
if (op.hasError())
return op.getError();
const auto v = op.val.getSigned();
if (v == 1) {
op.mode = M_C1;
} else if (v == 4) {
op.mode = M_C4;
} else if (op.val.overflow(7)) {
op.mode = M_IM3;
} else if (op.val.overflow(15)) {
op.mode = M_IM4;
} else if (!op.val.overflowUint8()) {
op.mode = M_IM8;
} else if (!op.val.overflowUint16()) {
op.mode = M_ADDR;
} else {
op.setError(OVERFLOW_RANGE);
}
op.mode = M_ADDR;
scan = p;
return OK;
}
Expand Down Expand Up @@ -141,8 +126,10 @@ void AsmF3850::encodeOperand(AsmInsn &insn, const Operand &op, AddrMode mode) co
insn.embed(v & 0xF);
break;
case M_IOA:
if (op.isOK() && !op.val.overflow(3, 0))
if (op.isOK() && !op.val.overflow(3))
insn.setErrorIf(op, OPERAND_NOT_ALLOWED);
if (op.val.overflow(255))
insn.setErrorIf(op, OVERFLOW_RANGE);
/* Fall-through */
case M_IM8:
if (op.val.overflowUint8())
Expand Down
2 changes: 1 addition & 1 deletion src/table_f3850.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ bool acceptMode(AddrMode opr, AddrMode table) {
return true;
if (opr == M_J)
return table == M_REG;
if (opr == M_C1 || opr == M_C4 || opr == M_IM3 || opr == M_IM4 || opr == M_IM8 || opr == M_ADDR)
if (opr == M_ADDR)
return table == M_C1 || table == M_C4 || table == M_IM3 || table == M_IM4 ||
table == M_IM8 || table == M_ADDR || table == M_REL || table == M_REG ||
table == M_IOS || table == M_IOA;
Expand Down
14 changes: 13 additions & 1 deletion test/test_asm_f3850.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ void test_accumlator() {
TEST("LIS 15", 0x7F);
ERRT("LIS 16", OVERFLOW_RANGE, "16", 0x70);
TEST("LI 0", 0x20, 0x00);
TEST("LI -1", 0x20, 0xFF);
TEST("LI -128", 0x20, 0x80);
TEST("LI H'FF'", 0x20, 0xFF);
ERRT("LI 256", OVERFLOW_RANGE, "256", 0x20, 0x00);
ERRT("LI -129", OVERFLOW_RANGE, "-129", 0x20, 0x7F);
TEST("NI 1", 0x21, 0x01);
TEST("NI -D'2'", 0x21, 0xFE);
TEST("OI $FE", 0x22, 0xFE);
Expand Down Expand Up @@ -209,6 +213,8 @@ void test_branch() {
ATEST(0x1000, "BT 5, H'1002'", 0x85, 0x01);
ATEST(0x1000, "BT 6, H'1002'", 0x86, 0x01);
ATEST(0x1000, "BT 7, H'1002'", 0x87, 0x01);
AERRT(0x1000, "BT 8, H'1002'", OVERFLOW_RANGE, "8, H'1002'", 0x80, 0x01);
AERRT(0x1000, "BT -1, H'1002'", OVERFLOW_RANGE, "-1, H'1002'", 0x87, 0x01);
ATEST(0x1000, "BR7 H'1002'", 0x8F, 0x01);

ATEST(0x1000, "BR H'1002'", 0x90, 0x01);
Expand All @@ -228,6 +234,8 @@ void test_branch() {
ATEST(0x1000, "BF 14, H'1002'", 0x9E, 0x01);
ATEST(0x1000, "BF 15, H'1002'", 0x9F, 0x01);
ATEST(0x1000, "BF 15, *+H'80'", 0x9F, 0x7F);
AERRT(0x1000, "BF 16, H'1002'", OVERFLOW_RANGE, "16, H'1002'", 0x90, 0x01);
AERRT(0x1000, "BF -1, H'1002'", OVERFLOW_RANGE, "-1, H'1002'", 0x9F, 0x01);
}

void test_io() {
Expand All @@ -243,6 +251,8 @@ void test_io() {
ERRT("IN 3", OPERAND_NOT_ALLOWED, "3", 0x26, 0x03);
TEST("IN 4", 0x26, 0x04);
TEST("IN H'FF'", 0x26, 0xFF);
ERRT("IN 256", OVERFLOW_RANGE, "256", 0x26, 0x00);
ERRT("IN -1", OVERFLOW_RANGE, "-1", 0x26, 0xFF);
TEST("OUTS 0", 0xB0);
TEST("OUTS 1", 0xB1);
ERRT("OUTS 2", OPERAND_NOT_ALLOWED, "2", 0xB2);
Expand All @@ -254,7 +264,9 @@ void test_io() {
ERRT("OUT 2", OPERAND_NOT_ALLOWED, "2", 0x27, 0x02);
ERRT("OUT 3", OPERAND_NOT_ALLOWED, "3", 0x27, 0x03);
TEST("OUT 4", 0x27, 0x04);
TEST("OUT H'AB'", 0x27, 0xAB);
TEST("OUT 255", 0x27, 0xFF);
ERRT("OUT 256", OVERFLOW_RANGE, "256", 0x27, 0x00);
ERRT("OUT -1", OVERFLOW_RANGE, "-1", 0x27, 0xFF);
}

void test_control() {
Expand Down

0 comments on commit 582622d

Please sign in to comment.