From 767f093ff86604314a9ee42c25842c79905f3248 Mon Sep 17 00:00:00 2001 From: Lauri Kasanen Date: Sat, 6 May 2017 13:20:55 +0300 Subject: [PATCH 1/5] Add fast path for char postinc --- src/cc65/expr.c | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/src/cc65/expr.c b/src/cc65/expr.c index 34cf550a2..502fd7a8e 100644 --- a/src/cc65/expr.c +++ b/src/cc65/expr.c @@ -1534,26 +1534,35 @@ static void PostInc (ExprDesc* Expr) /* Get the data type */ Flags = TypeOf (Expr->Type); - /* Push the address if needed */ - PushAddr (Expr); + /* Fast path: char */ + if ((Flags & CF_CHAR) == CF_CHAR && ED_GetLoc(Expr) & (E_LOC_GLOBAL | E_LOC_STATIC)) { - /* Fetch the value and save it (since it's the result of the expression) */ - LoadExpr (CF_NONE, Expr); - g_save (Flags | CF_FORCECHAR); + LoadExpr (CF_NONE, Expr); + AddCodeLine ("inc %s", ED_GetLabelName(Expr, 0)); - /* If we have a pointer expression, increment by the size of the type */ - if (IsTypePtr (Expr->Type)) { - g_inc (Flags | CF_CONST | CF_FORCECHAR, CheckedSizeOf (Expr->Type + 1)); } else { - g_inc (Flags | CF_CONST | CF_FORCECHAR, 1); + + /* Push the address if needed */ + PushAddr (Expr); + + /* Fetch the value and save it (since it's the result of the expression) */ + LoadExpr (CF_NONE, Expr); + g_save (Flags | CF_FORCECHAR); + + /* If we have a pointer expression, increment by the size of the type */ + if (IsTypePtr (Expr->Type)) { + g_inc (Flags | CF_CONST | CF_FORCECHAR, CheckedSizeOf (Expr->Type + 1)); + } else { + g_inc (Flags | CF_CONST | CF_FORCECHAR, 1); + } + + /* Store the result back */ + Store (Expr, 0); + + /* Restore the original value in the primary register */ + g_restore (Flags | CF_FORCECHAR); } - /* Store the result back */ - Store (Expr, 0); - - /* Restore the original value in the primary register */ - g_restore (Flags | CF_FORCECHAR); - /* The result is always an expression, no reference */ ED_MakeRValExpr (Expr); } From ead0338c0def811cb251090cc0851899a0cbdf8e Mon Sep 17 00:00:00 2001 From: Lauri Kasanen Date: Sat, 6 May 2017 13:23:50 +0300 Subject: [PATCH 2/5] Add fast path for char postdec --- src/cc65/expr.c | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/src/cc65/expr.c b/src/cc65/expr.c index 502fd7a8e..e5c1a17b9 100644 --- a/src/cc65/expr.c +++ b/src/cc65/expr.c @@ -1590,26 +1590,35 @@ static void PostDec (ExprDesc* Expr) /* Get the data type */ Flags = TypeOf (Expr->Type); - /* Push the address if needed */ - PushAddr (Expr); + /* Fast path: char */ + if ((Flags & CF_CHAR) == CF_CHAR && ED_GetLoc(Expr) & (E_LOC_GLOBAL | E_LOC_STATIC)) { - /* Fetch the value and save it (since it's the result of the expression) */ - LoadExpr (CF_NONE, Expr); - g_save (Flags | CF_FORCECHAR); + LoadExpr (CF_NONE, Expr); + AddCodeLine ("dec %s", ED_GetLabelName(Expr, 0)); - /* If we have a pointer expression, increment by the size of the type */ - if (IsTypePtr (Expr->Type)) { - g_dec (Flags | CF_CONST | CF_FORCECHAR, CheckedSizeOf (Expr->Type + 1)); } else { - g_dec (Flags | CF_CONST | CF_FORCECHAR, 1); + + /* Push the address if needed */ + PushAddr (Expr); + + /* Fetch the value and save it (since it's the result of the expression) */ + LoadExpr (CF_NONE, Expr); + g_save (Flags | CF_FORCECHAR); + + /* If we have a pointer expression, increment by the size of the type */ + if (IsTypePtr (Expr->Type)) { + g_dec (Flags | CF_CONST | CF_FORCECHAR, CheckedSizeOf (Expr->Type + 1)); + } else { + g_dec (Flags | CF_CONST | CF_FORCECHAR, 1); + } + + /* Store the result back */ + Store (Expr, 0); + + /* Restore the original value in the primary register */ + g_restore (Flags | CF_FORCECHAR); } - /* Store the result back */ - Store (Expr, 0); - - /* Restore the original value in the primary register */ - g_restore (Flags | CF_FORCECHAR); - /* The result is always an expression, no reference */ ED_MakeRValExpr (Expr); } From a2f61c667ab49cd99632cbd958ef79057d80ac77 Mon Sep 17 00:00:00 2001 From: Lauri Kasanen Date: Sun, 7 May 2017 20:31:41 +0300 Subject: [PATCH 3/5] Update comments and location checks according to Greg --- src/cc65/expr.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/cc65/expr.c b/src/cc65/expr.c index e5c1a17b9..e87b02ffc 100644 --- a/src/cc65/expr.c +++ b/src/cc65/expr.c @@ -1534,8 +1534,8 @@ static void PostInc (ExprDesc* Expr) /* Get the data type */ Flags = TypeOf (Expr->Type); - /* Fast path: char */ - if ((Flags & CF_CHAR) == CF_CHAR && ED_GetLoc(Expr) & (E_LOC_GLOBAL | E_LOC_STATIC)) { + /* Emit smaller code if a char variable is at a constant location */ + if ((Flags & CF_CHAR) == CF_CHAR && ED_IsLocConst(Expr)) { LoadExpr (CF_NONE, Expr); AddCodeLine ("inc %s", ED_GetLabelName(Expr, 0)); @@ -1590,8 +1590,8 @@ static void PostDec (ExprDesc* Expr) /* Get the data type */ Flags = TypeOf (Expr->Type); - /* Fast path: char */ - if ((Flags & CF_CHAR) == CF_CHAR && ED_GetLoc(Expr) & (E_LOC_GLOBAL | E_LOC_STATIC)) { + /* Emit smaller code if a char variable is at a constant location */ + if ((Flags & CF_CHAR) == CF_CHAR && ED_IsLocConst(Expr)) { LoadExpr (CF_NONE, Expr); AddCodeLine ("dec %s", ED_GetLabelName(Expr, 0)); From 7adcde1707eb7ab81bc1f15099ad91276f0f11eb Mon Sep 17 00:00:00 2001 From: Lauri Kasanen Date: Sun, 7 May 2017 20:35:49 +0300 Subject: [PATCH 4/5] Add explicit postinc/dec testcase --- test/val/postincdec.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 test/val/postincdec.c diff --git a/test/val/postincdec.c b/test/val/postincdec.c new file mode 100644 index 000000000..19a6eb2a7 --- /dev/null +++ b/test/val/postincdec.c @@ -0,0 +1,23 @@ +/* + !!DESCRIPTION!! char-sized post-increment and -decrement + !!ORIGIN!! cc65 regression tests + !!LICENCE!! Public Domain + !!AUTHOR!! Lauri Kasanen +*/ + + +static unsigned char val, array[2]; + +int main() { + + val = 0; + array[0] = array[1] = 10; + + array[val++] = 2; + array[val++] = 2; + --val; + array[val--] = 0; + array[val--] = 0; + + return (array[0] == array[1] && array[0] == 0) ? 0 : 1; +} From 077b14ccfa5f8b4defc2f98b01cf17e0f2a70922 Mon Sep 17 00:00:00 2001 From: Lauri Kasanen Date: Mon, 8 May 2017 10:19:26 +0300 Subject: [PATCH 5/5] test/val/postincdec: Test for the final val value --- test/val/postincdec.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/val/postincdec.c b/test/val/postincdec.c index 19a6eb2a7..6d3e9593c 100644 --- a/test/val/postincdec.c +++ b/test/val/postincdec.c @@ -19,5 +19,5 @@ int main() { array[val--] = 0; array[val--] = 0; - return (array[0] == array[1] && array[0] == 0) ? 0 : 1; + return (array[0] == array[1] && array[0] == 0 && val == 0xff) ? 0 : 1; }