From 25451c9197f0c42c161394e4587a16d08556e09b Mon Sep 17 00:00:00 2001 From: bernhardheld Date: Sun, 2 Dec 2001 23:01:45 +0000 Subject: [PATCH] fix "inc sp" git-svn-id: https://sdcc.svn.sourceforge.net/svnroot/sdcc/trunk/sdcc@1659 4a8a32a2-be11-0410-ad9d-d568d2c75423 --- src/mcs51/gen.c | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) diff --git a/src/mcs51/gen.c b/src/mcs51/gen.c index d91f0d4c..9a81b0cf 100644 --- a/src/mcs51/gen.c +++ b/src/mcs51/gen.c @@ -2171,6 +2171,7 @@ genFunction (iCode * ic) symbol *sym; sym_link *ftype; bool switchedPSW = FALSE; + int calleesaves_saved_register = -1; _G.nRegsSaved = 0; /* create the function header */ @@ -2367,6 +2368,9 @@ genFunction (iCode * ic) if (bitVectBitValue (sym->regsUsed, i) || (mcs51_ptrRegReq && (i == R0_IDX || i == R1_IDX))) { + /* remember one saved register for later usage */ + if (calleesaves_saved_register < 0) + calleesaves_saved_register = i; emitcode ("push", "%s", mcs51_regWithIdx (i)->dname); _G.nRegsSaved++; } @@ -2418,16 +2422,32 @@ genFunction (iCode * ic) } else if (i > 5) - { - - /* ISRs will be handled by the code above, because they - can't have parameters. Therefore it's save to use r0 */ - emitcode ("mov", "r0,a"); - emitcode ("mov", "a,sp"); - emitcode ("add", "a,#0x%02x", ((char) sym->stack & 0xff)); - emitcode ("mov", "sp,a"); - emitcode ("mov", "a,r0"); - + { + if (IFFUNC_CALLEESAVES(sym->type)) + { + /* if it's a callee-saves function we need a saved register */ + if (calleesaves_saved_register >= 0) + { + emitcode ("mov", "%s,a", mcs51_regWithIdx (calleesaves_saved_register)->dname); + emitcode ("mov", "a,sp"); + emitcode ("add", "a,#0x%02x", ((char) sym->stack & 0xff)); + emitcode ("mov", "sp,a"); + emitcode ("mov", "a,%s", mcs51_regWithIdx (calleesaves_saved_register)->dname); + } + else + /* do it the hard way */ + while (i--) + emitcode ("inc", "sp"); + } + else + { + /* not callee-saves, we can clobber ar0 */ + emitcode ("mov", "ar0,a"); + emitcode ("mov", "a,sp"); + emitcode ("add", "a,#0x%02x", ((char) sym->stack & 0xff)); + emitcode ("mov", "sp,a"); + emitcode ("mov", "a,ar0"); + } } else while (i--) -- 2.47.2