Skip to content

Commit

Permalink
[TMS7000] Add immediate range check
Browse files Browse the repository at this point in the history
  • Loading branch information
tgtakaoka committed Dec 29, 2024
1 parent 3af87c2 commit 2bc5283
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
16 changes: 13 additions & 3 deletions src/asm_tms7000.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ Error AsmTms7000::parseOperand(StrScanner &scan, Operand &op) const {
}
if (op.hasError())
return op.getError();
op.mode = op.val.overflowUint8() ? M_IM16 : M_IM8;
op.mode = M_IM16;
scan = p;
return OK;
}
Expand Down Expand Up @@ -184,19 +184,29 @@ void AsmTms7000::emitRelative(AsmInsn &insn, const Operand &op) const {
void AsmTms7000::encodeOperand(AsmInsn &insn, const Operand &op, AddrMode mode) const {
insn.setErrorIf(op);
switch (mode) {
case M_IM8:
if (op.val.overflowUint8())
insn.setErrorIf(op, OVERFLOW_RANGE);
insn.emitOperand8(op.val.getUnsigned());
break;
case M_RN:
case M_PN:
case M_IM8:
case M_IDIR:
insn.emitOperand8(op.val.getUnsigned());
break;
case M_REL:
emitRelative(insn, op);
break;
case M_IM16:
if (op.val.overflowUint16())
insn.setErrorIf(op, OVERFLOW_RANGE);
insn.emitOperand16(op.val.getUnsigned());
break;
case M_ABS:
case M_BIDX:
case M_IM16:
case M_BIMM:
if (op.val.overflow(UINT16_MAX))
insn.setErrorIf(op, OVERFLOW_RANGE);
insn.emitOperand16(op.val.getUnsigned());
break;
case M_TRAP:
Expand Down
4 changes: 2 additions & 2 deletions src/table_tms7000.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,8 @@ static bool acceptMode(AddrMode opr, AddrMode table) {
return true;
if (opr == M_A || opr == M_B)
return table == M_RN;
if (opr == M_IM8)
return table == M_IM16;
if (opr == M_IM16)
return table == M_IM8;
if (opr == M_ADRR)
return table == M_RN || table == M_REL || table == M_TRAP;
if (opr == M_ADRP)
Expand Down
3 changes: 2 additions & 1 deletion test/test_asm_tms7000.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ void test_register() {
TEST("ADC %5AH, B", 0x59, 0x5A);
TEST("ADC %122, R123", 0x79, 0x7A, 0x7B);
ERRT("ADC %122, >123", OPERAND_NOT_ALLOWED, "%122, >123");
ERRT("ADC %>122, 123", OPERAND_NOT_ALLOWED, "%>122, 123");
ERRT("ADC %>122, 123", OVERFLOW_RANGE, "%>122, 123", 0x79, 0x22, 0x7B);

TEST("ADD B, A", 0x68);
TEST("ADD A, B", 0x38, 0x00);
Expand All @@ -163,6 +163,7 @@ void test_register() {
TEST("ADD R73, R74", 0x48, 0x49, 0x4A);
TEST("ADD %?11001, A", 0x28, 0x19);
TEST("ADD %>59, B", 0x58, 0x59);
TEST("ADD %-59, B", 0x58, 0xC5);
TEST("ADD %>79, R122", 0x78, 0x79, 0x7A);

TEST("AND B, A", 0x63);
Expand Down

0 comments on commit 2bc5283

Please sign in to comment.