From 831cf141d641f01012775e08b4c29448b2033a3d Mon Sep 17 00:00:00 2001 From: MaartenBrock Date: Mon, 10 Mar 2008 21:34:22 +0000 Subject: [PATCH 1/1] * src/SDCCast.c (isConformingBody): fixed bug 1505811, thanks Robert Larice * support/regression/tests/bug1505811.c: new, added git-svn-id: https://sdcc.svn.sourceforge.net/svnroot/sdcc/trunk/sdcc@5087 4a8a32a2-be11-0410-ad9d-d568d2c75423 --- ChangeLog | 5 ++++ src/SDCCast.c | 43 ++++++++++++++------------- support/regression/tests/bug1505811.c | 34 +++++++++++++++++++++ 3 files changed, 61 insertions(+), 21 deletions(-) create mode 100644 support/regression/tests/bug1505811.c diff --git a/ChangeLog b/ChangeLog index 65079f55..ca49bdca 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2008-03-10 Maarten Brock + + * src/SDCCast.c (isConformingBody): fixed bug 1505811, thanks Robert Larice + * support/regression/tests/bug1505811.c: new, added + 2008-03-09 Raphael Neider * device/include/pic16/pic18f2620.h, diff --git a/src/SDCCast.c b/src/SDCCast.c index 94a1c5a2..869752b3 100644 --- a/src/SDCCast.c +++ b/src/SDCCast.c @@ -1799,8 +1799,8 @@ astHasDeref (ast * tree) } /*-----------------------------------------------------------------*/ -/* isConformingBody - the loop body has to conform to a set of rules */ -/* for the loop to be considered reversible read on for rules */ +/* isConformingBody - the loop body has to conform to a set of */ +/* rules for the loop to be considered reversible read on for rules*/ /*-----------------------------------------------------------------*/ bool isConformingBody (ast * pbody, symbol * sym, ast * body) @@ -1809,14 +1809,13 @@ isConformingBody (ast * pbody, symbol * sym, ast * body) /* we are going to do a pre-order traversal of the tree && check for the following conditions. (essentially a set of very shallow tests ) - a) the sym passed does not participate in - any arithmetic operation + a) the sym passed does not participate in any arithmetic operation b) There are no function calls c) all jumps are within the body d) address of loop control variable not taken - e) if an assignment has a pointer on the - left hand side make sure right does not have - loop control variable */ + e) if an assignment has a pointer on the left hand side make sure + right does not have loop control variable + */ /* if we reach the end or a leaf then true */ if (!pbody || IS_AST_LINK (pbody) || IS_AST_VALUE (pbody)) @@ -1871,9 +1870,9 @@ isConformingBody (ast * pbody, symbol * sym, ast * body) /* if right is NULL then unary operation */ /*------------------------------------------------------------------*/ -/*----------------------------*/ + /*----------------------------*/ /* address of */ -/*----------------------------*/ + /*----------------------------*/ if (!pbody->right) { if (IS_AST_SYM_VALUE (pbody->left) && @@ -1973,7 +1972,8 @@ isConformingBody (ast * pbody, symbol * sym, ast * body) if (astHasVolatile (pbody->left)) return FALSE; - if (astHasDeref(pbody->right)) return FALSE; + if (astHasDeref(pbody->right)) + return FALSE; return isConformingBody (pbody->left, sym, body) && isConformingBody (pbody->right, sym, body); @@ -1990,27 +1990,31 @@ isConformingBody (ast * pbody, symbol * sym, ast * body) assert ("Parser should not have generated this\n"); /*------------------------------------------------------------------*/ -/*----------------------------*/ + /*----------------------------*/ /* comma operator */ -/*----------------------------*/ + /*----------------------------*/ case ',': return isConformingBody (pbody->left, sym, body) && isConformingBody (pbody->right, sym, body); /*------------------------------------------------------------------*/ -/*----------------------------*/ + /*----------------------------*/ /* function call */ -/*----------------------------*/ + /*----------------------------*/ case CALL: - /* if local & not passed as paramater then ok */ - if (sym->level && !astHasSymbol(pbody->right,sym)) + /* if local & not passed as parameter & + not used to find the function then ok */ + if (sym->level && !astHasSymbol (pbody->right, sym) && + !astHasSymbol (pbody->left, sym)) + { return TRUE; + } return FALSE; /*------------------------------------------------------------------*/ -/*----------------------------*/ + /*----------------------------*/ /* return statement */ -/*----------------------------*/ + /*----------------------------*/ case RETURN: return FALSE; @@ -2029,9 +2033,6 @@ isConformingBody (ast * pbody, symbol * sym, ast * body) return isConformingBody (pbody->left, sym, body) && isConformingBody (pbody->right, sym, body); - - - } /*-----------------------------------------------------------------*/ diff --git a/support/regression/tests/bug1505811.c b/support/regression/tests/bug1505811.c new file mode 100644 index 00000000..ddb4e0ab --- /dev/null +++ b/support/regression/tests/bug1505811.c @@ -0,0 +1,34 @@ +/* bug 1505811 + * demonstrates an incorrect "loopreverse" + * note func0, is a kind of safeguard, the incorrect code + * will access indices 0 and 1 instead of 1 and 2, + * and with incorrect order + */ + +#include + +char glbl; + +void func0() { glbl = 0; } +void func1() { glbl = 1; } +void func2() { glbl = 2; } + +typedef void (*fptr)(); + +fptr ep_init[3] = { func0, func1, func2 }; + +void buggy() +{ + unsigned char i; + for(i = 1; i <= 2; i++) + { + ep_init[i](); + } +} + +void +testBug(void) +{ + buggy(); + ASSERT(glbl == 2); +} -- 2.30.2