Skip to content

Commit

Permalink
[Z8000] Fix register overlap
Browse files Browse the repository at this point in the history
  • Loading branch information
tgtakaoka committed Oct 9, 2023
1 parent 85d2da6 commit 2695683
Show file tree
Hide file tree
Showing 12 changed files with 993 additions and 366 deletions.
17 changes: 9 additions & 8 deletions src/asm_z8000.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ void AsmZ8000::emitOperand(AsmInsn &insn, AddrMode mode, const Operand &op, Mode
}
}

void AsmZ8000::checkRegisterOverlap(const Operand &dstOp, const Operand &srcOp, RegName cnt) {
void AsmZ8000::checkRegisterOverlap(const Operand &dstOp, const Operand &srcOp, const Operand *cntOp) {
const auto dst = dstOp.reg;
const auto src = srcOp.reg;
const uint8_t dnum = encodeGeneralRegName(dst);
Expand All @@ -406,14 +406,15 @@ void AsmZ8000::checkRegisterOverlap(const Operand &dstOp, const Operand &srcOp,
const uint8_t ss = encodeGeneralRegName(src);
const uint8_t se = isLongReg(src) ? ss + 1 : ss;
if (ds == ss || ds == se || de == ss || de == se)
setErrorIf(dstOp, REGISTERS_OVERLAPPED);
if (cnt == REG_UNDEF)
setErrorIf(srcOp, REGISTERS_OVERLAPPED);
if (cntOp == nullptr)
return;
const auto cnt = cntOp->reg;
const uint8_t c = encodeGeneralRegName(cnt);
if (ds == c || de == c)
setErrorIf(dstOp, REGISTERS_OVERLAPPED);
setErrorIf(*cntOp, REGISTERS_OVERLAPPED);
if (ss == c || se == c)
setErrorIf(srcOp, REGISTERS_OVERLAPPED);
setErrorIf(*cntOp, REGISTERS_OVERLAPPED);
}

void AsmZ8000::checkRegisterOverlap(
Expand All @@ -422,19 +423,19 @@ void AsmZ8000::checkRegisterOverlap(
setErrorIf(dstOp, REGISTER_NOT_ALLOWED);
if (srcOp.mode == M_IR && (srcOp.reg == REG_R0 || srcOp.reg == REG_RR0))
setErrorIf(srcOp, REGISTER_NOT_ALLOWED);
checkRegisterOverlap(dstOp, srcOp, cntOp.reg);
if (insn.isTranslateInsn()) {
// @R1 isn't allowed as dst/src.
// Original content of RH1 are lost, so that R1 must not be used as dst/src.
if (!segmentedModel()) {
if (dstOp.reg == REG_R1)
setErrorIf(dstOp, REGISTER_NOT_ALLOWED);
if (srcOp.reg == REG_R1)
setErrorIf(srcOp, REGISTER_NOT_ALLOWED);
}
// R1 isn't allowed as cnt.
// R1 should not be used as cnt as well.
if (cntOp.reg == REG_R1)
setErrorIf(cntOp, REGISTER_NOT_ALLOWED);
}
checkRegisterOverlap(dstOp, srcOp, &cntOp);
}

int8_t AsmZ8000::parseIntrNames(StrScanner &scan) const {
Expand Down
3 changes: 2 additions & 1 deletion src/asm_z8000.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ struct AsmZ8000 final : Assembler, Config {
void emitFlags(AsmInsn &insn, ModeField field, const Operand &op);
void emitCtlRegister(AsmInsn &insn, ModeField field, const Operand &op);
void emitOperand(AsmInsn &insn, AddrMode mode, const Operand &op, ModeField field);
void checkRegisterOverlap(const Operand &dstOp, const Operand &srcOp, RegName cnt = REG_UNDEF);
void checkRegisterOverlap(
const Operand &dstOp, const Operand &srcOp, const Operand *cntOp = nullptr);
void checkRegisterOverlap(
const AsmInsn &insn, const Operand &dstOp, const Operand &srcOp, const Operand &cntOp);

Expand Down
2 changes: 2 additions & 0 deletions src/dis_z8000.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,8 @@ OprSize registerSize(const DisInsn &insn, AddrMode mode) {
return SZ_ADDR;
if (mode == M_GEND && (insn.opCode() >> 14) == 2) // M_R
return insn.size();
if (mode == M_R)
return insn.size();
return SZ_WORD;
}

Expand Down
1 change: 0 additions & 1 deletion src/reg_z8000.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/*
* Copyright 2020 Tadashi G. Takaoka
*
Expand Down
62 changes: 35 additions & 27 deletions test/reference/test_z8001.hex
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
:1000F00037421234774205001402123456787602E1
:100100008A00123476428A00123434421234744225
:1001100006003402FFFCBD1F1C21000F5C01000E15
:100120008A0012345C21000D8A0012341C2901005F
:100120008A0012345C21000D8A0012341C29000F51
:100130005C0901018A0012345C2901028A00123430
:100140003101FFFC3301FFFC3001FFFC3201FFFCF9
:100150003502FFFC3702FFFC9741174257408A00E7
Expand Down Expand Up @@ -92,30 +92,38 @@
:1005A0000200B2130200B3470200B3190010B219DF
:1005B0000008B34D0020B3110001B2110001B34592
:1005C0000001B319FFFEB21900FEB34DFFFEB311D7
:1005D000FFFDB21100FDB345FFFDBB280416BA288C
:1005E000041EBB2C0416BA2C041EBB200416BA2011
:1005F000041EBB240416BA24041EBB4A0626BA4AAB
:10060000062EBB4E0626BA4E062EBB420626BA4220
:10061000062EBB460626BA46062EBB490628BA4910
:100620000628BB490620BA490620BB410628BA4124
:100630000628BB410620BA410620B8280640B82C3F
:100640000640B8200640B8240640B82A0640B82E16
:10065000064EB8220640B826064E3D213C213B14EA
:1006600012343A1412343B4806283A4806283B48CC
:1006700006203A4806203B4006283A4006283B40E0
:1006800006203A4006203B4A06203A4A06203B42D2
:1006900006203A4206203F123E123B2612343A26EA
:1006A00012343B4A06283A4A06283B4206283A4278
:1006B00006283B1512343A1512343B4906283A49AC
:1006C00006283B4906203A4906203B4106283A4184
:1006D00006283B4106203A4106203B2712343AA720
:1006E00012343B4B06203A4B06203B4306203A434C
:1006F00006203B4B06283A4B06283B4306283A4344
:1007000006288DF58DF58D858D458D158D258D15DD
:100710007C007C027C017C047C067C057A007D1ACE
:100720007D1B7D1C7D1D7D1E7D1F7D127D137D1417
:100730007D157D167D178C118C29392079008A0052
:10074000123479208A0012347B0A7B1D7B097B08D6
:100750008D078DF38DF38D838D438D138D238D1335
:0E0760008DF18DF18D818D418D118D218D11C9
:1005D000FFFDB21100FDB345FFFDBB280416BB288B
:1005E0000406BB280016BA28041EBA28040EBA282E
:1005F000001EBB2C0416BB2C0406BB2C0016BA2C08
:10060000041EBA2C040EBA2C001EBB200416BB20FC
:100610000406BB200016BA20041EBA20040EBA201D
:10062000001EBB240416BB240406BB240016BA24F7
:10063000041EBA24040EBA24001EBB4A0626BB4A76
:100640000026BA4A062EBA4A002EBB4E0626BB4EDC
:100650000026BA4E062EBA4E002EBB420626BB42DC
:100660000026BA42062EBA42002EBB460626BB46DC
:100670000026BA46062EBA46002EBB490628BB49BC
:100680000028BA490628BA490028BB490620BB49B8
:100690000020BA490620BA490020BB410628BB41C8
:1006A0000028BA410628BA410028BB410620BB41B8
:1006B0000020BA410620BA410020B8280640B82CD4
:1006C0000640B8200640B8240640B82A0640B82E96
:1006D000064EB8220640B826064E3D213C213B146A
:1006E00012343A1412343B4806283A4806283B484C
:1006F00006203A4806203B4006283A4006283B4060
:1007000006203A4006203B4A06203A4A06203B4251
:1007100006203A4206203F123E123B2612343A2669
:1007200012343B4A06283A4A06283B4206283A42F7
:1007300006283B1512343A1512343B4906283A492B
:1007400006283B4906203A4906203B4106283A4103
:1007500006283B4106203A4106203B2712343AA79F
:1007600012343B4B06203A4B06203B4306203A43CB
:1007700006203B4B06283A4B06283B4306283A43C3
:1007800006288DF58DF58D858D458D158D258D155D
:100790007C007C027C017C047C067C057A007D1A4E
:1007A0007D1B7D1C7D1D7D1E7D1F7D127D137D1497
:1007B0007D157D167D178C118C29392079008A00D2
:1007C000123479208A0012347B0A7B1D7B097B0856
:1007D0008D078DF38DF38D838D438D138D238D13B5
:0E07E0008DF18DF18D818D418D118D218D1149
:00000001FF
34 changes: 33 additions & 1 deletion test/reference/test_z8001.inc
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
ldm r0,@rr2,#16
ldm r0,data1234,#15
ldm r0,data1234(r2),#14
ldm @rr2,r1,#1
ldm @rr2,r0,#16
ldm data1234,r1,#2
ldm data1234(r2),r1,#3
;# Load Relative
Expand Down Expand Up @@ -476,40 +476,72 @@
srll rr4,#3
;# Compare and Decrement
cpd r1,@rr2,r4,eq
cpd r0,@rr2,r4,eq
cpd r1,@rr2,r0,eq
cpdb rh1,@rr2,r4,ne
cpdb rh0,@rr2,r4,ne
cpdb rh1,@rr2,r0,ne
;# Compare, Decrement, and Repeat
cpdr r1,@rr2,r4,eq
cpdr r0,@rr2,r4,eq
cpdr r1,@rr2,r0,eq
cpdrb rh1,@rr2,r4,ne
cpdrb rh0,@rr2,r4,ne
cpdrb rh1,@rr2,r0,ne
;# Compare and Increment
cpi r1,@rr2,r4,eq
cpi r0,@rr2,r4,eq
cpi r1,@rr2,r0,eq
cpib rh1,@rr2,r4,ne
cpib rh0,@rr2,r4,ne
cpib rh1,@rr2,r0,ne
;# Compare, Increment, and Repeat
cpir r1,@rr2,r4,eq
cpir r0,@rr2,r4,eq
cpir r1,@rr2,r0,eq
cpirb rh1,@rr2,r4,ne
cpirb rh0,@rr2,r4,ne
cpirb rh1,@rr2,r0,ne
;# Compare String and Decrement
cpsd @rr2,@rr4,r6,eq
cpsd @rr2,@rr4,r0,eq
cpsdb @rr2,@rr4,r6,ne
cpsdb @rr2,@rr4,r0,ne
;# Compare String, Decrement, and Repeat
cpsdr @rr2,@rr4,r6,eq
cpsdr @rr2,@rr4,r0,eq
cpsdrb @rr2,@rr4,r6,ne
cpsdrb @rr2,@rr4,r0,ne
;# Compare Sring and Increment
cpsi @rr2,@rr4,r6,eq
cpsi @rr2,@rr4,r0,eq
cpsib @rr2,@rr4,r6,ne
cpsib @rr2,@rr4,r0,ne
;# Compare String, Increment, and Repeat
cpsir @rr2,@rr4,r6,eq
cpsir @rr2,@rr4,r0,eq
cpsirb @rr2,@rr4,r6,ne
cpsirb @rr2,@rr4,r0,ne
;# Load and Decrement
ldd @rr2,@rr4,r6
ldd @rr2,@rr4,r0
lddb @rr2,@rr4,r6
lddb @rr2,@rr4,r0
;# Load, Decrement, and Repeat
lddr @rr2,@rr4,r6
lddr @rr2,@rr4,r0
lddrb @rr2,@rr4,r6
lddrb @rr2,@rr4,r0
;# Load and Increment
ldi @rr2,@rr4,r6
ldi @rr2,@rr4,r0
ldib @rr2,@rr4,r6
ldib @rr2,@rr4,r0
;# Load, Increment, and Repeat
ldir @rr2,@rr4,r6
ldir @rr2,@rr4,r0
ldirb @rr2,@rr4,r6
ldirb @rr2,@rr4,r0
;# Translate and Decrement
trdb @rr2,@rr4,r6
;# Translate, Decrement, and Repeat
Expand Down
60 changes: 34 additions & 26 deletions test/reference/test_z8002.hex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
:1010C0003742123477420500140212345678760201
:1010D000123476421234344212347442060034021E
:1010E000FFFCBD1F1C21000F5C01000E12345C21AF
:1010F000000D12341C2901005C09010112345C2925
:1010F000000D12341C29000F5C09010112345C2917
:10110000010212343101FFFC3301FFFC3001FFFC0E
:101110003201FFFC3502FFFC3702FFFC974117420A
:1011200057401234574212349542154255401234FA
Expand Down Expand Up @@ -76,29 +76,37 @@
:1014B0000200B3190010B2190008B34D0020B31197
:1014C0000001B2110001B3450001B319FFFEB219CA
:1014D00000FEB34DFFFEB311FFFDB21100FDB34599
:1014E000FFFDBB280416BA28041EBB2C0416BA2C18
:1014F000041EBB200416BA20041EBB240416BA2402
:10150000041EBB4A0626BA4A062EBB4E0626BA4E13
:10151000062EBB420626BA42062EBB460626BA4611
:10152000062EBB490628BA490628BB490620BA49F7
:101530000620BB410628BA410628BB410620BA4115
:101540000620B8280640B82C0640B8200640B8242B
:101550000640B82A0640B82E064EB8220640B826E5
:10156000064E3D213C213B1412343A1412343B48C0
:1015700006283A4806283B4806203A4806203B40C1
:1015800006283A4006283B4006203A4006203B4ABF
:1015900006203A4A06203B4206203A4206203F12E5
:1015A0003E123B2612343A2612343B4A06283A4A67
:1015B00006283B4206283A4206283B1512343A15C3
:1015C00012343B4906283A4906283B4906203A4945
:1015D00006203B4106283A4106283B4106203A4175
:1015E00006203B2712343AA712343B4B06203A4BD5
:1015F00006203B4306203A4306203B4B06283A4B45
:1016000006283B4306283A4306288DF58DF58D853F
:101610008D458D158D258D157C007C027C017C040B
:101620007C067C057A007D1A7D1B7D1D7D1F7D1249
:101630007D137D157D178C118C293920790012348A
:10164000792012347B0A7B1D7B097B088D078DF383
:101650008DF38D838D438D138D238D138DF18DF13E
:0A1660008D818D418D118D218D11BA
:1014E000FFFDBB280416BB280406BB280016BA283B
:1014F000041EBA28040EBA28001EBB2C0416BB2CEE
:101500000406BB2C0016BA2C041EBA2C040EBA2CEE
:10151000001EBB200416BB200406BB200016BA2008
:10152000041EBA20040EBA20001EBB240416BB24DD
:101530000406BB240016BA24041EBA24040EBA24DE
:10154000001EBB4A0626BB4A0026BA4A062EBA4AE5
:10155000002EBB4E0626BB4E0026BA4E062EBA4EB5
:10156000002EBB420626BB420026BA42062EBA42D5
:10157000002EBB460626BB460026BA46062EBA46B5
:10158000002EBB490628BB490028BA490628BA499B
:101590000028BB490620BB490020BA490620BA49A9
:1015A0000020BB410628BB410028BA410628BA41A9
:1015B0000028BB410620BB410020BA410620BA41A9
:1015C0000020B8280640B82C0640B8200640B824B1
:1015D0000640B82A0640B82E064EB8220640B82665
:1015E000064E3D213C213B1412343A1412343B4840
:1015F00006283A4806283B4806203A4806203B4041
:1016000006283A4006283B4006203A4006203B4A3E
:1016100006203A4A06203B4206203A4206203F1264
:101620003E123B2612343A2612343B4A06283A4AE6
:1016300006283B4206283A4206283B1512343A1542
:1016400012343B4906283A4906283B4906203A49C4
:1016500006203B4106283A4106283B4106203A41F4
:1016600006203B2712343AA712343B4B06203A4B54
:1016700006203B4306203A4306203B4B06283A4BC4
:1016800006283B4306283A4306288DF58DF58D85BF
:101690008D458D158D258D157C007C027C017C048B
:1016A0007C067C057A007D1A7D1B7D1D7D1F7D12C9
:1016B0007D137D157D178C118C293920790012340A
:1016C000792012347B0A7B1D7B097B088D078DF303
:1016D0008DF38D838D438D138D238D138DF18DF1BE
:0A16E0008D818D418D118D218D113A
:00000001FF
34 changes: 33 additions & 1 deletion test/reference/test_z8002.inc
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
ldm r0,@r2,#16
ldm r0,data1234,#15
ldm r0,data1234(r2),#14
ldm @r2,r1,#1
ldm @r2,r0,#16
ldm data1234,r1,#2
ldm data1234(r2),r1,#3
;# Load Relative
Expand Down Expand Up @@ -476,40 +476,72 @@
srll rr4,#3
;# Compare and Decrement
cpd r1,@r2,r4,eq
cpd r0,@r2,r4,eq
cpd r1,@r2,r0,eq
cpdb rh1,@r2,r4,ne
cpdb rh0,@r2,r4,ne
cpdb rh1,@r2,r0,ne
;# Compare, Decrement, and Repeat
cpdr r1,@r2,r4,eq
cpdr r0,@r2,r4,eq
cpdr r1,@r2,r0,eq
cpdrb rh1,@r2,r4,ne
cpdrb rh0,@r2,r4,ne
cpdrb rh1,@r2,r0,ne
;# Compare and Increment
cpi r1,@r2,r4,eq
cpi r0,@r2,r4,eq
cpi r1,@r2,r0,eq
cpib rh1,@r2,r4,ne
cpib rh0,@r2,r4,ne
cpib rh1,@r2,r0,ne
;# Compare, Increment, and Repeat
cpir r1,@r2,r4,eq
cpir r0,@r2,r4,eq
cpir r1,@r2,r0,eq
cpirb rh1,@r2,r4,ne
cpirb rh0,@r2,r4,ne
cpirb rh1,@r2,r0,ne
;# Compare String and Decrement
cpsd @r2,@r4,r6,eq
cpsd @r2,@r4,r0,eq
cpsdb @r2,@r4,r6,ne
cpsdb @r2,@r4,r0,ne
;# Compare String, Decrement, and Repeat
cpsdr @r2,@r4,r6,eq
cpsdr @r2,@r4,r0,eq
cpsdrb @r2,@r4,r6,ne
cpsdrb @r2,@r4,r0,ne
;# Compare Sring and Increment
cpsi @r2,@r4,r6,eq
cpsi @r2,@r4,r0,eq
cpsib @r2,@r4,r6,ne
cpsib @r2,@r4,r0,ne
;# Compare String, Increment, and Repeat
cpsir @r2,@r4,r6,eq
cpsir @r2,@r4,r0,eq
cpsirb @r2,@r4,r6,ne
cpsirb @r2,@r4,r0,ne
;# Load and Decrement
ldd @r2,@r4,r6
ldd @r2,@r4,r0
lddb @r2,@r4,r6
lddb @r2,@r4,r0
;# Load, Decrement, and Repeat
lddr @r2,@r4,r6
lddr @r2,@r4,r0
lddrb @r2,@r4,r6
lddrb @r2,@r4,r0
;# Load and Increment
ldi @r2,@r4,r6
ldi @r2,@r4,r0
ldib @r2,@r4,r6
ldib @r2,@r4,r0
;# Load, Increment, and Repeat
ldir @r2,@r4,r6
ldir @r2,@r4,r0
ldirb @r2,@r4,r6
ldirb @r2,@r4,r0
;# Translate and Decrement
trdb @r2,@r4,r6
;# Translate, Decrement, and Repeat
Expand Down
Loading

0 comments on commit 2695683

Please sign in to comment.