From 78263cd24b301406f9567f3e0a4fa03488445656 Mon Sep 17 00:00:00 2001 From: bbbradsmith Date: Fri, 17 Feb 2023 23:48:32 -0500 Subject: [PATCH 1/8] Fix OptStackOps label migration case for toscmpbool, add test case --- src/cc65/coptstop.c | 2 ++ test/val/bug1989.c | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 test/val/bug1989.c diff --git a/src/cc65/coptstop.c b/src/cc65/coptstop.c index 4d0151c55..8b2821dcd 100644 --- a/src/cc65/coptstop.c +++ b/src/cc65/coptstop.c @@ -1153,6 +1153,8 @@ static unsigned Opt_a_toscmpbool (StackOpData* D, const char* BoolTransformer) /* Save lhs into zeropage */ AddStoreLhsA (D); + /* AddStoreLhsA may have moved the OpIndex, recalculate insertion point to prevent label migration. */ + D->IP = D->OpIndex + 1; /* cmp */ X = NewCodeEntry (OP65_CMP, AM65_ZP, D->ZPLo, 0, D->OpEntry->LI); diff --git a/test/val/bug1989.c b/test/val/bug1989.c new file mode 100644 index 000000000..b3e3d4c8d --- /dev/null +++ b/test/val/bug1989.c @@ -0,0 +1,40 @@ + +/* bug #1989 - OptStackOps Opt_a_toscmpbool bypassed a comparison, discovered in 544a49c */ + +#include + +unsigned char i,r,j; + +void fail() // for the r=0 case, the == comparison was getting jumped over by OptStackOps +{ + if ((i & 0x1f) == (r ? 0 : 16)) j -=8; +} + +void pass() +{ + if ((i & 0x1f) == (unsigned char)(r ? 0 : 16)) j -= 8; +} + +void test(unsigned char ti, unsigned char tr, unsigned char tj) +{ + unsigned char rj; + i = ti; + r = tr; + j = tj; + pass(); + rj = j; + i = ti; + r = tr; + j = tj; + fail(); + if (j != rj) exit(1); +} + +int main(void) +{ + test( 1,0,33); + test( 0,0,33); + test( 1,1,33); + test(16,1,33); + return 0; +} From 4e411e8526df0c627c5ab0e6338853a82a51c0b2 Mon Sep 17 00:00:00 2001 From: bbbradsmith Date: Sat, 18 Feb 2023 00:13:34 -0500 Subject: [PATCH 2/8] Opt_a_tosicmp likely has a bug which will incorrectly migrate a label on a replaced op in one case. --- src/cc65/coptstop.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/cc65/coptstop.c b/src/cc65/coptstop.c index 8b2821dcd..ae40a55e9 100644 --- a/src/cc65/coptstop.c +++ b/src/cc65/coptstop.c @@ -1208,6 +1208,8 @@ static unsigned Opt_a_tosicmp (StackOpData* D) /* RHS src is not directly comparable */ X = NewCodeEntry (OP65_STA, AM65_ZP, D->ZPHi, 0, D->OpEntry->LI); InsertEntry (D, X, D->Rhs.A.ChgIndex + 1); + /* RHS insertion may have moved the OpIndex, recalculate insertion point to prevent label migration. */ + D->IP = D->OpIndex + 1; /* Cmp with stored RHS */ X = NewCodeEntry (OP65_CMP, AM65_ZP, D->ZPHi, 0, D->OpEntry->LI); From 864eecf7c3764741855801de3228f4363b4d69fa Mon Sep 17 00:00:00 2001 From: bbbradsmith Date: Mon, 20 Feb 2023 20:08:58 -0500 Subject: [PATCH 3/8] ca16 doc for A8/A18/I8/I16: use "assume" instead of "switch" to avoid implying that a hardware state is changed. See: #1759 --- doc/ca65.sgml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/doc/ca65.sgml b/doc/ca65.sgml index 19fd3aa2a..c84c57353 100644 --- a/doc/ca65.sgml +++ b/doc/ca65.sgml @@ -2019,7 +2019,7 @@ Here's a list of all control commands and a description, what they do: .A16