From 77bfcc1ff0a88e0430f077d22b6fad07c7d0c86b Mon Sep 17 00:00:00 2001 From: uz Date: Sun, 25 Apr 2010 21:51:59 +0000 Subject: [PATCH] Change the OptStackOps function so that it adjusts the instruction pointer after changing code. Without this correction, the insn pointer may be wrong after one of the subfunctions has changed code, which may cause a valid sequence not to be detected. This change (when proven good) may also go into 2.13. git-svn-id: svn://svn.cc65.org/cc65/trunk@4649 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- src/cc65/coptstop.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/cc65/coptstop.c b/src/cc65/coptstop.c index 620a73504..f00410fa5 100644 --- a/src/cc65/coptstop.c +++ b/src/cc65/coptstop.c @@ -1724,7 +1724,8 @@ unsigned OptStackOps (CodeSeg* S) { unsigned Changes = 0; /* Number of changes in one run */ StackOpData Data; - unsigned I; + int I; + int OldEntryCount; /* Old number of entries */ enum { Initialize, @@ -1756,7 +1757,7 @@ unsigned OptStackOps (CodeSeg* S) * intermediate code for zero page use. */ I = 0; - while (I < CS_GetEntryCount (S)) { + while (I < (int)CS_GetEntryCount (S)) { /* Get the next entry */ CodeEntry* E = CS_GetEntry (S, I); @@ -1890,6 +1891,9 @@ unsigned OptStackOps (CodeSeg* S) Data.OpEntry = CS_GetEntry (S, Data.OpIndex); Data.NextEntry = CS_GetNextEntry (S, Data.OpIndex); + /* Remember the current number of code lines */ + OldEntryCount = CS_GetEntryCount (S); + /* Adjust stack offsets to account for the upcoming removal */ AdjustStackOffset (&Data, 2); @@ -1901,12 +1905,17 @@ unsigned OptStackOps (CodeSeg* S) /* Call the optimizer function */ Changes += Data.OptFunc->Func (&Data); + /* Since the function may have added or deleted entries, + * correct the index. + */ + I += CS_GetEntryCount (S) - OldEntryCount; + /* Regenerate register info */ CS_GenRegInfo (S); /* Done */ State = Initialize; - break; + continue; }