From d93acb5f147ab4be430f94076e3ccb4ad90db1e8 Mon Sep 17 00:00:00 2001 From: pjs Date: Sun, 6 Apr 2003 18:32:47 +0000 Subject: [PATCH] Added peephole to replace LJMP to RET with just a RET instruction git-svn-id: https://sdcc.svn.sourceforge.net/svnroot/sdcc/trunk/sdcc@2488 4a8a32a2-be11-0410-ad9d-d568d2c75423 --- ChangeLog | 5 +++++ src/SDCCpeeph.c | 41 +++++++++++++++++++++++++++++++++++++++++ src/mcs51/peeph.def | 9 +++++++++ 3 files changed, 55 insertions(+) diff --git a/ChangeLog b/ChangeLog index 75c8008b..636e61b4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2003-04-06 Paul Stoffregen + + * src/SDCCpeeph.c: added labelIsReturnOnly test + * src/mcs51/peeph.def: Peephole 244: replace ljmp to ret with ret + 2003-04-05 * src/SDCCcse.c (cseAllBlocks): fixed bug #460088 diff --git a/src/SDCCpeeph.c b/src/SDCCpeeph.c index 13eadb79..d7aa888d 100644 --- a/src/SDCCpeeph.c +++ b/src/SDCCpeeph.c @@ -275,6 +275,44 @@ FBYNAME (labelInRange) return TRUE; } +/*-----------------------------------------------------------------*/ +/* labelIsReturnOnly - Check if label %5 is followed by RET */ +/*-----------------------------------------------------------------*/ +FBYNAME (labelIsReturnOnly) +{ + /* assumes that %5 pattern variable has the label name */ + const char *label, *p; + const lineNode *pl; + int len; + + label = hTabItemWithKey (vars, 5); + if (!label) return FALSE; + len = strlen(label); + + for(pl = currPl; pl; pl = pl->next) { + if (pl->line && !pl->isDebug && + pl->line[strlen(pl->line)-1] == ':') { + if (strncmp(pl->line, label, len) == 0) break; /* Found Label */ + if (strlen(pl->line) != 7 || !isdigit(*(pl->line)) || + !isdigit(*(pl->line+1)) || !isdigit(*(pl->line+2)) || + !isdigit(*(pl->line+3)) || !isdigit(*(pl->line+4)) || + *(pl->line+5) != '$') { + return FALSE; /* non-local label encountered */ + } + } + } + if (!pl) return FALSE; /* did not find the label */ + pl = pl->next; + if (!pl || !pl->line || pl->isDebug) return FALSE; /* next line not valid */ + p = pl->line; + for (p = pl->line; *p && isspace(*p); p++) + ; + if (strcmp(p, "ret") == 0) return TRUE; + return FALSE; +} + + + /*-----------------------------------------------------------------*/ /* operandsNotSame - check if %1 & %2 are the same */ /*-----------------------------------------------------------------*/ @@ -607,6 +645,9 @@ callFuncByName (char *fname, { "portIsDS390", portIsDS390 }, + { + "labelIsReturnOnly", labelIsReturnOnly + }, { "24bitModeAndPortDS390", flat24bitModeAndPortDS390 } diff --git a/src/mcs51/peeph.def b/src/mcs51/peeph.def index a7c433e7..7e404ed0 100644 --- a/src/mcs51/peeph.def +++ b/src/mcs51/peeph.def @@ -219,6 +219,15 @@ replace { %2: } if labelInRange + +replace { + ljmp %5 +} by { + ; Peephole 244 replaced ljmp to ret with ret + ret +} if labelIsReturnOnly + + replace { ljmp %5 %1: -- 2.30.2