From 5ef420af5a4be4350780b335354093bedc81b5c0 Mon Sep 17 00:00:00 2001 From: acqn Date: Tue, 1 Mar 2022 10:40:01 +0800 Subject: [PATCH] Fixed OptCmp1 with certain code patterns with labels. --- src/cc65/coptcmp.c | 4 ++-- test/val/bug1690.c | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 test/val/bug1690.c diff --git a/src/cc65/coptcmp.c b/src/cc65/coptcmp.c index fda23ae0a..92401a858 100644 --- a/src/cc65/coptcmp.c +++ b/src/cc65/coptcmp.c @@ -309,10 +309,10 @@ unsigned OptCmp1 (CodeSeg* S) /* Insert the ora instead */ X = NewCodeEntry (OP65_ORA, L[0]->AM, L[0]->Arg, 0, L[0]->LI); - CS_InsertEntry (S, X, I); + CS_InsertEntry (S, X, I+3); /* Remove all other instructions */ - CS_DelEntries (S, I+1, 3); + CS_DelEntries (S, I, 3); /* Remember, we had changes */ ++Changes; diff --git a/test/val/bug1690.c b/test/val/bug1690.c new file mode 100644 index 000000000..317913634 --- /dev/null +++ b/test/val/bug1690.c @@ -0,0 +1,23 @@ +/* OptCmp1 messed up with labels */ + +#include + +int main(void) +{ + register unsigned int x = 0x200; + register unsigned int y = 0; + + do + { + ++y; + } + while (--x); + + if (y != 0x200) + { + printf("0x%X\n", y); + return 1; + } + + return 0; +}